-1

I am new to web development and am working on a web application that will use servlets and JSPs and I created both my servlet and my JSP but I am having a hard time getting the servlet to forward the request to the JSP.

In my main page, there is a <form> that uses a get method and the action is pointed to my servlet (SelectSupportUnit.do):

<form method="get" action="SelectSupportUnit.do">

And in my servlet, it forwards the results from my JDBC Query to a JSP:

request.setAttribute("suppUnitList", suppUnitList); 
RequestDispatcher view = request.getRequestDispatcher("QueryResults.jsp"); 
view.forward(request, response);

And in my web.xml file it declares the JSP (this is what it says to do in the Head First Servlets and JSPs book when forwarding a request from your servlet to a JSP):

<servlet> 
<servlet-name>SelectSupportUnit</servlet-name> 
<jsp-file>/QueryResults.jsp</jsp-file> 
</servlet> 
<servlet-mapping> 
<servlet-name>SelectSupportUnit</servlet-name> 
<url-pattern>/QueryResults.jsp</url-pattern> 
</servlet-mapping>

When I run this (using Eclipse IDE and Tomcat v7 container) it is giving me HTTP status 404. And says it cannot find my servlet (SelectSupportUnit.do).

That would make it appear as if in the <form> element that calls the servlet, you should call the JSP, but if I do this won't it bypass the servlet?

My form looks like this: My element looks like this now:

<form target="_blank" method="get" action="${PageContext.request.contextPath}/SelectSupportUnit"> 
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Minsk
  • 315
  • 1
  • 3
  • 9
  • 1
    What actually mean `SelectSupportUnit.do` ? Do your Servlet have a __dot (.)__ in its name ? – ThisaruG Feb 10 '16 at 11:44
  • 1
    Either the problem @ThisaruGuruge mentioned (wrong spelling of servlet) or you forgot to mention it in the web.xml (not pom, why fault). You should definitly call the servlet and not JSP if you don't want to bypass it. – ctst Feb 10 '16 at 11:49
  • @ThisaruGuruge, yes, that's the way it is formed. – Minsk Feb 10 '16 at 11:50
  • @Minsk Just to be certain: your package name is `SelectSupportUnit` and the class name is `do`? – ctst Feb 10 '16 at 11:52
  • I did a mistake there, I corrected the name to `SelectSupportUnit`. It was in some example and I didn't understand that the .do is not needed. – Minsk Feb 10 '16 at 12:02
  • I see u added `PageContext`, instead of the cameCase notation. It should be `pageContext` not `PageContext`. Case counts! – Simply Me Feb 10 '16 at 12:08

3 Answers3

0

You Have to mention the name of the servlet in your form

<form method="get" action="SelectSupportUnit">

Then it will go to the Servlet's doGet() method. You don't need to add SelectSupportUnit.do in the form action.

ThisaruG
  • 3,222
  • 7
  • 38
  • 60
  • This isn't a good practice. Don't ask others to up vote you, so that you can up vote them too. It's serial voting. If you find a post worth of voting, just vote it. DO NOT VOTE A POST BECAUSE HE UP VOTED YOU. – ThisaruG Feb 14 '16 at 19:05
  • Actually, I liked your answer and wanted to upvote but I couldn't because of lacking points. – Minsk Mar 22 '16 at 06:25
0

You have to map your servlet and jsp files correctly to seperated urls!

Here is a simple example for the web.xml that should work:

<web-app>
  <servlet>
    <servlet-name>SelectSupportUnit</servlet-name>
    <servlet-class>my.package.path.SelectSupportUnit</servlet-class>
  </servlet>

  <servlet>
      <servlet-name>QueryResults</servlet-name>
      <jsp-file>QueryResults.jsp</jsp-file>
  </servlet>

  <servlet-mapping>
    <servlet-name>SelectSupportUnit</servlet-name>
    <url-pattern>SelectSupportUnit.do</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>QueryResults</servlet-name>
    <url-pattern>/QueryResults.html</url-pattern>
  </servlet-mapping>
</web-app>  
Rene M.
  • 2,660
  • 15
  • 24
0

Your action is missing the context path. The action should either be action="${pageContext.request.contextPath}/SelectSupportUnit.do", or created as the result of the <c:url> JSTL action (which will automatically inserts the context path).

Second problem would be the .do name, which seems inappropriate. I would recommend using suggestive names for servlets.

The 404 is because of the poorly formed action URL. You never directly address a JSP. Always address its servlet controller.

Simply Me
  • 1,579
  • 11
  • 23