开发者

Handling response.redirect errors in JSP page

i have a condition where an URL is manipulated or changed wantedly. i have to catch the error and move to error page. but the error page is never called.

I am using response.sendRedirect("MyURL"); in the jsp page. If the URL is correct its working fine.

If i manipulate the URL and use the command it shows HTTP 404 error in the same page but not calling the error page

here is my code.

one.jsp  
---------  
<%@ page errorPage="exceptionHandler.jsp" %>  
<%

String webBrowserURL="http://URL"  

response.sendRedirect(webBrowserURL);  

%>  


开发者_如何学运维exceptionHandler.jsp  
------------------------  

<%@ page isErrorPage="true" %>  
<% String root = request.getContextPath(); %>  

                <%  
                         //get the correct url form the database and send it to the page  
                %>  


<jsp:forward page="one.jsp" />  

Help me


So if the URL is correct and the redirect works fine, then why would you expect it to work with an incorrect URL?

Check that you're using the proper URL and ensure that you're not printing anything on your JSP, like out.println("something") or that you don't have any HTML code before calling the redirect.


Hey man i think you are doing it in wrong way. As i understand your question. I hope you want to redirect your page to errorHandler.jsp in a case if exception occurs at your one.jsp page.

To make your code work correctly lemme redefine both jsp's, And i hope you will understand well from that example.


one.jsp

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

int x=0,y=23;
int z=y/x;

%>  

In above jsp page i had manually generated an exception to check whether my code is working. You could also try this to generate exception.

if(request.getParameter("e")==null)
{
throw new Exception();
}

exceptionHandler.jsp

<html>
<body>
    <%-- Log error on server side --%>
    <%
        //When the page attribute "isErrorPage" is set to "true" the exception object is available
        System.err.println("Error : " + exception.getMessage());
    %>

    <%-- Display generic error to client --%>
    <b>An error occur !</b>
</body>
</html>


You shouldn't be doing this inside a JSP file. It's often already too late to change the response then. If you check the server logs, then the chance is big that you see IllegalStateException: response already committed spreading over the logs.

The best place to do this kind of stuff is a Filter. Keep JSP development rule #1 in mind: raw Java code belongs in Java classes, not in JSP files. You're developing code, not prototyping code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