我想在Servlet里的处理函数(如doPost)中,重新生成一个表单,再把它提交到另一个地方(如远程服务器上的某个地址)。
茫无头绪,恳请诸位开示。
---------------------------------------------------------------
try {
URL destURL = new URL("http://foo/cgi-bin/foo");
String requestString = "paramName=paramValue\r\n";
URLConnection urlConn = destURL.openConnection();
urlConn.setDoOutput(true); // we need to write
urlConn.setDoInput(true); // just to be safe...
urlConn.setUseCaches(false); // get info fresh from server
// Tell the server what kind of data you are sending
urlConn.setRequestProperty("Content-type","application/octet-stream");
// Must tell the server the size of the data you are sending.
// This also tells the URLConnection class that you are doing
// a POST instead of a GET.
urlConn.setRequestProperty("Content-length", ""+requestString.length());
// Open an output stream so you can send the info you are posting
DataOutputStream outStream = new DataOutputStream(
urlConn.getOutputStream());
// Write out the actual request data
outStream.writeBytes(requestString);
outStream.close();
// Now that you have sent the data, open up an input stream and get
// the response back from the server
DataInputStream inStream = new DataInputStream(
urlConn.getInputStream());
int ch;
// Dump the contents of the request to System.out, or wherever
// you need , such String
while ((ch = inStream.read()) >= 0) {
System.out.print((char) ch);
}
inStream.close();
} catch (Exception e) {
e.printStackTrace();
}
====================================================
向一个Web站点发送POST请求只需要简单的几步。首先要和URL下的URLConnection对话。URLConnection可以很容易的从URL得到。比如:
// Using java.net.URL and
//java.net.URLConnection
URL url = new
URL("http://jobsearch.dice.com/jobsearch/jobsearch.cgi");
URLConnection connection = url.openConnection();
然后把连接设为输出模式。URLConnection通常作为输入来使用,比如下载一个Web页。通过把URLConnection设为输出,你可以把数据向你个Web页传送。下面是如何做:
...
connection.setDoOutput(true);
最后,为了得到OutputStream,简单起见,把它约束在Writer并且放入POST信息中,例如:
...
OutputStreamWriter out = new
OutputStreamWriter(uc.getOutputStream(), "8859_1");
out.write("username=bob&password="+password+"");
// remember to clean up
out.flush();
out.close();
这样就可以发送一个看起来象这样的POST:
POST /jobsearch/jobsearch.cgi HTTP 1.0
ACCEPT: text/plain
Content-type: application/x-www-form-urlencoded
Content-length: 99
username=bob
password=someword
一旦发送成功,用以下方法就可以得到服务器的回应:
connection.getInputStream();
一些Web站点用POST形式而不是GET,这是因为POST能够携带更多的数据,而且不用URL,这使得它看起来不那么庞大。使用上面列出的大致的代码,Java代码可以和这些站点轻松的实现对话。
分享到:
相关推荐
Servlet中doGet与doPost的区别
Servlet访问流程以及doGet和doPost 能力目标 能叙述Servlet访问流程以及Servlet的生命周期 能描述init()、service()、destory()什么时候被调用的 能描述doGet()和doPost()内部访问流程
当客户端发出请求时,Servlet引擎传递给Servlet一个ServletRequest对象和一个ServletResponse对象,这两个对象作为参数传递到service()方法中。 Servlet也可以执行ServletRequest接口和ServletResponse接口。...
重新装入 Servlet 时装入 Servlet 后,服务器创建一个 Servlet 实例并且调用 Servlet 的 init() 方法。在初始化阶段,Servlet 初始化参数被传递给 Servlet 配置对象。 (2) 请求处理 对于到达服务器的客户机...
Servlet技术是Java Web开发中常用的一种技术,用于响应用户请求并生成动态内容。本文将介绍Servlet技术的基本原理以及使用方式。 首先,Servlet是一个Java类,它可以接收HTTP请求并且生成HTTP响应。当服务器接收到...
在doPost()方法中获取了客户端发送的消息,并将消息存储到数据库或其他数据源中。然后设置响应的内容类型,并输出一条确认消息和客户端发送的消息。最后在main()方法中演示了如何使用Servlet来处理客户端请求。由于...
我们在javaWeb项目时,使用doget和dopost总是会出现各种原因的中文乱码问题,楼主在查阅大量资料后,将为什么有这样的问题,和如何解决这种问题做个总结。 思维导图: 1. 应答乱码处理(response输出页面时乱码) 1.1 ...
方法初始化servlet,再根据不同请求调用doGet或doPost方法,最后再通过destroy()方法进行销毁。 2.Get和Post的区别 在页面提交数据时,get的数据大小有限制4k,post没有限制,get请求提交的数据会在地址栏显示,...
一个页面由一个编译好的 Java servlet 类(可以带有任何的 include 指令,但是没有 include 动作)表示。 这既包括 servlet 又包括被编译成 servlet 的 JSP 页面。 request是代表与 Web 客户机发出的一个请求相关...
这是一个在刚学习servlet时写的九九乘法表
doget和dopost中文乱码问题1
Step10.md:从一个 Servlet 重定向到另一个 - 新 TodoServlet。 Step11.md:第一个 JSTL 标签:使用循环 todos Step12:会话范围和请求范围之间的区别 Step13.md:添加新的待办事项 Step14.md:使用 equals 和 ...
javabean+servlet+jsp实现圆的面积和周长计算,还有注册信息然后输出
当客户机第一次调用一个Stateful Session Bean 时,容器必须立即在服务器中创建一个新的Bean实例,并关联到客户机上,以后此客户机调用Stateful Session Bean 的方法时容器会把调用分派到与此客户机相关联的Bean实例...
1、serialVersionUID作用, 一个是默认的1L,比如:private static final long serialVersionUID = 1L; 2、Servlet中service()方法 3、servlet中的doGet和doPost的不同 4、……
这个是servlet中的doGet()和doPost()的区别,介绍的是相当的详细。值得期待。
java 关于servlet的一个简单的实例demo,在web.xml的配置前台与后台的连接,获取浏览器上的数据后点击确认跳转提示成功,使用doGet,doPost的接口方法,form表单里面的action唯一标识
doget 可以打印出网页,在文本框中输入数字和字母 dopost接受数据并在根目录查看和下载文件。
Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. DIGEST_AUTH - Static variable in interface javax.servlet.http.HttpServletRequest String ...