8

I want to redirect JSP page from one servlet. All JSP pages are under Web Content. not under Web-INF. I have a problem of calling that JSP pages. I got 404 errors. problem with path.

How can I call jsp pages under Web Content?

ServletContext context = getServletContext();
                 RequestDispatcher dispatcher = context.getRequestDispatcher("/thankYou.jsp");
                 dispatcher.forward(request,response);

Thanks ahead.

PROBLEM SOLVED !

kitokid
  • 3,009
  • 17
  • 65
  • 101
  • 2
    I solved it using like this RequestDispatcher requestDispatcher ; requestDispatcher = request.getRequestDispatcher("/thankYou.jsp" ) ; requestDispatcher.forward( request, response ) ; – kitokid Nov 01 '11 at 06:08

5 Answers5

18

I solved the problem using RequestDispatcher like this:

RequestDispatcher requestDispatcher; 
requestDispatcher = request.getRequestDispatcher("/thankYou.jsp");
requestDispatcher.forward(request, response);
Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62
kitokid
  • 3,009
  • 17
  • 65
  • 101
10

A slightly cleaner way to write this code is:

request.getRequestDispatcher("/thankyou.jsp").forward(request, response);
ryandlf
  • 27,155
  • 37
  • 106
  • 162
0

This error occurs when you have an error in java scriptlet of your jsp you've forwarded your request to.
For example I was calling <% request.getAttribute("user"); %> whereas the problem solved when I used <% request.getParameter("user") %>

Pulkit
  • 1
  • 4
-1

Use SendDirect if you want to work with JSP pages

response.sendRedirect("/thankyou.jsp");

This is simpe thing to use than RequestDispatcher which doesn't work with doPost().

Rohan
  • 5,121
  • 1
  • 17
  • 19
  • Of course it doesn't - a POST request isn't ment to be dispatched with "visual output", that is sent back to the client. But you don't redirect to the JSP then, but to the Path that is mapped for the Servlet (so the site is requested by the Client via GET again). – UniversE Nov 04 '14 at 14:29
  • With request dispatcher we can forward request and response objects to next page from an intermediate page, which is not possible with your way. – Susheel Singh Jan 08 '15 at 08:56
-1

Better way to use 'sendRedirect()' method using response object.

you can write like

 response.sendRedirect("./newpage.jsp");

This will send control to your 'newpage.jsp' page.

Kedar1442
  • 217
  • 2
  • 8
  • 1
    No, this is the ultimative wrong way. This would send a HTTP 302 to the client and redirect it to the JSP page, which makes the Servlet pretty useless. If you put your jsp into the "WEB-INF/view" directory e.g. it wouldn't work anyway. – UniversE Nov 04 '14 at 14:27