3

I am using Spring in my JSP project:

I have following mapping for all HTML requests:

<servlet>
  <servlet-name>spring</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>

and

<servlet-mapping>
   <servlet-name>spring</servlet-name>
   <url-pattern>*.html</url-pattern>
</servlet-mapping>

So each request goes to the dispatcher.

I have a JSP page, which has session.getAttribute(), which returns sessions variables to that page.

If some user accesses that page directly, then the session variable is returned as null.

To avoid this, I tried adding the following line in the JSP page:

<%@page errorPage="error.jsp"%>

The JSP file is in the same directory where that page is, but no luck. I am getting the following exception: --> 404

I also tried a combination, so that errorPage would point as follows:

<%@page errorPage="../error.jsp"%> still not working

Then, I added an entry in the Controller as follows:

 @RequestMapping("/xyz/result/error")
        public ModelAndView showErrorPageEng() {
            return new ModelAndView("errorpage", "command", null);
        }

Updated Part of Question:

I have the following tiles entry for errorpage:

<definition name="errorpage" extends="basic"
    template="/error/error_layout.jsp">
    <put-attribute name="error" value="/error/error.jsp"/>  
</definition>

And modified code in the JSP as:

<%@page errorPage="error.html"%>

The above path /xyz/result/error is as follows:

ROOT/xyz/result/error

I can access the file mydomain/xyz/result/error.html but the same is not happening, with the following exception:

Starts as

at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
at org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:745)
at org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:716)
at org.apache.jsp.error_jsp._jspService(error_jsp.java:63)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
at org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:745)


**and ends as** 

    at org.apache.tiles.servlet.context.ServletTilesRequestContext.dispatch(ServletTilesRequestContext.java:220)
    at org.apache.tiles.renderer.impl.TemplateAttributeRenderer.write(TemplateAttributeRenderer.java:44)
    at org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:103)
    at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:659)
    at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:678)
    at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:633)
    at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:322)
    at org.springframework.web.servlet.view.tiles2.TilesView.renderMergedOutputModel(TilesView.java:124)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549

)

jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
Pradeep
  • 6,303
  • 9
  • 36
  • 60
  • Did you try `<%@page errorPage="/error.jsp"%>` with just the / ? – jamesmortensen May 26 '12 at 11:41
  • for this , i am getting same exception as above ,but the page remains blank , on tomcat console the same exception is throne – Pradeep May 26 '12 at 11:51
  • Pradeep, the framework and container usually log things at a lower level than your application does, and this appears to be an error thrown by the framework/server. Consider increasing your log level to DEBUG or ALL so that you can see more about what the server is trying to do. We need to know why the error is occurring. Also, the exception is related to tiles. Is your error.jsp part of a tile? – jamesmortensen May 26 '12 at 12:00
  • Also, what is the full error, you're showing the stack trace but not the exception or line that threw that exception. – jamesmortensen May 26 '12 at 12:00
  • 1. yes, i am using tiles, let me edit the question , i will put tiles entry for error page, 2. the exceptions is about 70000 chars, i can not post here , even i am not getting any Caused By statement – Pradeep May 26 '12 at 12:04
  • How about the few lines before the exception and then a few lines into it. Unfortunately, error messages must be read in order to determine what they mean. :) – jamesmortensen May 26 '12 at 12:09
  • yes :) , please find edited question – Pradeep May 26 '12 at 12:20
  • `org.apache.jsp.error_jsp._jspService(error_jsp.java:63)`. You're using Tomcat? If so look in your work directory and try to locate error_jsp.java. It's the JSP converted to Java before being compiled. On line 63 there is something going on. Need to know what that is. Then that can tell us what's wrong in error.jsp. Any tiles in error.jsp? Are you sure that error.jsp isn't pulling in a tile that isn't getting it's data from the server? – jamesmortensen May 26 '12 at 12:43
  • Do you have 2 error.jsp pages? Your tile shows it as being in /error/error.jsp and that it's also an attribute to a tile. – jamesmortensen May 26 '12 at 12:50
  • http://stackoverflow.com/q/3552515/552792 – jamesmortensen May 26 '12 at 12:54
  • no i have only one error jsp page , will check above link , i will update that error_jsp.java:63 line soon – Pradeep May 26 '12 at 13:03
  • You're not updating anything in error_jsp.java. It's an automatically generated java file. You're just looking at it so you can determine where the specific error is in error.jsp. JSP files are just abstractions to make it easier for you to work with the HTML. Behind the scenes, they're converted to Java classes. – jamesmortensen May 26 '12 at 13:07
  • in error.jsp i only had , – Pradeep May 27 '12 at 04:58
  • Are you sure you can put an HTML file in there? Doesn't that need to be a JSP? I don't work with JSP pages or tiles that much, so I can't say for sure :) – jamesmortensen May 27 '12 at 05:17
  • no , we are not putting any html pages, every request with html ending goes to spring dispatcher, we found possible workaround ( posted in answer) but it's not the one tried using jsp errorpage – Pradeep May 27 '12 at 20:51

2 Answers2

2

We found possible workaround as follows , we can put this in jsp page, i don't know but putting return works ,

String  myObject= (String)session.getAttribute("finalList");

    if(myObject==null)
        {
            out.println("<div class=\"content\">     <div class=\"mainbarWH\">       <div class=\"article\">         <h2 class=\"style1\">Sorry !</h2>         <div class=\"clr style1\"></div>         <p class=\"style1\"><strong>Your session has been expired </strong></p>         <p class=\"style1\">Please go to <a href=\"engineering.html\">Home</a></p>       </div>       </div>     <div class=\"clr\"></div> </div>");
            return;

        }

let's say for jsp page you have like

x - tile y - tile z - tile in a jsp page and if null pointer exception comes in tile y ,

then above code gives page as ,

x -tile y - tile

so while going back , we just did out.println('some html code')

this is just workaround , not standard solution yet

Pradeep
  • 6,303
  • 9
  • 36
  • 60
  • +1 - This would work. You inspired me to post an answer that builds upon this one. You may want to consider it as well. Thanks for following up by posting a solution. Good luck! – jamesmortensen May 27 '12 at 20:58
2

Another possible solution that builds on @pradeep's existing solution is to use the HttpServletResponse object to redirect to the static HTML page.

While static HTML pages cannot be used in JSP scriplet page declarations, existing paths accessible by the browser are something that the server can redirect the user to.

The advantage of such a solution is that the HTML stays where it belongs, inside the HTML files, instead of in Java strings. This creates a more maintainable environment where code is readable by Java engineers and accessible by Web developers, yet still builds on top of the proposed workaround.

Java:

String  myObject= (String)session.getAttribute("finalList");

    if(myObject==null)
        {

            // redirect to the error page, using static HTML
            response.sendRedirect("/errorPage.html");
            return;

        }

errorPage.html:

<html>
<head></head>
<body>
  <div class="content">
    <div class="mainbarWH">
        <div class="article">
        <h2 class="style1">Sorry !</h2>
        <div class="clr style1"></div>
        <p class="style1">
            <strong>Your session has been expired </strong>
        </p>
        <p class="style1">Please go to <a href="engineering.html">Home</a></p>
     </div>
   </div>
   <div class="clr"></div> </div>
</body>
</html>
jamesmortensen
  • 33,636
  • 11
  • 99
  • 120