Process data submitted by Form

Source: Internet
Author: User

Source: LoveJSP. site
In Web programming, processing data submitted by forms is the primary way to obtain Web data. Today, let's take a look at how Servlet processes data from forms.

The form data submission methods include the Post method and the Get method. When the Post method is used, the data is read by the standard input device. When the Get method is used, the data is passed to the form data processing program by the CGI variable QUERY_STRING.

The Servlet automatically processes the data obtained by the preceding two methods, so that you can obtain the value of the variable by simply calling the getParameter method of HttpServletRequest and giving the variable name. Note that the variable name is case sensitive. For the data submitted by the Post or Get methods, the Servlet processing method is the same. If the requested variable does not exist, an empty string is returned. If the variable has multiple values, you should call getParameterValues. This method will return a string array. You can use getParameterNames to obtain the names of all variables. This method returns an Emumeration method.

Let's take a simple example. The Servlet below reads the values of the five fields with the specified name in the form. Download this example

// Html file ----> postdata.htm download this file
<Html>
<Head>
<Title> getFormData Servlet Example form LoveJSP.com </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>

<Body bgcolor = "# FFFFFF">
<H1 align = "center"> <I> <B> Demo Page </B> </I> <Form action = "/try/servlet/lovejsp. getFormData">
<P> </p>
<Table width = "41%" border = "2" align = "center">
<Tr bgcolor = "# 6633CC" align = "center">
<Td colspan = "2" align = "center"> <font color = white> getFormData Servlet Demo
Page </font> </td>
</Tr>
<Tr bgcolor = "# FFFFCC">
<Td align = "center" width = "43%">
<Div align = "right"> username: </div>
</Td>
& Lt; td width = "57%" & gt;
<Div align = "left">
<Input type = "text" name = "username">
</Div>
</Td>
</Tr>
<Tr bgcolor = "# CCFF99">
<Td align = "center" width = "43%">
<Div align = "right"> password: </div>
</Td>
& Lt; td width = "57%" & gt;
<Div align = "left">
<Input type = "password" name = "password">
</Div>
</Td>
</Tr>
<Tr bgcolor = "# FFFFCC">
<Td align = "center" width = "43%">
<Div align = "right"> Email: </div>
</Td>
& Lt; td width = "57%" & gt;
<Div align = "left">
<Input type = "text" name = "email">
</Div>
</Td>
</Tr>
<Tr bgcolor = "# CCFF99">
<Td align = "center" width = "43%">
<Div align = "right"> Homepage: </div>
</Td>
& Lt; td width = "57%" & gt;
<Div align = "left">
<Input type = "text" name = "Homepage">
</Div>
</Td>
</Tr>
</Table>
<P align = "center">
<Input type = "reset" name = "Reset" value = "clear">
<Input type = "submit" name = "Submit2" value = "Lets Go">
</P>
</Form>
</Body>
</Html>

// Servlet File getFormData. java download this File

Package lovejsp;

Import java. io .*;
Import javax. servlet .*;
Import javax. servlet. http .*;

/** Servlet getParameter Ex from Lovejsp. site (http://www.lovejsp.com)
*/

Public class getFormData extends HttpServlet {
Public void doGet (HttpServletRequest request,
HttpServletResponse response)
Throws ServletException, IOException {
Response. setContentType ("text/html ");
PrintWriter out = response. getWriter ();
String title = "reading form data ";
Out. println (LovejspTools. headTitle (title) + // a tools method to show the html code with title
"<Body bgcolor =" # FDF5E6 ">" +
"<H1 ALIGN = CENTER>" + title + "</H1>" +
"<UL>" +
"<LI> <B> username </B> :"
+ Request. getParameter ("username") + "" +
"<LI> <B> password </B> :"
+ Request. getParameter ("password") + "" +
"<LI> <B> Email </B> :"
+ Request. getParameter ("Email") + "" +
"<LI> <B> Homepage </B> :"
+ Request. getParameter ("Homepage") + "" +
"</UL>" +
"</BODY> </HTML> ");
}
}


The running result of this program is shown in:

You may have noticed that the Email value in the above result is null because this field is not in the form and the form email name is email.

The following Program is an example of getParameterNames and a good tool for debugging programs. Its function is to display all form data.

// Servlet File ShowAllFormData. java download this File

Package lovejsp;

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

Public class ShowAllFormData extends HttpServlet {
Public void doGet (HttpServletRequest request,
HttpServletResponse response)
Throws ServletException, IOException {
Response. setContentType ("text/html ");
PrintWriter out = response. getWriter ();
String title = "show all FZ records? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcm2x5MG/tcTWtQ = ";
Out. println ("<HTML> <HEAD> <TITLE>" + title + "</TITLE> </HEAD>" +
"<Body bgcolor =" # FDF5E6 ">" +
"<H1 ALIGN = CENTER>" + title + "</H1>" +
"<Table border = 1 ALIGN = CENTER>" +
"<Tr bgcolor =" # FFAD00 ">" +
"<TH> variable name <TH> variable value ");
Enumeration paramNames = request. getParameterNames ();
While (paramNames. hasMoreElements ()){
String paramName = (String) paramNames. nextElement ();
Out. println ("<TR> <TD>" + paramName + "<TD> ");
String [] paramValues = request. getParameterValues (paramName );
If (paramValues. length = 1 ){
String paramValue = paramValues [0];
If (paramValue. length () = 0)
Out. print ("<I> No Value </I> ");
Else
Out. print (paramValue );
} Else {
Out. println ("<UL> ");
For (int I = 0; I <paramValues. length; I ++ ){
Out. println ("<LI>" + paramValues [I]);
}
Out. println ("</UL> ");
}
}
Out. println ("</TABLE> </BODY> </HTML> ");
}

Public void doPost (HttpServletRequest request,
HttpServletResponse response)
Throws ServletException, IOException {
DoGet (request, response );
}
}

Running result:


Okay, thats all for today, thank you all. see you next time


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.