Send response and request from JSP to servlet

14,161

Solution 1

you can use the RequestDispatcher forward(ServletRequest request, ServletResponse response)

Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server.

You can do it like this:

ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/YourServlet");
rd.forward(request, response);

UPDATE

Also note on your code that you have response.setRedirect instead of response.sendRedirect(...) but note that this method will not work as you want it to because it just asks the browser to make a new request to your servlet rather than forward your request and response object to that servlet . See RequestDispatcher.forward() vs HttpServletResponse.sendRedirect() for more info

Solution 2

You can just use <jsp:include> on a servlet URL.

<jsp:include page="/servletURL" />

The servlet doXxx() method will just be invoked with the current request/response. Note that the servlet cannot forward to another JSP afterwards. It has to write directly to the response, or to set some request/session attributes which the JSP can intercept on after the <jsp:include> line.

Note that this is bad design. You're abusing a JSP as a front controller. It should be the other way round. The servlet should act as a front controller and the JSP should act as a view. The client should send the request to the servlet URL directly instead of to some JSP file. The servlet should perform the business job and finally forward to a JSP to let it present the results in HTML. See also our servlets tag wiki page for some Hello World examples.

Share:
14,161
nimrod
Author by

nimrod

I am a fullstack developer (PHP, Java, AngularJS, etc).

Updated on June 04, 2022

Comments

  • nimrod
    nimrod almost 2 years

    How can I send my response and request object from a jsp file to a servlet by code? I don't want to submit a form or so.

    I tried it with:

    response.setRedirect("my page"):
    

    But then it says:

    Exception in thread "main" org.apache.http.client.HttpResponseException: Moved Temporarily
        at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:68)
        at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:54)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:945)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:919)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:910)
        at com.xx.xx.client.Client.sendPOSTRequest(Client.java:185)
        at com.xx.xx.client.Client.main(Client.java:46)
    

    As clarification: I have a client that sends a post request to a JSP file. This JSP file parses a file and puts the needed information into the session. I want to call a servlet from this jsp file to add something into the database. I think this error code is thrown by this line String responseBody = httpclient.execute(httppost, responseHandler);

  • nimrod
    nimrod over 11 years
    I tried this before, but I don't see that there is also a request to the specified servlet. Any idea why?
  • nimrod
    nimrod over 11 years
    Your code works perfectly. The output was just inside another console. Thanks very much!