当前位置: 技术问答>java相关
!!Servlet里如何提交表单?
来源: 互联网 发布时间:2017-04-30
本文导语: 我想在Servlet里的处理函数(如doPost)中,重新生成一个表单,再把它提交到另一个地方(如远程服务器上的某个地址)。 茫无头绪,恳请诸位开示。 | try { URL destURL = new URL("http://foo/cgi-bin...
我想在Servlet里的处理函数(如doPost)中,重新生成一个表单,再把它提交到另一个地方(如远程服务器上的某个地址)。
茫无头绪,恳请诸位开示。
茫无头绪,恳请诸位开示。
|
try {
URL destURL = new URL("http://foo/cgi-bin/foo");
String requestString = "paramName=paramValuern";
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();
}
URL destURL = new URL("http://foo/cgi-bin/foo");
String requestString = "paramName=paramValuern";
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();
}