[Servlet] differences between Get and Post and examples, servletget

Source: Internet
Author: User
Tags html header

[Servlet] differences between Get and Post and examples, servletget

Original works of Lin bingwen Evankaka. Reprinted please indicate the source http://blog.csdn.net/evankaka

I. directory structure of J2EE WEB Application Files

Java Web applications are composed of a group of Static HTML pages, Servlets, JSP, and other related classes, which constitute a large project together. Each component has a fixed storage directory in the Web application. The configuration information of the Web application is stored in the web. xml file. When publishing certain components (such as servlets), you must add the corresponding configuration information to the web. xml file.
Java Web applications must use a standard directory structure

1. the root directory of the application, which can be any name. All HTML and JSP files are stored in this directory.
1.1 WEB-INF Directory: Required directory
1.1.1 web. xml: description file of Web application deployment, required file
1.1.2 classes directory:
1) used to store a single *. classes bytecode file. Servlet files are also stored in this directory.
1.1.3 lib directory:
1) store the third-party class library file, that is, the packaged JAR File
1.1.4 TLD file: Tag library description file
1.2 Other static files:
1.2.1 HTML
1.2.2 CSS
1.2.3 JavaScript
1.2.4 images, etc.
1.3 *. jsp: stores any number of JSP pages




In many cases, some information needs to be transmitted, from the browser to the Web server, and finally to the background program. The browser can transmit the information to the Web server in two ways: GET method and POST method.

Ii. GET Method

The GET method sends the encoded user information to the page request. Between the page and the encoded information? Characters are separated as follows:

http://www.test.com/hello?key1=value1&key2=value2

The GET method is the default method for passing information from the browser to the Web server. It generates a long string and appears in the address bar of the browser. If you want to pass a password or other sensitive information to the server, do not use the GET method. The GET method has a size limit: The request string can contain a maximum of 1024 characters.

This information is transmitted using the QUERY_STRING header and can be accessed using the QUERY_STRING environment variable. Servlet usesDoGet ()Method to process this type of request.

Iii. POST Method

Another reliable method for transmitting information to the background program is the POST method. The POST method packages the information in the same way as the GET method, but the POST method does not use the information as the URL? The text string after the character is sent, but the information is used as a separate message. Messages are delivered to the background program in the form of standard output. You can parse and use these standard outputs. Servlet uses the doPost () method to process such requests.

4. Use Servlet to read form data

Servlet processes form data, which is automatically parsed according to different situations:

  • GetParameter ():You can call the request. getParameter () method to obtain the value of the form parameter.
  • GetParameterValues ():If the parameter appears more than once, call this method and return multiple values, such as check boxes.
  • GetParameterNames ():This method is called if you want to obtain a complete list of all parameters in the current request.
V. Differences

First, when Post transmits data, it does not need to be displayed in the URL, but the Get method must be displayed in the URL.
Second, the size of Post data transmission can reach 2 MB, while the Get method can only transmit about 1024 bytes due to the URL length restriction.
Again: Post is used to transmit data to the server segment, and Get is used to retrieve data from the server segment. the reason why Get can also transmit data is to design and tell the server what data you actually need. the Post information is used as the content of the http request, while Get is transmitted in the Http header.

6. Differences between GET and POST in Servlet

Get and post are two methods of the http protocol, including head and delete. There is an essential difference between the two methods. get has only one stream. After a parameter is attached to a url, the size and quantity of parameters are strictly limited and can only be strings. The post parameter is transmitted through another stream, not through the url, so it can be large or pass binary data, such as file upload.

In servlet development, the get and post methods are processed by doGet () and doPost () respectively. There is also a doService (), which is a scheduling method. When a request occurs, it first executes doService (), whether it is get or post. The base class HttpServlet implements an angle. First, it determines whether the request is get or post. If it is get, it calls doGet (). If it is post, it calls doPost (). You can also directly overload the doService () method, so that you can get or post. Will execute this method.

