Recently, a JSP website needs to receive data from the instrument. The data is transmitted using the POST method, but the format is enctype = "multipart/form" and cannot be obtained using the Servlet request, it has brought some trouble. After a simple research, we found it was not very difficult to communicate with you.
Webpage code:
<Form action = "login" method = "Post" enctype = "multipart/form-Data"> User Name: <input type = "text" name = "username"> </input> password: <input type = "password" name = "password"> </input> <input type = "Submit" name = "Submit" value = "Submit"/> </form>
As shown above, it is actually used to transmit two strings. But if we want to use this method, we have to find a way to take it out, isn't it ?~
After enctype = "multipart/form-Data" is set, in the servlet, except for the file type form, the values obtained by other values through the request. getparameter () method are null.
Solution:
Use the jspsmartupload component for upload.
First go to http://t.cn/ztqz28qto download a jspsmartupload.jarfile (you have installed Tomcat and eclipse by default), put this file under the Tomcat lib folder, and right-click the project required in eclipse and choose -- Build
Path -- add external jar -- select this jar
Open the. Java file to be processed. The code we used previously could not get the value is:
String usrn=request.getParameter("username");String pasw=request.getParameter("password");
Now rewrite it:
Smartupload su = new smartupload (); Su. initialize (getservletconfig (), request, response); Su. setmaxfilesize (2000000); Su. upload (); // note that this sentence may prompt you to add try-catchstring usrn = Su. getrequest (). getparameter ("username"); string pasw = Su. getrequest (). getparameter ("password ");
Now try to output the data to get the passed value ~
Reference: http://liyanblog.cn/articles/2012/10/23/1350962399952.html
Http://baike.baidu.com.cn/view/2295302.htm
Reprinted please indicate from icyfox_bupt: http://blog.csdn.net/icyfox_bupt/article/details/8901222