The following is written by a senior guy named [bird] On csdn.
Servlet three elements:
1. must inherit from httpservlet
2. doget () or dopost () must be implemented ()
3. servlet must be configured in Web. xml <servlet>
<Servlet-Name> </servlet-Name>
<Servlet-class> </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> </servlet-Name>
<URL-pattern> </url-pattern>
</Servelt-mapping>
Httpserveltrrequest: request object
Getparameter (): Get the value of the form Element
Getattribute (): Get the attribute value in the request range
Setattribute (): Set the attribute value in the reqeust range
Setcharacterencoding (): sets the character encoding.
Httpserletresponse: Object
Sendredirect (): external jump
Getwriter (): Get the output stream object
Setcontenttype ("text/html; charset = UTF-8"): sets the content format and encoding.
Four session tracking methods:
1. Session
Httpsession session = request. getsession ();
Session. setattribute ("name", "zhangsan ");
Session. setattribute ("PWD", "AAA ");
String name = (string) Session. getattribute ("name ");
2. COOKIE:
// Create a cookie
Cookie = new cookie ("name", "zhangsan ");
// Set the cookie timeout time
Cookie. setmaxage (24*60*60*60 );
// Send the cookie to the client
Response. addcookie (cookie );
// Obtain the cookie sent by the client
Cookie [] cookies = request. getcookies ();
For (INT I = 0; I <cookies. length; I ++ ){
Cookie temp = Cookies [I];
String key = temp. getname ();
String value = temp. getvalue ();
}
3. Hide form fields
<Input type = "hidden" name = "name" value = "zhangsan"/>
Request. getparameter ("name ");
4. URL rewriting
Question mark parameter passing
Loginservlet? Username = zhangsan & PWDs = 123
String name = request. getparameter ("username ");
String Pwd = request. getpareameter ("PWD ");
Internal jump:
Loginservlet
Request. getrequestdispatcher ("index. jsp"). Forward (request, resposne );
External jump:
Response. sendredirect ("index. jsp ");
Internal redirection is a request and a response
External redirects are two requests and two responses.
Servletcontext: servlet context object
It is a public region that can be shared by all clients.
Setattribute (): Put data into the public area
Getattribute (): Get data from the Public Region