0

I need to implement the HttpRequestWrapper and a Filter in order to reuse my request. This is because I firstly need the request to see what the user has selected and secondly to upload a file to the tomcat server. Right now I have a Servlet Upload File "public class UploadFile extends HttpServlet {}" which runs for each user. Once it returns what the user has selected it goes to null and I can't upload a file to the server. Just wondering would I have to alter my code much in order to implement HttpRequestWrapper? Do I change from "public class UploadFile extends HttpServlet {}" to "public class UploadFile extends HttpRequestWrapper {}"?

  • You should _never_ reuse the request object. It is only valid for the current request. If you need to retain some data, then store it in the user session. – NilsH Nov 05 '14 at 13:45
  • can you please give me an example of how to do this, for instance I was getting request information through " Part myStringPart = request.getPart("ConvertFile");" then within my upload method I have the line "items = upload.parseRequest(request);" but the second request is null so it does not upload the file. How do I implement sessions to solve this issue? – user44392 Nov 05 '14 at 14:15
  • It's impossible to say without seeing some code. Please update your question with the code you have written so far. – NilsH Nov 06 '14 at 07:04
  • I added my code as an answer as I hadn't enough spaces. Please see below thanks for taking a look I appreciate it. – user44392 Nov 06 '14 at 08:55

1 Answers1

0
So firstly I check the parameters to see which checkbox has been ticked : 

String ConvertFile = request.getParameter("ConvertFile"); 

String Powershell = request.getParameter("Powershell");

String LMBackup = request.getParameter("LM_Backup");

String Restful_API = request.getParameter("Restful_API");        

if (Powershell != null) {
            Powershell = request.getParameter("Powershell");
        } 

if (LMBackup != null) {
            LMBackup = request.getParameter("LM_Backup");
        }

if (Restful_API != null) {
            Restful_API = request.getParameter("Restful_API");
        }



if (ConvertFile != null && LMBackup == null && Powershell != null && Restful_API == null)
 {
            System.out.println(ConvertFile + " and " + Powershell + " selected");

doUpload(request, response); //Here the request that is being passed is now null because I've used it previously

response.sendRedirect("index.jsp");

        }

//Upload Method is as shown //Items remains null because the request has already been used

protected void doUpload(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        try {
            System.out.println("Uploading File");
            boolean ismultipart = ServletFileUpload.isMultipartContent(request);
            System.out.println(request + " <<<<<<<<<<<<<+++++++++++++");
            if (ismultipart) {
                FileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload upload = new ServletFileUpload(factory);
                List<FileItem> items = null;
                try {
                    items = upload.parseRequest(request);
                    System.out.println(items + "<<<<<<<<<--------------");
                } catch (Exception e) {
                    System.out.println(e);
                }
  • Are you sure that the request object is null? What is the error you're getting, and at which line is it occuring? What statement is on that line? – NilsH Nov 06 '14 at 11:28
  • items = upload.parseRequest(request); //items remains null after this line. If I call the upload before I do request.getParameter, items holds all the file information. Could be a case that my input stream is being consumed by the request.getParameter command – user44392 Nov 06 '14 at 12:06
  • Does the form submitting the file have the correct content type? Also, you should not _both_ use `request.getParameter` _and_ `ServletFileUpload`. Take a look at [this post](http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet/2424824#2424824) for an example of how to get form fields when using `ServletFileUpload`. – NilsH Nov 12 '14 at 08:19