Use JSP to process forms in the format of enctype = "multipart/form"

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.