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:
- Code: Select all
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:
- Code: Select all
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.