配置tomcat接受发布请求

时间:2011-02-25 20:55:25

标签: java jsp tomcat servlets

如何配置tomcat,以便在发出请求时将请求参数输出到jsp文件?我是否需要一个转发到jsp的servlet或者可以在jsp文件中处理它?<​​/ p>

这是我将post请求发送到tomcat服务器的方法 -

 public void sendContentUsingPost() throws IOException {

        HttpConnection httpConn = null;
          String url = "http://LOCALHOST:8080/services/getdata";
     //   InputStream is = null;
        OutputStream os = null;

        try {
          // Open an HTTP Connection object
          httpConn = (HttpConnection)Connector.open(url);
          // Setup HTTP Request to POST
          httpConn.setRequestMethod(HttpConnection.POST);

          httpConn.setRequestProperty("User-Agent",
            "Profile/MIDP-1.0 Confirguration/CLDC-1.0");
          httpConn.setRequestProperty("Accept_Language","en-US");
          //Content-Type is must to pass parameters in POST Request
          httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");


          // This function retrieves the information of this connection
          getConnectionInformation(httpConn);

                  String params;
          params = "?id=test&data=testdata";
                      System.out.println("Writing "+params);
          //            httpConn.setRequestProperty( "Content-Length", String.valueOf(params.length()));

          os = httpConn.openOutputStream();

          os.write(params.getBytes());

 } finally {
          if(os != null)
            os.close();
      if(httpConn != null)
            httpConn.close();
    }

        }

由于

2 个答案:

答案 0 :(得分:3)

首先,您的查询字符串无效。

params = "?id=test&data=testdata";

应该是

params = "id=test&data=testdata";

?仅在将其作为GET查询字符串连接到请求URL时才有效。当你想把它写成POST请求体时,你不应该使用它。

说,如果这个服务不应该返回HTML(例如明文,JSON,XML,CSV等),那么使用servlet。这是一个发出明文的例子。

String id = request.getParameter("id");
String data = request.getParameter("data");
response.setContentType("text/plain");
response.setContentEncoding("UTF-8");
response.getWriter().write(id + "," + data);

如果该服务应该返回HTML,那么使用JSP。将URL更改为指向JSP。

String url = "http://LOCALHOST:8080/services/getdata.jsp";

然后将以下内容添加到JSP模板以打印请求参数。

${param.id}
${param.data}

无论哪种方式,您都应该通过阅读URLConnection#getInputStream()来获得结果(响应主体)。

另见:


与具体问题无关,您没有仔细考虑字符编码。我强烈建议这样做。有关详细示例,另请参阅上面的链接。

答案 1 :(得分:1)

servlet可以通过以下方式处理get和post请求:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
   //remaning usedefinecode
    } 

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

如果您从头开始安装tomcat,请不要忘记将以下行添加到web.xml中,以便让服务器接受GET,POST等请求:

 <servlet>
    <servlet-name>default</servlet-name>
    <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>

    ...

    <init-param>
            <param-name>readonly</param-name>
            <param-value>false</param-value>
    </init-param>

   ...

</servlet>