response.sendRedirect Query

P

parkarumesh

Hi

The following code snippet is a funtion in my servlet.

public static void errorHandling(int code)
{
try{
String forwardString = "/parser/errorPage.jsp?errorCode="+code;
System.out.println("****FORWARD STRING: "+forwardString);

response.sendRedirect(forwardString);
return;

/*RequestDispatcher disp =
request.getRequestDispatcher(forwardString);
disp.forward(request,response);*/

//response.resetBuffer();
//response.flushBuffer();
/*if(!response.isCommitted())
response.flushBuffer();*/

}catch(Exception ex){ex.printStackTrace();}
}

}

Here response and request are static.

Now when i call this function from a normal class it executes fine.
However if i call it from a jsp no mesaage is displyed and the foll
exception is thrown.

java.lang.NullPointerException
at
org.apache.coyote.tomcat4.CoyoteResponseFacade.isCommitted(CoyoteRes
onseFacade.java:242)
at
org.apache.coyote.tomcat4.CoyoteResponseFacade.sendRedirect(CoyoteRe
ponseFacade.java:337)
at parserPckg.ReadDate.errorHandling(ReadDate.java:79)
at
org.apache.jsp.FolderActivity_jsp._jspService(FolderActivity_jsp.jav
:101)
at
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrappe
..java:210)
at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:
95)
at
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(App
icationFilterChain.java:247)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(Application
ilterChain.java:193)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapper
alve.java:256)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveConte
t.invokeNext(StandardPipeline.java:643)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.ja
a:480)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995

at
org.apache.catalina.core.StandardContextValve.invoke(StandardContext
alve.java:191)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveConte
t.invokeNext(StandardPipeline.java:643)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.ja
a:480)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995

at
org.apache.catalina.core.StandardContext.invoke(StandardContext.java
2417)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.
ava:180)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveConte
t.invokeNext(StandardPipeline.java:643)
at
org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatch
rValve.java:171)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveConte
t.invokeNext(StandardPipeline.java:641)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.
ava:172)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveConte
t.invokeNext(StandardPipeline.java:641)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.ja
a:480)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995

at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVa
ve.java:174)
at
org.apache.catalina.core.StandardPipeline$StandardPipelineValveConte
t.invokeNext(StandardPipeline.java:643)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.ja
a:480)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995

at
org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:1
3)
at
org.apache.coyote.http11.Http11Processor.process(Http11Processor.jav
:781)
at
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proc
ssConnection(Http11Protocol.java:549)
at
org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.jav
:589)
at
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(Thread
ool.java:666)
at java.lang.Thread.run(Thread.java:534)

Also attached the errorPage.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charsekt=ISO-8859-1">
<title>Error Page-You will be redirected to the previous page</title>
</head>
<body>
<%
int errorCode = Integer.parseInt(request.getParameter("errorCode"));
System.out.println("****ERRORCODE: "+errorCode);
switch(errorCode)
{
case 1: out.println("No activity in within specified dates");%>
<a href="/parser/index.jsp">Back to Index</a>

<%break;

case 2: out.println("No activity in this folder");%>
<a href="/parser/ActivityList.jsp">Back to Index</a>

<%break;

}
%>
</body>
</html>

Plz help
 
C

chris brat

Check that your expected "errorCode" does actually exist in the request
- a redirect creates a new request and all previously existing state
data (your "errorCode" in this case is lost).

To maintain state in the request rather use a forward or if the session
if it is necessary.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top