) Basic JSP Essence

Source: Internet
Author: User
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

Related Article

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.