HTTP error 302: response.sendRedirect()

Hi everybody,

I have a first servlet (AGSenderServlet) sending a file to a second one (AGReceiverServlet). In this second servlet I’m receiving a file and then redirect to a JSP page to run a Headless Client to run a method in my Servoy Solution. But I’m getting the following exception:

13-Sep-2010 13:20:42 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet AGSenderServlet threw exception
java.io.IOException: Received HTTP status code 302
        at AGSenderServlet.doGet(AGSenderServlet.java:43)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

....

I’ve looked on internet and apparently the error 302 is linked to response.sendRedirect().

Here is my code:

public class AGReceiverServlet extends HttpServlet {
	public void doGet(HttpServletRequest request,
            HttpServletResponse response)
	throws ServletException, IOException {
	    // Create a factory for disk-based file items
		DiskFileItemFactory factory = new DiskFileItemFactory();

	    // Create a new file upload handler
		ServletFileUpload upload = new ServletFileUpload(factory);
		String realPath = this.getServletContext().getRealPath("sync/in");
		List items = null;
				try {
					items = upload.parseRequest(request);
				} catch (FileUploadException e) {
					e.printStackTrace();
				}
				// Process the uploaded items
				Iterator iter = items.iterator();

				try {
					String fileName = "blabla";
					while (iter.hasNext()) {
						FileItem item = (FileItem) iter.next();
						// Process a file upload
						if (!item.isFormField()) {					
							fileName = item.getName();
							long sizeInBytes = item.getSize();
							//Write to File
							if (fileName != null) {
						        fileName = FilenameUtils.getName(fileName);
						    }
							File uploadedFile = new File(realPath);
							if (!uploadedFile.exists())
								uploadedFile.mkdirs();
							uploadedFile = new File(realPath+"/"+fileName);
							
							item.write(uploadedFile);
						}
					}

					System.out.println("http://"+request.getServerName()+':'+request.getServerPort()+"/upTool/filereceived.jsp?filename="+fileName);
					String redirectURL = "http://"+request.getServerName()+':'+request.getServerPort()+"/upTool/filereceived.jsp?filename="+fileName;
					response.sendRedirect(redirectURL);					
					
				} catch (Exception e) {
					e.printStackTrace();
				}								
	}
	
	public void doPost(HttpServletRequest request,
            HttpServletResponse response)
	throws ServletException, IOException {
			doGet(request, response);
	}
}

The file received is saved properly and is ok.
Using print out I have discovered that it was the running the whole code but not the code in filereceived.jsp (the JSP of the sendRedirect() ).
I have tried the url used in the sendRedirect() and it works ok.

Any idea?

Cheers.

Sounds more like a question to be posted on a Java forum.

Paul

pbakker:
Sounds more like a question to be posted on a Java forum.

Paul

That’s what I did 2 hours ago. But no reply until 5min ago. (all americans are still sleeping :lol: ).
Not a very helpful one btw but we are progressing :)

Hi everybody,

I’m still struggling with this redirect :(
Someone is helping me on the Java forum, but I don’t really understand what he wants to explain me :?
Maybe some of you will understand better than I: http://forums.sun.com/thread.jspa?messa … 4#11047964

To resume, I to call a 1st servlet which calls a 2nd one and which call a JSP file. At the moment the 1st one is called and calls correctly the 2nd one. But after the JSP file is not called (or run).
This JSP file is actually starting an Headless Client to run a method of my solution:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import = "java.util.*" %>
<%@ page import = "com.servoy.j2db.server.headlessclient.*" %>
<%@ page import = "com.servoy.j2db.util.*" %>
<%@ page import = "com.servoy.j2db.dataprocessing.IDataSet" %>
<%@ page errorPage="errorpage.jsp" %> 
<% 
	ISessionBean servoy_hc = (ISessionBean)session.getAttribute("servoy");
	if (servoy_hc == null)
	{
		servoy_hc = HeadlessClientFactory.createSessionBean(request,"AssetGuardian","HeadlessClient","pwd");
		session.setAttribute("servoy",servoy_hc);
		boolean ok = servoy_hc.setMainForm("frm_files_handler");
		if (!ok)
		{
			out.print("error cannot work on required form");
			return;
		}
	}else{
		out.print("Not in if !");
		return;
	}

	String filename = request.getParameter("filename");
	Object pdfNameObj = servoy_hc.executeMethod(null,"genPDF",new Object[]{filename});
	String pdfName = pdfNameObj.toString();
	session.setAttribute("servoy",null);
%>
<html>
<body>
</body>
</html>

Managed to solve the problem.
I’m using a server redirect instead of client one, which is what I should have done from the start actually :lol:

RequestDispatcher dispatcher = request.getRequestDispatcher(redirectURL);
request.setAttribute("filename",fileName);
dispatcher.forward(request, response);