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))