Java-Servlet,

Source: Internet
Author: User

Java-Servlet,

1. What is Servlet?
A component specification developed by sun to expand the functions of web servers.
(1) used to expand web server functions
Early web servers (such as apache http server and iis) can only process requests for static resources (that is, they must be written in advance and stored on hard disks, such as html files and images ), dynamic resource requests cannot be processed (calculation is required, and corresponding html is generated). Therefore, the function must be extended.
CGI programs were used for early extension.
CGI (understanding ):
Common Gateway Interface (Common Gateway Interface), which can be used
C/c ++, perl and other languages to develop CGI programs. Poor portability and Performance
It is not very good, so it is rarely used now.
(2) component specifications
1) What is a component?
Comply with the specifications, implement some functions, and must be deployed in the container before
The software module that runs.
Servlet is a component that must be deployed in the Servlet container to run.
2) What is a container?
Programs that provide the runtime environment for components.
For example, Tomcat is a Servlet container.
2. How to Write a Servlet?
Step1. write a java class to implement the Servlet interface or inherit
HttpServlet class.
Step2. compile.
Step.
Create a folder with the following structure:

Appname (Application name, which can be customized)
WEB-INF
Classes (put the. class file)
Lib (optional, put the. jar file)
Web. xml (deployment description file)

Step 4. Deployment
Copy the folder created in step 3 to a location specified by the container.
Note: You can use the jar command to compress the folder created in step 3.
(Compress the file into a. war file with the suffix) and then copy the file.
Step 5. Start the container and access the Servlet
Http: // ip: port/appname/url-pattern
Note: url-pattern is set in web. xml.
3. How does the Servlet run?
For example, enter http: // ip: port/appname/hello in the address bar of the browser.
Step1. the browser establishes a connection with the server based on the ip address and port.
Step 2: the browser will package the request data (that is, according to the http protocol requirements)
Request Packet), and then send the packet to the server.
Step. Unpack the server (that is, parse the data in the Request Packet according to the http protocol ),
Add the parsed data to the request object, and create a response
Object.
Step 4. The server creates a servlet instance and then calls its service method.
Note: The server will pass in the request object and response object as parameters,
You can use the request object to obtain the request-related data (for example, obtain the request parameter value), and then write the processing result to the response object.
Step 5. The server extracts the data from the response object, packs the data (that is, creates a response packet according to the http protocol), and sends the data to the browser.
Step 6. Split the package in the browser and generate the corresponding page.
4. Common Errors
(1) 404
1) Meaning: The corresponding resource cannot be found based on the Request Path (
For example, the servlet cannot be found ).
Note: 404 is a status code (the status code is a three-digit number, which is defined by the http protocol,
Indicates a status in which the server processes the request ).
2) Error cause:
A. Application Deployment failed.
B. The Request Path is incorrect.
(2) 500
1) meaning: system error.
2) Error cause:
A. The description file is written incorrectly. For example, the class name is incorrect.
B. The source code is faulty.
(3) 405
1) meaning: the processing method cannot be found.
2) Error cause:
The service method does not comply with the specifications.
5. http protocol (learn more)
(1) What is http?
It is a network application layer protocol that specifies how browsers communicate with web servers.
And the corresponding data packet format.
(2) how to communicate?
Step 1. Establish a connection.
Step2. send request data packets.
Step3. send response data packets.
Step 4. Close the connection.
One request and one connection. If the browser wants to send a new request to the server
Create a new connection.
Advantage: the server can use limited connections to serve as many requests as possible.
(3) data packet format
1) Request data packets
A. Request Line (request type request resource path protocol type and Version)
B. Several Message Headers
(Understanding)
The message header is a key-Value Pair (separated ":").
Define its meaning. The browser and server can use the message header to transmit some
Specific Message. For example, a browser can send a "user-agent" Message Header
To tell the server the type and version of the browser.
C. entity content
Object content only has data when the request type is post.
2) response packet
A. Status line (protocol type and version status code status description)
Note: 200 normal
B. Several Message Headers
The server can also send some headers to browsers, such
Content-type message header, which tells the browser that the server returns
Data Type (MIME type ).
C. entity content
The result of the program processing will be parsed by the browser, and then the corresponding
Page.
(4) Request type
1) get request
A. Under what circumstances will the browser send a get request?
Enter an address in the browser address bar.
Click the link.
The default form submission method.
B. Features
The request parameters are added to the end of the Request resource path and can only be submitted.
A small amount of data is sent to the server.
Note: The request line can only store about 2 K of data.
The request parameters are displayed in the browser address bar, which is insecure.
Note: For example, the router records the request address (including the request parameters)
2) post request
A. Under what circumstances will the browser send a post request?
Set the method attribute of the form to "post ".
B. Features
Put the Request Parameters in the object content to submit a large amount of data.
To the server.
Request Parameters are not displayed in the browser address bar, which is relatively secure.
Note: Both post and get requests do not encrypt request parameters.

 

I am a beginner. If any updates are poor, I would like to thank you!

More highlights will be updated later!

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.