HTTP error 302: response.sendRedirect()

The forum to discuss the Headless version of Servoy. Web, Java and Servlet development questions can all be posted here.

HTTP error 302: response.sendRedirect()

Postby Foobrother » Mon Sep 13, 2010 3:58 pm

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:
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.
Current configuration: Servoy 5.2.6 Build 1011, Java 6u24, PostgreSQL 8.3, Windows Server 2003

Servoy / Java Developer
http://www.assetguardian.com
User avatar
Foobrother
 
Posts: 530
Joined: Tue Jan 13, 2009 5:46 pm

Re: HTTP error 302: response.sendRedirect()

Postby pbakker » Mon Sep 13, 2010 4:33 pm

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

Paul
pbakker
 
Posts: 2822
Joined: Wed Oct 01, 2003 8:12 pm
Location: Amsterdam, the Netherlands

Re: HTTP error 302: response.sendRedirect()

Postby Foobrother » Mon Sep 13, 2010 4:51 pm

pbakker wrote: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 :)
Current configuration: Servoy 5.2.6 Build 1011, Java 6u24, PostgreSQL 8.3, Windows Server 2003

Servoy / Java Developer
http://www.assetguardian.com
User avatar
Foobrother
 
Posts: 530
Joined: Tue Jan 13, 2009 5:46 pm

Re: HTTP error 302: response.sendRedirect()

Postby Foobrother » Tue Sep 14, 2010 12:29 pm

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:
Code: Select all
<%@ 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>
Current configuration: Servoy 5.2.6 Build 1011, Java 6u24, PostgreSQL 8.3, Windows Server 2003

Servoy / Java Developer
http://www.assetguardian.com
User avatar
Foobrother
 
Posts: 530
Joined: Tue Jan 13, 2009 5:46 pm

Re: HTTP error 302: response.sendRedirect()

Postby Foobrother » Thu Sep 16, 2010 5:32 pm

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:
Code: Select all
RequestDispatcher dispatcher = request.getRequestDispatcher(redirectURL);
request.setAttribute("filename",fileName);
dispatcher.forward(request, response);
Current configuration: Servoy 5.2.6 Build 1011, Java 6u24, PostgreSQL 8.3, Windows Server 2003

Servoy / Java Developer
http://www.assetguardian.com
User avatar
Foobrother
 
Posts: 530
Joined: Tue Jan 13, 2009 5:46 pm


Return to Servoy Headless Client

Who is online

Users browsing this forum: No registered users and 3 guests