1

How can I configure tomcat so when a post request is made the request parameters are outputted to a jsp file? Do I need a servlet which forwards to a jsp or can this be handled within a jsp file ?

Here is my method which sends the post request to the tomcat server -

 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();
    }

        }

Thanks

blue-sky
  • 45,835
  • 124
  • 360
  • 647
  • You can use [RequestDispatcher](http://download.oracle.com/javaee/5/api/javax/servlet/RequestDispatcher.html) to forward the request,response to a JSP page and then use `request.getParameter()` in the JSP page – Alpine Feb 25 '11 at 21:11
  • You can just use a JSP page for that. No reason to create a servlet for simple request. A good example is here http://stackoverflow.com/questions/2548687/get-all-parameters-from-jsp-page – Amir Raminfar Feb 25 '11 at 21:00
  • Ok, so does this line - String url = "http://LOCALHOST:8080/services/getdata" need to change to - String url = "http://LOCALHOST:8080/getparameters.jsp ? – blue-sky Feb 25 '11 at 21:02
  • How will my jsp page be refreshed ? Do I need some kind of listener ? – blue-sky Feb 25 '11 at 21:03
  • I think you are confused. But you need to create the jsp page for this to work. Create a jsp page and use a container like tomcat to place the code snippet in there. Try hitting it from your browser first. If it works then you can use your "test" client. – Amir Raminfar Feb 25 '11 at 21:04

2 Answers2

3

First of all, your query string is invalid.

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

It should have been

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

The ? is only valid when you concatenate it to the request URL as a GET query string. You should not use it when you want to write it as POST request body.

Said that, if this service is not supposed to return HTML (e.g. plaintext, JSON, XML, CSV, etc), then use a servlet. Here's an example which emits plaintext.

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

If this service is supposed to return HTML, then use JSP. Change the URL to point to the JSP's one.

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

And then add the following to the JSP template to print the request parameters.

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

Either way, you should be able to get the result (the response body) by reading the URLConnection#getInputStream().

See also:


Unrelated to the concrete problem, you are not taking character encoding carefully into account. I strongly recommend to do so. See also the above link for detailed examples.

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
1

A servlet can handle both get and post request in following manner:

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);
    }

If you have a tomcat installation from scratch, don't forget to add the following lines to web.xml in order to let the server accept GET, POST, etc. request:

 <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>
Sathyajith Bhat
  • 19,739
  • 21
  • 90
  • 126
Ajay Takur
  • 5,287
  • 4
  • 31
  • 50
  • Hi, good point but I also 'd like to know is it fine for TomEE 1.7.4 for example cause I have error on its start? It throws me something like `java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost...` – user390525 Nov 15 '19 at 14:52