Java Development Study Note 6: servlet-process customer form data

Source: Internet
Author: User

This instance passes the test under tomcat.
The user enters the basic information on the HTML page. This html basically uses all form elements. When you click "Submit", the request is submitted to the servlet, servlet returns user input information to the customer in a table.

1. customer terminal html(formparam.html)
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = GBK"/>
<Title>
Information Input
</Title>
</Head>

<Body bgcolor = "# ffffff">
<P align = "center"> <B> <font color = "#009999" size = "+ 2"> employee information form <br> </font>
<Font color = "#000000" size = "+ 1"> <br> </font> </B> </P>
<P> This example shows how to use servlet parameters: </P>
<Form action = "formparam" method = "Post"> last name: <input size = "20" type = "text" name = "last name"> <br> Name:
<Input size = "20" type = "text" name = "name"> <br>
<Input type = "checkbox" name = "know Java"> familiar with Java <br>
<Input type = "checkbox" name = "know C ++"> familiar with C ++ <br>
<Input type = "radio" name = "working" value = "woking"> working <input type = "radio" name = "working" value = "unworking"> not working <br> type of work <br>
<Select size = "3" name = "job">
<Option value = "engineer"> engineer </option>
<Option value = "student"> Student </option>
<Option value = "farmer"> farmer </option>
</SELECT> <br> qualifications <br>
<Select size = "3" name = "jobrank">
<Option value = "low"> low </option>
<Option value = "intermediate"> intermediate </option>
<Option value = "advanced"> advanced </option>
</SELECT> <br>
<Textarea rows = "8" Cols = "50" name = "Remarks"> enter other information here </textarea>
<Br>
<Input type = "Submit" name = "Submit" value = "Submit"> <input type = "reset" name = "reset" value = "reset">
</Form> </body>

Ii. servlet (formparam. Java)
Package fey. servlets;

Import javax. servlet .*;
Import javax. servlet. http .*;
Import java. Io .*;
Import java. util .*;

Public class formparam extends httpservlet {
Public void dopost (httpservletrequest req, httpservletresponse resp)
Throws servletexception, ioexception
{
Printwriter out;
String [] paramvalues;
String Param, value;
Enumeration paramnames;
Boolean first;
Int I = 0;
Resp. setcontenttype ("text/html; charset =/" GBK /"");
Out = resp. getwriter ();
Out. println ("<HTML> <body> Out. println ("<Table border = 1> <tr> <TH> name </Th> <TH> value </Th> </tr> ");
Req. setcharacterencoding ("gb2312 ");
Paramnames = Req. getparameternames ();
While (paramnames. hasmoreelements ()){
Param = (string) paramnames. nextelement ();
Paramvalues = Req. getparametervalues (PARAM );
First = true;
Value = "";
For (I = 0; I <paramvalues. length; I ++)
{
If (first) {value = value + paramvalues [I]; first = false ;}
Else value = value + "," + paramvalues [I];
}
Value = Req. getparameter (PARAM );
Out. println ("<tr> <TD>" + Param + "</TD> <TD>" + value + "</TD> </tr> ");
}
Out. println ("</table> ");
Out. println ("<br> <form action =/" formparam.html/"> <input type =/" Submit/"name =/" return/"value =/" return/"> </form> </body> Out. Flush ();

}
Public void doget (httpservletrequest req, httpservletresponse resp)
Throws servletexception, ioexception
{
Dopost (req, resp );
}
}

Iii. Web. xml
<? XML version = "1.0" encoding = "ISO-8859-1"?>
<! Doctype web-app
Public "-// Sun Microsystems, Inc. // DTD web application 2.3 // en"
Http://java.sun.com/dtd/web-app_2_3.dtd>
<Web-app>
<Display-Name> Fey's servlet </display-Name>
<Description> Fey's servlet </description>

<Servlet>
<Servlet-Name> formparam </servlet-Name>
<Servlet-class> fey. servlets. formparam </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> formparam </servlet-Name>
<URL-pattern>/formparam </url-pattern>
</Servlet-mapping>

</Web-app>

4. Questions about Chinese display
During the two tests on servlet, a problem occurs: If the servlet response character data contains Chinese characters, it will always be garbled in the client browser. After debugging, the following points are summarized:
1. If the request data sent by the form from the client browser to the Web server contains Chinese characters, set the method attribute of form to post;
2. Before obtaining the getwriter object of servletresponse to write a response, set servletresponse. setcontenttype ("text/html; charset =/" GBK /"")
3. Set servletrequest. setcharacterencoding ("GBK") or servletrequest. setcharacterencoding ("GBK") or servletrequest. setcharacterencoding ("gb2312 ")
 

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.