What are the essential differences between GET and POST?
Use GET and form to encode the data in the url, and use POST form to transmit the data in the http header. In terms of use, GET is used only when the request idempotence (literally, the request returns the same result at any time, essentially the request itself does not change the server data and status, when the request changes the server data or status (update data, upload files), you should use POST.
What is the difference between GET and POST?
When you repeatedly access the page requested by the GET method, the browser uses the cache to process subsequent requests. When using the form submission method of POST, the browser will generate a permanent change assumption based on POST, and the user will be asked to submit for confirmation. When the compiled personnel correctly use GET and POST, the browser will provide good cache cooperation, and the response speed is faster.
Differences in form submission
The first step of form submission is to create a dataset and encode the dataset according to ENCTYPE. ENCTYPE has two values: multipart/form-data, application/x-www-form-urlencoded (default value). The former can be used for both GET and POST, and the latter only for POST. Then perform data transmission-for the GET method, the dataset is encoded using content type application/x-www-form-urlencoded and attached to the url. In this mode, the data is strictly limited to ASCII code; for POST, use the content type encoding Character Set and construct it into message sending.
Differences in server processing
In principle, there are no separate GET and POST requests. However, because data is encoded using different methods, different decoding mechanisms are required. Therefore, method changes will lead to code changes for processing requests. For example, for cgi, parameters are obtained through environment variables during GET processing, and data is obtained through standard input (stdin) When POST requests are processed.

From the usage experience, we will summarize the following:
1. get adds the parameter data queue to the URL referred to by the ACTION attribute of the submission form. The values correspond to each field in the form one by one and can be seen in the URL. Post uses the HTTP post mechanism to place fields in the form and their content in the html header and send them to the URL address referred to by the ACTION attribute. You cannot see this process.
2. For the get method, the server uses Request. QueryString to obtain the value of the variable. For the post method, the server uses Request. Form to obtain the submitted data. You can use Request to obtain parameters in either of the two methods.
3. The data volume transmitted by get is small and cannot exceed 2 kb. The amount of data transmitted by post is large, which is generally not restricted by default. Theoretically, the maximum size of IIS4 is 80 KB, and that of IIS5 is 100KB.
4. Low get security and high post security.
5. <form method = "get" action = "a. asp? B = B "> with <form method =" get "action =". asp "> is the same. That is to say, the parameter list behind the action page is ignored, while <form method =" post "action =". asp? B = B "> unlike <form method =" post "action =" a. asp ">.

VII. Examples

Create a web project in eclipse, name it casually, then add web. xml index. jsp, and servlet. The structure is as follows:


The GetServlet. java code is as follows:

DoGet

Protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response. setContentType ("text/html; charset = UTF-8"); PrintWriter out = response. getWriter (); String name1 = request. getParameter ("name1"); String price1 = request. getParameter ("price1"); out. println ("<HTML>"); out. println ("<HEAD> <TITLE> A Servlet </TITLE> </HEAD>"); out. println ("<BODY>"); out. print ("This is"); out. print (this. getClass (); out. println (", using the POST method"); out. println ("<br> </br>"); out. println ("name:" + name1); out. println ("<br> </br>"); out. println ("Price:" + price1); out. println ("</BODY>"); out. println ("</HTML>"); out. flush (); out. close ();}
DoPost

Protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response. setContentType ("text/html; charset = UTF-8"); PrintWriter out = response. getWriter (); String name2 = request. getParameter ("name2"); String price2 = request. getParameter ("price2"); out. println ("<HTML>"); out. println ("<HEAD> <TITLE> A Servlet </TITLE> </HEAD>"); out. println ("<BODY>"); out. print ("This is"); out. print (this. getClass (); out. println (", using the POST method"); out. println ("<br> </br>"); out. println ("name:" + name2); out. println ("<br> </br>"); out. println ("Price:" + price2); out. println ("</BODY>"); out. println ("</HTML>"); out. flush (); out. close ();}

Index. jsp:

<% @ Page language = "java" contentType = "text/html" pageEncoding = "UTF-8" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 

Web. xml:

<? Xml version = "1.0" encoding = "UTF-8"?> <Web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <! -- Configure the initial page --> <welcome-file-list> <welcome-file> index.html </welcome-file> <welcome-file> index.htm </welcome-file> <welcome-file> index. jsp </welcome-file> </welcome-file-list> <servlet> <! -- Class name --> <servlet-name> GetServlet </servlet-name> <! -- Package --> <servlet-class> com. mucfc. chapter0.GetServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name> GetServlet </servlet-name> <! -- Access url --> <url-pattern>/servlet/GetServlet </url-pattern> </servlet-mapping> </web-app>
Then it runs:

Submit with get

Submit with post


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.