Handling response.redirect errors on a JSP page

I have a condition where the URL is manipulated or changed separately. I have to catch the error and go to the error page. but the error page is never called.

I am using response.sendRedirect ("MyURL"); on the jsp page. If the URL is correct, then it works fine.

If I manipulate the URL and use the command, it shows an HTTP 404 error on the same page, but does not cause 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

+3
source share
3 answers

So, if the url is correct and the redirect works fine, then why do you expect it to work with the wrong url?

, URL- , JSP, out.println( "-" ) HTML .

+1

, , . . , errorHandler.jsp , one.jsp .

lemme, jsp, , .


one.jsp

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

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

%>  

jsp , , . .

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>
+1

JSP . . , , IllegalStateException: response already committed, .

The best place for this type is Filter. Follow rule 1 of JSP development: Raw Java code belongs to Java classes, not JSP files. You are developing code, not prototype code.

0
source

Source: https://habr.com/ru/post/1735000/


All Articles