aspose file tools*
The moose likes JSP and the fly likes Servlet to JSP issues Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Servlet to JSP issues" Watch "Servlet to JSP issues" New topic
Author

Servlet to JSP issues

Chris Whited
Greenhorn

Joined: Oct 19, 2012
Posts: 24

Hi, 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?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60055
    
  65

Chris Whited wrote:Hi, I am new to web development

Welcome to the Ranch!

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

Two things:
  • What's with the .do suffix? That's an antiquated convention from Struts 1 days that just makes your stuff look dated. I'd drop it in favor of more modern mappings.
  • 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).


  • And in my web.xml file it declares the JSP

    Why? Superfluous and unnecessary. Don't bother.

    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 has nothing to do with the forwarding. It's because of the poorly formed action URL.

    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?

    You never directly address a JSP. Always address its servlet controller.

    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Chris Whited
    Greenhorn

    Joined: Oct 19, 2012
    Posts: 24

    Well after that change to my action attribute definitely helped! It is recognizing that it should be sending it to a JSP. But now I am getting an HTTP status 404 for the JSP. Do I need to declare the fully qualified path in my servlet to the JSP?

    And could you give me a little more detail on the <c:url> element. I know it goes into my JSP and it has something to do with URL encoding. But I am unsure how to use it in this instance.

    Thanks for the response. I really appreciate the help.

    p.s. I also changed my web.xml back to:
    <servlet>
    <servlet-name>SelectSupportUnit</servlet-name>
    <servlet-class>com.dstsystems.SelectSupportUnit</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>SelectSupportUnit</servlet-name>
    <url-pattern>/SelectSupportUnit.do</url-pattern>
    </servlet-mapping>
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60055
        
      65

    When you forward to the JSP, the path should be relative from the web context root and starting with the "/".

    So, for example: "/WEB-INF/views/something.jsp"
    Chris Whited
    Greenhorn

    Joined: Oct 19, 2012
    Posts: 24

    This is weird, when I posted this thread on Friday, it was working just fine (except I was getting a Null Pointer Exception cause my servlet is incorrect). But today when I came in and loaded it up I am getting this error:
    /Disaster_Recovery_Web_Page/$%7BPageContext.request.contextPath%7D/SelectSupportUnit

    I looked around and I found a forum on this site with that error and I went through it to see if I was having the same problem (forum thread found here. And at the end of that forum his problem was he needed to rename his JSP to .jsp as opposed to .html. However, mine is already named .jsp.

    My <form> element looks like this now:
    <form target="_blank" method="get" action="${PageContext.request.contextPath}/SelectSupportUnit">

    And I don't have any page directives that would disable the EL. I am not sure why it isn't working today. Any ideas?
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60055
        
      65

    pageContext not PageContext. Case counts.
    Chris Whited
    Greenhorn

    Joined: Oct 19, 2012
    Posts: 24

    Still getting the same error. Does it have something to do with isELIgnored? I am using the <form> element in my index.html file. Or might it be something in my servlet (SelectSupportUnit) or jsp?
    Seetharaman Venkatasamy
    Ranch Hand

    Joined: Jan 28, 2008
    Posts: 5575

    Plus, you are missing .do
    Chris Whited
    Greenhorn

    Joined: Oct 19, 2012
    Posts: 24

    I added the .do back. Here is my <form> element:
    <form target="_blank" method="get" action="${pageContext.request.contextPath}/SelectSupportUnit.do">

    Here is my servlet code:


    And here is my JSP code:


    And I am still getting the error. Does there appear to be anything wrong?
    Seetharaman Venkatasamy
    Ranch Hand

    Joined: Jan 28, 2008
    Posts: 5575

    your Servlet is executing ? and

    Change this to

    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60055
        
      65

    Yes... whenever you see a URL or path with ".." in it, it needs to be changed!
    Chris Whited
    Greenhorn

    Joined: Oct 19, 2012
    Posts: 24

    Okay I changed the path to /WEB-INF/QueryResults.jsp.

    And as far as my servlet executing; it was executing, then I tried to forward the request to a JSP and it was giving me issues which is why I started this thread. Then Bear recommended changing my action item, so I did, and it worked just fine on Friday (besides getting a Null Pointer Exception) and now when I run the page and click on my button that invokes the servlet it is giving me this error:
    /Disaster_Recovery_Web_Page/$%7BpageContext.request.contextPath%7D/SelectSupportUnit.do

    It's like something happened and now it won't process the EL
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60055
        
      65

    In the future, please cut/paste the exact error message including relevant portions of the stack trace. We need accurate info on the error to help diagnose.

    I can see in this case that the problem is that the EL is not evaluating in your page. We need to find out why not. Did you change the web.xml? The file type? Anything?
    Chris Whited
    Greenhorn

    Joined: Oct 19, 2012
    Posts: 24

    I am using Tomcat 7.0.3 as my container, and I am using Eclipse Juno as my IDE. I am using JSP 2.0.

    When I run my main page--called index.html--it loads that page just fine. I have a button on that page that is invokes the servlet SelectSupportUnit.java. It is that button that has the <form> element I mentioned earlier.
    When I click on that button, I get a new window that has this error:
    HTTP Status 404 - /Disaster_Recovery_Web_Page/$%7BpageContext.request.contextPath%7D/SelectSupportUnit.do

    type Status report

    message /Disaster_Recovery_Web_Page/$%7BpageContext.request.contextPath%7D/SelectSupportUnit.do

    description The requested resource is not available.
    Apache Tomcat/7.0.30

    In the address bar in the browser for this (above) window I get this:
    http://localhost:8080/Disaster_Recovery_Web_Page/$%7BpageContext.request.contextPath%7D/SelectSupportUnit.do?supportUnits=AWD+VM+Rep


    When I run just the servlet, it seems to be working fine.

    And the only thing that I changed in my web.xml was the stuff in the <web-app> tag (not sure of its name) and I added a welcome file and error page. Here is my web.xml code:
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60055
        
      65

    Create a JSP that contains: ${3+4}

    Ignore everything else until that shows 7. Everything else is just fluff until that works.

    Check your web.xml header. A mistake there will cause the servlet container to fall back to minimal functionality.
    Chris Whited
    Greenhorn

    Joined: Oct 19, 2012
    Posts: 24

    Okay. Should it be accessed directly from my index.html file? or should it be done through the servlet?
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60055
        
      65

    Directly. Cut out the middle man for debug purposes.
    Chris Whited
    Greenhorn

    Joined: Oct 19, 2012
    Posts: 24

    Alright when I run that JSP on the server I get 7.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60055
        
      65

    Within the same web app?

    If so what happens when you place that fragment in your JSP page? (which is named with .jsp, right?)
    Chris Whited
    Greenhorn

    Joined: Oct 19, 2012
    Posts: 24

    Correct. It was in the same web app. I didn't run the html file and connect it to the JSP with the ${3+4}. I just ran that JSP.

    And when I placed that fragment in my JSP it gave me the same error when I tried to access it through the index.html. But it gave me HTTP status 404 when I just ran it. But that is because it cannot be accessed by the client directly because it is placed inside of my WEB-INF folder.
    Chris Whited
    Greenhorn

    Joined: Oct 19, 2012
    Posts: 24

    Bear, you mentioned earlier the <c:url> tag which would automatically create the context path for me. Would that be a better solution than doing ${pageContext.request.ContextPath}/SelectSupportUnit.do?
    or created as the result of the <c:url> JSTL action (which will automatically inserts the context path).
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60055
        
      65

    Wait a minute.... index.html? How does that fit in here? EL in an HTML page will not be evaluated.
    Chris Whited
    Greenhorn

    Joined: Oct 19, 2012
    Posts: 24

    Thank you guys very very much. I figured it out.

    Bear, you have been very awesome and helpful. Thanks
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60055
        
      65



    Would you care to post what the problem was? That would help any future poster who might have the same problem.
    Chris Whited
    Greenhorn

    Joined: Oct 19, 2012
    Posts: 24

    No problem.

    Since I was accessing the servlet from my index.html sheet my action item looks like this:
    <form target="_blank" method="get" action="SelectSupportUnit.do">
    That invokes the servlet, which runs SQL commands on a database, makes the ResultSet from the query a Result object and then forwards that Result object to a JSP which displays the query information in a JSP.

    If anyone would like so see my code, feel free to ask.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60055
        
      65

    One followup: it is never a good idea to send the result set to the the JSP. That exposes too much DB-specific info to the front end, and causes the result set and associated DB resources to be held open for far too long.

    Rather, copy the result set's info to regular Java collections (which to use depends upon the nature of the data) and/or beans, and close all the DB resources ASAP. Then pass the collections/beans to the front end where JSTL and EL (never scriptlets) in the JSP can use them.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Servlet to JSP issues
     
    Similar Threads
    Unable to map request to appropriate JSP using servlet-mapping in DD
    very very basic ques about using servlets
    Servlet Context Path Issue
    Please help with web.xml problem
    404 Error when trying to use a filter for a servlet