Servlet Notes

1. What is a servlet?
A component specification developed by sun company to extend the function of web server.
(1) Extending the web server function
The web server can usually only process requests for static resources (that is, prepare the html file in advance), and can be extended by using servlets (that is, the web server can process requests for dynamic resources by calling servlets, such as accessing database).
(2) Component specification
1) What is a component?
A software module that meets certain specifications, implements some functions, and needs to be deployed in the corresponding container to run. A servlet is a component that needs to be deployed to the corresponding servlet container to run.
Tomcat is a container that provides a running environment for servlets.
2. How to write a servlet?
Step1, write a java class that implements the servlet interface or inherits the HttpServlet abstract class.
Note: Inheritance is usually chosen.
step2, compile
step3, package and
create a folder with the following structure:
appname (application name)
WEB-INF (fixed immutable name)
classes (put class file)
lib (optional, put jar file)
web.xml (deployment description ) file)
step4, deployment
Copy the entire folder created in step3 to the location where the servlet container responds.
Note: You can use the jar command to compress the entire folder created in step 3 into a file with a ".war" suffix, and then copy it.
step5, start the container and access the servlet
http://ip:port/appname/url-pattern
http://localhost:8080/xxx/xxx
Note: url-pattern is defined in the web.xml file.
3. Tomcat installation (www.apche.org)
4. How does the servlet work? For example, enter: http://ip:port/web01/hello step1
in the browser address bar , the browser establishes a connection step2 according to the ip and port, the browser packages the relevant data (such as request parameters), and then sends the request. Step3, the container parses the request data packet, encapsulates the parsed data into the request object, and creates a response object at the same time. Step4, the container creates the servlet object, and then calls the servlet method of the object. Note: The container will pass the request and response as parameters, you can get the request parameters through the request, or you can write the processing result to the response. Step5, the container reads the processing result in the response, and then packages the processing result and sends it to the browser. Step6, the browser parses the response data packet and generates the corresponding page. 5. Frequently Asked Questions (1) 404 Note: 404 is a status code (it is a three-digit number, defined by W3C, indicating a state in which the server processes the request). 1) Meaning the server cannot find the corresponding resource according to your request address. 2) Reasons a. The application is not deployed b. <servlet-name> is inconsistent
















c. The request address is wrong and check the request address
according to http://ip:port/appname/url-pattern
.
(2) 500
1) Meaning: System error
2) Causes The
program has an error during running.
a. Does not inherit HTTPServlet
b. <servlet-class> is wrong.
(3) 405
meaning: No processing method found

 

 

HTTP protocol:
(1) What is HTTP protocol
is a network application layer protocol formulated by W3C, which specifies how to communicate and the format of corresponding data packets.
1) How to communicate
step1, establish a connection
step2, send a request
step3, send a response
step4, close the connection
2) Features
One request, one connection
, that is, if the browser needs to send a new request, it needs to establish a new connection. The advantage of this design is that the server can serve as many requests as possible with limited connections.
(2) The structure of the data packet
1) The
request line of the request data packet (the request method requests the resource path protocol type and version)
The message header
is some key-value pairs (separated by ":"), indicating specific meanings, such as browsers The "user-agent" header can be sent to tell the server the type and version of the browser.
Entity content
Only when the request method is post, there will be data (request parameters) in the entity content.
2) Response packet
status line (protocol type and version status code status description)
200 Correct
500 System error
404 Cannot find the corresponding resource
header The
server can also send some headers to the browser, for example, send a "content-type" message The header, tells the browser, the data type returned by the server.
The processing result of the entity content
program, the browser will parse the data and
generate the corresponding page.
(3) Two request methods
1) get request
a. In some cases, the browser will send a get request
to directly enter an address;
click the link address;
the form is submitted by default.
b. Features
will add request parameters to the back of the request resource path, and
only a small amount of data can be submitted.
The request parameters will be displayed in the browser address bar, which is not safe.
Note:
For example, the router will record the request address that contains the request parameters.
2) Post request
a. In some cases, the browser will send a post request to
set the form method="post"
b. The feature
will add request parameters to the entity content, which can submit
a large amount of data.
The request parameters will not be displayed in the browser address bar, which is relatively safe.
Note:
Request parameters are not encrypted.


servlet output Chinese
(1) Why is there garbled characters?
The out.println method will use "ISO-8859-1" to decode by default.
to code.
(2) How to solve
response.setContentType("text/html;charset=utf-8");


The form contains Chinese parameter values
​​(1) Why is there garbled
characters? When the form is submitted, the browser will encode the Chinese parameter values ​​in the form.
Note:
It will be encoded according to the character set of the page where the form is opened. By default, the server will use "ISO-8859-1" to decode.
(2) The solution
step1 is to ensure that when the form is submitted, it is encoded according to the specified character set.
Add in the HTML file:
<meta http-equiv="content-type" content="text/html;charset=utf-8"> 
step2, the server uses the corresponding character set to decode.
Method 1:
request.setCharacterEncoding("utf-8");
Note:
This method must be submitted in the form of post, which is only valid for post requests.
Method 2: 
First restore the byte array sent by the browser, and then use the correct character set to decode.
uname = new String
((uname.getBytes("iso-8859-1")),"utf-8");
Note:
Both get and post are applicable.
Read request parameter value
(1) String request.getParameter(String paramName);
Note:
a.paramName must be the same as the actual parameter name sent, if not, it will get null.
b.
(2) String[] request.getParameterValues(String paramName);
Note:
a. Use this method when there are multiple parameters with the same name.
b. For checkboxes and radios, if you don't select any option, you will get null.
To use JDBC to access the database
step1, you need to copy the JDBC driver (jar file) to WEB-INF\lib.
Step2, use a set of APIs provided by JDBC to access the database.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325279339&siteId=291194637