Reputation: 6603
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
)
Upvotes: 3
Views: 1274
Reputation: 34038
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>
Upvotes: 2
Reputation: 6603
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
Upvotes: 2