0

I need a single page with a file upload and a text area where the contents of the file are printed in this page.

At the moment I have a jsp file and a servlet:

Part of index.jsp :

 <form action="FileReader" ENCTYPE="multipart/form-data" method="POST">
 <textarea name="textinputarea" rows="14" cols="130" readonly>
  Some text 
 </textarea>
 <br> <br><tr>


  <td valign="top" align="left" height="200" width="33%">
  <img class="start_img" src="file_Selections.jpg"> <br> 

  <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
  <input type="file" name="user_file" accept="text/xml">   
  <input type="submit" value="Validate" /> <br>
   </form>

Part of the servlet:

public void doPost(HttpServletRequest request, HttpServletResponse response)      
String name = request.getParameter("textinputarea");
(...)
}else {
String otherFieldName = item.getFieldName();
String otherFieldValue = item.getString();}}

(...)

out.println("<html>");
out.println("<head>");
out.println("<title>Processing get requests with data</title>");
out.println("</head>");

// body section of document
out.println("<body>");
while ((strLine = br.readLine()) != null) {

// Print the content on the console
out.println(strLine + "</br>");
 }
 out.println("</body>");

 // end of html document
 out.println("</html>");
  out.close();

  } catch (Exception e) {
 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
 };
 }

This actually prints the content of file in a new page. I tried to give the same name of text area and the "String name = request.getParameter("textinputarea"); "..

Thanks for your time!

Angelina
  • 15
  • 1
  • 6

1 Answers1

1

All form fields are available in this else block which you completely ignored in the postprocessing.

} else {
    String otherFieldName = item.getFieldName();
    String otherFieldValue = item.getString();
}

Don't ignore it. Those values represent the name=value pairs of regular form fields.

Note that you cannot use getParameter() on a multipart/form-data encoded request. That's exactly why you were using Apache Commons FileUpload to extract the file. You should use the same API to extract the other parts of a multipart/form-data request.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • OK thanks for the advices! Trying to find a way to display the loaded file into the textarea, I noticed that the default text(which are some instructions for the user) has not been removed. I tried to use some javascript but couldnt find a way. Finally, I found by sending the request to another jsp which actually is the same jsp as the one I already got except that the textarea is blank.So now when a file is uploaded the instructions are removed and the uploaded file appeared. I was wondering if there is more efficient way to do it. – Angelina Dec 28 '11 at 18:13
  • These the commands i use: request.setAttribute("textArea", file); request.getRequestDispatcher("Fileopened.jsp").forward(request, response); – Angelina Dec 28 '11 at 18:16
  • Just print the textarea's body dynamically/conditionally. E.g. `` and use the same servlet to preprocess the request (in `doGet()`) and postprocess the request (in `doPost()`). In the `doGet()` you set the default value and in `doPost()` you set the file content. Finally open the page by servlet's URL instead of JSP's URL. See also http://stackoverflow.com/tags/servlets/info – BalusC Dec 28 '11 at 18:21
  • In which part should i call get and post? I mean in the "method" field of the form currently i have "post". where should i put the "get" in order to be executed? – Angelina Dec 28 '11 at 18:50
  • Uh, just keep your form POST. The `doGet()` will be called when you open the page in browser the usual way (if you're using servlet's URL instead of JSP's URL). Once again, read the aforelinked servlets wiki page for concrete examples and detailed explanation. – BalusC Dec 28 '11 at 18:52
  • ok, i did it as you said and it worked. I am gonna read the wiki page to get a better idea. Thanks for your help! – Angelina Dec 28 '11 at 19:09
  • Oh a last question, is that ok to open the page by servlet's url? Do you know if is there any way to open it from jsp and work? – Angelina Dec 28 '11 at 19:13
  • Why would it not be OK? This way you're adhering the proper MVC approach. Just hide the JSP away in `/WEB-INF` folder if all you want is to prevent enduser from accessing it directly. Again, also already mentioned in the aforelinked wiki page. – BalusC Dec 28 '11 at 19:14