Good day to all! The question is: There is a JSP scale that contains a form: several text fields and one field for the image.

1. <form method="post" action="StepTwo" enctype="multipart/form-data">

When I pull values ​​from the servlet through request.getParameter then for some reason I always get null . But the photo is pulled out as it should. Servlet Listing:

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); String contentType = request.getContentType(); response.setContentType("text/html;charset=Windows-1251"); request.setCharacterEncoding("Cp1251"); String email=(String) request.getParameter("email"); out.print(email); String about=(String) request.getParameter("about"); String day=(String)request.getParameter("day"); String month=(String)request.getParameter("month"); String year=request.getParameter("year"); HttpSession session = request.getSession(true); session.setAttribute("email",email); session.setAttribute("about",about); session.setAttribute("birthday",year); //------------------------------------ String saveFile=""; contentType = request.getContentType(); if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){ DataInputStream in = new DataInputStream(request.getInputStream()); int formDataLength = request.getContentLength(); byte dataBytes[] = new byte[formDataLength]; int byteRead = 0; int totalBytesRead = 0; while(totalBytesRead < formDataLength){ byteRead = in.read(dataBytes, totalBytesRead,formDataLength); totalBytesRead += byteRead; } String file = new String(dataBytes); saveFile = file.substring(file.indexOf("filename=\"") + 10); saveFile = saveFile.substring(0, saveFile.indexOf("\n")); saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\"")); int lastIndex = contentType.lastIndexOf("="); String boundary = contentType.substring(lastIndex + 1,contentType.length()); int pos; pos = file.indexOf("filename=\""); pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; int boundaryLocation = file.indexOf(boundary, pos) - 4; int startPos = ((file.substring(0, pos)).getBytes()).length; int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length; File ff = new File(saveFile); FileOutputStream fileOut = new FileOutputStream(ff); fileOut.write(dataBytes, startPos, (endPos - startPos)); fileOut.flush(); fileOut.close(); session.setAttribute("photo", dataBytes); //---------------------------------- } } 

Well, actually, and the question! Why other values ​​are not pulled out and how to pull them out?

one.

 form method="post" action="StepTwo" enctype="multipart/form-data;" Email: input type="email" name="email" value="<% String email=(String)session.getAttribute("email");if (email!=null){out.print(email);}; %>" </br> About You: input type="text" name="about" value="<% String about=(String)session.getAttribute("about");if (about!=null){out.print(about);}; %>"</br> input type="file" name="foto"</br> input type="submit" value="Шаг 3" /form 

2

 <servlet> <servlet-name>StepTwo</servlet-name> <servlet-class>ServletStepAndSave.StepTwo</servlet-class> </servlet> <servlet-mapping> <servlet-name>StepTwo</servlet-name> <url-pattern>/StepTwo</url-pattern> </servlet-mapping> 

3.When the servlet runs, it displays null (out.print (email))

  • one
    You have left a lot of unnecessary code here, to put it mildly, it has no interest here. Better show the form code on the jsp page, the servlet mapping in web.xml and the output that you have shows String email = request.getParameter ("email"); // I don’t understand your type conversion to the string of a method that so returns String System.out.println (email); // if NullPointerException falls out here - we will understand, no - it means problems in your logic and subsequent actions - Viacheslav
  • Added the necessary information in the body of the question. - Avtostopom_do_Raya
  • Well, until I ran away, a couple of tips - never use java-code on the jsp page, for this there are jsp tags and jstl tags. Second - never, never call packages with a capital letter (I already mentioned this). If an object is stored in a session, then java-code is not needed here, it is visible on the page. How to get free - throw an example of how it should look. For now, if you have time - read about jsp, its tags and jstl - Viacheslav
  • Viacheslav, why can't you use the package names with a capital letter? What are the side effects of this? I usually always write with a little, but I got on a project where with a big one. What is the difference? (Alas, I could not find it on HashCode) - Anton Feoktistov 6:42 pm
  • System.out.println (email); Why would NullPointerException fall out here? null is passed to the method, and no method is called on null. - Sasha121

3 answers 3

Try instead

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

call

 String month = request.getHeader("month"); 

or

 String month = request.getAttribute("month"); 

I can not say for sure, but perhaps one of them. You can also check the list of names of all parameters, whether there is something that interests you or not (for attributes and headers, the same):

 Enumeration params = request.getParameterNames(); 
  • Most likely the 3rd option should come up =) - Kobayashi_Maru

Everything turned out to be a little more complicated. The problem was that if we pass a file and text through a form, then due to enctype="multipart/form-data" we cannot get to text fields through request . Then FileUpload comes to the rescue.

 String email=""; String about=""; String day=""; String month=""; String year=""; int count1=0,count2=0,count3=0,count4=0,count5=0; boolean isMultipart = ServletFileUpload.isMultipartContent(request); PrintWriter out = response.getWriter(); if (!isMultipart) { } else { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List items = null; try { items = upload.parseRequest(request); } catch (FileUploadException e) { e.printStackTrace(); } Iterator itr = items.iterator(); while (itr.hasNext()) { FileItem item = (FileItem) itr.next(); if (item.isFormField()) { String name = item.getFieldName(); String value = item.getString(); if(name.equals("email")) { email=value; } if(name.equals("about")) { about=value; } if(name.equals("day")) { day=value; } if(name.equals("month")) { month=value; } if(name.equals("year")) { year=value; } } else { try { String itemName = item.getName(); } } } } 

PS Thank you all for the feedback! Viacheslav, and the truth, why it is impossible to name packages with a capital letter?

  • one
    @ Avtostopom_do_Raya, it's good that they themselves figured it out, I thought the error was different. The fact is that there is a so-called code convention in java, subject to which, developers unfamiliar with each other can more easily understand someone else's code. The project can be respected its code convention, as in the case of @ Anton Feoktistov. Personally, I see the ServletStepAndSave.StepTwo entry in the code - I think about the nested class, although I understand that this is unlikely in this case (it may and may need to be checked). - Viacheslav

When assigning a value, you need to write not <%...%> , but <%=...%> .