kitokid
kitokid

Reputation: 3117

redirect jsp from servlet RequestDispatcher

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 !

Upvotes: 8

Views: 72984

Answers (5)

Pulkit
Pulkit

Reputation: 1

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") %>

Upvotes: 0

Kedar1442
Kedar1442

Reputation: 217

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.

Upvotes: -1

kitokid
kitokid

Reputation: 3117

I solved the problem using RequestDispatcher like this:

RequestDispatcher requestDispatcher; 
requestDispatcher = request.getRequestDispatcher("/thankYou.jsp");
requestDispatcher.forward(request, response);

Upvotes: 18

Rohan
Rohan

Reputation: 5431

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().

Upvotes: -1

ryandlf
ryandlf

Reputation: 28555

A slightly cleaner way to write this code is:

request.getRequestDispatcher("/thankyou.jsp").forward(request, response);

Upvotes: 10

Related Questions