1.Servlet
Servlet Overview: A small Java program running on a Web server to receive and respond to requests sent from the client, typically using the HTTP protocol. The servlet is a dynamic web development technology provided by Sun.
Version 2.5: Profile-based development (web. xml)
Version 3.0: annotation-based development
The role of the servlet:
It will be based on the HTTP protocol, on the server side to receive requests from the client, and then the corresponding business logic processing, after processing, will return a response to the client
2. Use the servlet:
(1). Write a class
1. Implementing the Servlet interface
2. Inheriting the Genericservlet class
3. Inheriting the HttpServlet class
Actual development use
In general, the servlet is implemented by inheriting the HttpServlet class.
(2). Configure this class to be written to the server.
Configure Web. xml
Note: The value of Url-pattern can not be duplicated, otherwise Tomcat fails to start
3. Use ServletRequest to receive parameters:
* String GetParameter (string name); ------is used to receive data with a name that corresponds to a value.
* string[] Getparametervalues (String name); ------is used to receive data with a name that corresponds to multiple values.
* Map Getparametermap ();------used to receive all the data in the form, the map key is the parameter name submitted by the form, and the value of map is the values of the commit parameter.
* Enumeration Getparameternames ()-------used to get the names of all the parameters submitted in the form.
4.servlet Execution Process:
1. Go to the Web. xml file in the project to find the matching Url-pattern value according to the identity of the browser's servlet access
2. Go to the servlet-name tag in servlet-mapping to get the value
3. Go to the servlet-name tag in the servlet tag to find the matching value
4. Go to Servlet-class to find the path to the servlet and let the corresponding servlet run in a reflective manner
life cycle of 5.Servlet
When the servlet was created and when it was destroyed
When the client makes a request to the server, the server will first determine if the corresponding servlet has been initialized, and if the servlet is not initialized, then Init () will be executed first to initialize the servlet, then run service (), If the servlet is already initialized, run the service directly () and execute the Destory () destroy servlet when the server shuts down
Init ()
will only be run once, when the servlet is initialized
Service ()
Every time the servlet is manipulated, it will be executed.
Destory ()
Executes only once, destroying the servlet
6. Extended Tags: <load-on-startup>
Used to control when the servlet is initialized----written in the <servlet></servlet> tag
If the tag is not set or the value is negative, the servlet is initialized only when it is accessed, and if the value of the tag is positive, the servlet is initialized when the server starts up.
Note: The value must be an integer, when a positive integer, the value must be greater than 1, the smaller the value the higher the priority
7. HTTP protocol Hypertext Transfer Protocol
Definition: Hypertext Transfer Protocol (Http,hypertext Transfer Protocol) is one of the most widely used network protocols on the Internet. All WWW documents must comply with this standard. HTTP was originally designed to provide a way to publish and receive HTML pages
Function: Specifies the format of data interaction between the browser and the server.
(1) Features of the HTTP protocol:
Request-and response-based models
* Must have a response before request.
* Requests and responses must appear in pairs.
The default port number is 80.
(2) The request part of the HTTP protocol
Client:(Browser, cell phone, tablet) make a request, send to server side
Request Line:
Request mode: The difference between get and post
* Get the submitted parameters are displayed on the address bar, and post is not displayed.
* Get is often limited by size, while post has no size limit.
* Get has no request body, while Post has request body.
* Submission Path:
* Protocol version:
Request Header:are displayed in the form of key-value pairs. Typically a key corresponds to a value, and there is an individual key that corresponds to more than one value.
User-agent: Represents the type of browser. ---file download: Download the Chinese file: IE uses Urlencodor to encode, while Firefox uses BASE64 encoding.
Referer: Represents the source of the Web page. ---anti-theft chain.
If-modified-since: Typically used with header last-modified in response to find local cache
Request Body:
encapsulates the submission parameters of the Post submission method
(3) The response part of the HTTP protocol
server: sends a response to the client
Response Line:
* Protocol version
* Status Code:
* 200: Success
* 302: Redirect
* 304: Find local cache
* 404: Resource does not exist
* 500: Server Internal Error
* Status Code description
response Header: key value pair, usually a key corresponding to a value, there is a key corresponding to more than one value
* Last-modified: Use with if-modified-since in the request to find the local cache.
* Content-dispostion: The use of a header information for file download.
* Location: Redirect The path of the jump.
* Refresh: Timed update/timed jump.
The response header is used to modify the response body
SetHeader ("", ""): A key corresponds to a value
AddHeader (): One key corresponds to more than one value
Response Body:
Display the contents of a browser's page
Used to set information that needs to be returned to the client
Getwriter (). Write ()
Getoutputstream (). Write ()
Questions to note about setting the response body:
1. Use character stream must appear Chinese garbled characters
2. Character stream and byte stream can not appear in a servlet at the same time, otherwise 500
3. Character stream can not directly output the number, if the output number, will follow the ASCII table transcoding.
Getwriter (). write (20);
Resolution: Getwriter (). Write (20+ "");
Java Review Web Chapter--servlet