Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

HTTP error 302: response.sendRedirect()

843842Sep 13 2010 — edited Sep 16 2010
Hi everybody,

I have a first servlet (AGSenderServlet) sending a file to a second one (AGReceiverServlet). This second servlet I'm receiving a file and then redirect to a JSP page. 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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 14 2010
Added on Sep 13 2010
20 comments
7,134 views