0

In my hello world program on tomcat i am not able to forward my request from servlet to jsp page.Here are my locations:-

servlet location is webapps\hello\WEB-INF\classes\test

Location of jsp page(RequestObjectInJSP) is webapps\hello

My code to forward the request is

req.getRequestDispatcher("RequestObjectInJSP").forward(req, res);

But it gives the error The requested resource (/hello/RequestObjectInJSP) is not available.

Not sure what i am missing here?

Question2:- similarily if i try to forward the request from one servlet to another servlet(both lying under same folder) does not work .Below is the code snippet

req.getRequestDispatcher("servlet2").forward(req, res);

If i give the mapping of servlet2 in web.xml then it starts working.As per my understanding if we forward the request from one servlet to another and both lying under webinf/classes folder it should work without giving the sevlet 2 mapping in web.xml. Right?

M Sach
  • 33,416
  • 76
  • 221
  • 314

1 Answers1

1

Your JSP file is missing the JSP extension. Add it and fix the path accordingly.

req.getRequestDispatcher("RequestObjectInJSP.jsp").forward(req, res);

As to your 2nd question, yes definitely you need to map the servlet on an URL pattern in order to be able to forward to the URL(!!) of the given servlet.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks Balu. It worked.Regarding second question, yo mean even if we have to forward/redirect the request, we need to give mapping for every servlet in web.xml.Correct? – M Sach Oct 06 '11 at 18:12
  • Yes. The `forward()` expects an URL. So you *really* have to map the servlet on that URL. You can alternatively also look at MVC front controller pattern so that you end up with only a single servlet. Or, of course, use a real MVC framework like JSF, SpringMVC, etc so that you end up with just a JSP (or Facelets) file and a simple JavaBean class per view. See also http://stackoverflow.com/questions/3541077/design-patterns-web-based-applications/ – BalusC Oct 06 '11 at 18:13