jsp获取post参数并将它们发布到具有新值的另一个jsp

时间:2015-09-27 07:57:32

标签: jsp post

我是jsp的新手,需要编写一个jsp获取post参数以供进一步处理。 必须将参数值设置为新值,并且必须使用POST将它们发送到另一个jsp。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

这是演示代码。它使用快速而脏的scriptlet。

<%@ page import="java.net.*,java.io.*" %>
 <%
  URL url = new URL("http://localhost:8080/target.jsp");
  HttpURLConnection conn = (HttpURLConnection)url.openConnection();
  conn.setDoOutput(true);
  conn.setDoInput(true);
  conn.setRequestMethod("POST");
  OutputStream connOut = conn.getOutputStream();
  connOut.write("a=111&b=222".getBytes()); //you might need URLEncoder
  connOut.flush();
  connOut.close();
  if(HttpURLConnection.HTTP_OK == conn.getResponseCode()){
    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()), 8192);
         StringBuilder sb = new StringBuilder();
     String line = "";
    while ((line = in.readLine()) != null) {
             sb.append(line);
    }
        in.close(); 
        out.print(sb.toString());
  }else out.print("Response code is " + conn.getResponseCode());
%>

这里是target.jsp

hello from target.jsp a is ${param.a} and b is ${param.b} 
  and request method is  ${pageContext.request.method}