Web Summary-servlet, JSP, filter and listener, Four Scopes and nine built-in objects, El expression language, custom tags, jstl, internationalization and Chinese character encoding

Source: Internet
Author: User

1 Servlet
1. servlet and servletconfig: the former corresponds to a servlet class, and the latter corresponds to the configuration information in Web. xml.
(1) servlet:
Init (servletconfig): it is called only once after the class is loaded and instantiated.
Service (servletrequest, servletresponse): Will be called multiple times
Destroy (): only once
Getservletconfig ()
Getservletinfo ()
(2) servletcionfig
Getservletname ()
Getinitparameter (string): --- <servlet> <init-param> </servlet>
Getinitparameternames ():
Getservletcontext
Note that getinitparameter (string) is also available in servletcontext, which corresponds to the nested elements in <context-param>.
The <context-param> and <servlet> labels are at the same level, and the entire application is initialized.

2. two classes:
Genericservlet/httpservlet:
Genericservlet implements Servlet and servletconfig. httpservlet inherits from genericservlet and is related to HTTP.
(1) genericservlet: has a non-parameter init method to facilitate subclass coverage.
(2) httpservlet: There are two service methods and seven do methods. The do method is generally rewritten during rewriting.

3. servlet Configuration
It refers to the information in Web. XML,
<Servlet>
<Servlet-Name> </servlet-Name>
<Servlet-class> </servlet-class>
<Init-param> </init-param>
</Servlet>
// The configuration above corresponds to something in servletconfig.
<Servlet-mapping>
<Servlet-Name> </servlet-Name>
<URL-pattern> </url-pattern>
</Servlet-mapping>

4. Structure of Web Application folders
WEB-INF
|
| ------------ Web. xml
| ------------ JAR file in the Lib folder
| ------------ Classes Class File
| ------------ Tags tagfile Tag file
-- Static resources and JSP files

5. Requests and responses:
(1) Request servletrequest-> httpservletrequest
Getparameter (string ),
Getheader (),
Getparametervalues (string) for check boxes
Getparameternames,
Getremoteaddr to obtain the remote address
Getlocaladdr to get the local address
Getlocale to get the localization Information
Getsession/getcookies
Getrequestdispatcher

(2) Response to servletresponse-> httpservletresponse
Sendredirect
Senderror
Setheader/addheader/setintheader
Getwriter/getoutputstream: These two methods cannot be called at the same time !!!!!
Setcontenttype/setcharacterencoding

6. The difference between request forwarding and response redirection should be clarified.
(1) forwarding:
A. Three forwarding methods:
Requestdispatcher. Forward/include
<JSP: Include>
<JSP: Forward> is equivalent to requestdispatcher. Forward (); return;
That is to say, after using requestdispatcher. Forward (), the following code will still be executed, but after <JSP: Forward> is executed, the following code will not be executed.
Pagecontext. Forward/include
B. How to obtain requestdispatcher:
Request. getrequestdispatcher
Servletcontext. getrequestdispatcher

Request forwarding of requestdispatcher, including command element in JSP page, and getresourceasstream (string path) in servletcontext)
(Used to read the path Resources in the folder) can access the WEB-INF folder,
For example, to prevent users from accessing some protected pages (such as controller servlet), put them in the WEB-INF, can be accessed when the request is forwarded.
Sometimes you need to read some configuration Resources in the WEB-INF, you need the getresourceasstream method in servletcontext.
And some websites of each page has the same header image and the last image, these stuff can be placed in the WEB-INF folder with the include command of the page element to include in.

Note: If you use the requestdispatcher of servletcontext, to forward requests from application a to application B, you must set the crosscontext VALUE OF A to true!

(2) redirection
Response. sendredirect: for example, when you go to the next page after registration, you must use redirection to change the URL address of the browser!
Request forwarding cannot be out of the Tomcat server range. If you want to be out of the server, you can only respond to redirection. Redirect generates a temporary response (Response
Once the request is generated, the browser sends a request to find a new address instead of displaying anything after receiving the temporary response.

Ii. jsp
1. templates and elements:
For JSP, it cannot be executed. It must be translated into a servlet for execution. The elements that must be specially processed by the container are called,
The template is printed directly to the output stream.

Elements are divided into the following types:
1. Script Element
2 command element
3 Action Element

1. Script element:
(1) script snippet: it is stored in the service method after translation.
(2) script Declaration: put in the class, but outside the service method,
(3) script expression: Put it in out. Print, and the script expression cannot add points.

Built-in objects cannot be used in script declaration! Because they are used in the service method.

2 command element:
Page command:
Include command <% @ include file = "" %>. The file contained must be read in plain text format. The encoding problem occurs when the file is read. This is pageencoding.
.
Taglib command

3 page garbled: pageencoding/contenttype
The former sets the form of JSP page reading, and the latter sets a response header to tell the browser what encoding format to display
If pageencoding is not set but only contenttype is set, pageencoding will be affected by contenttype, and vice versa.
That is, if the two are set to only one, they will be displayed according to one encoding!

Both the request and the response are decoded by default by the iso-8859-1 when the request is submitted. Make sure that the encoding is consistent during read/write and display.

4 Action elements:
<JSP: usebean>: Mainly used to open a script variable and store an attribute in the scope.
<JSP: setproperty>
<JSP: getproperty>
<JSP: Forward>
<JSP: Include>
<JSP: param>
The first six are important!
<JSP: invoke>
<JSP: Dobody>
<JSP: plugin>
<JSP: fallback>
<JSP: Params>

3. Filters and listeners
1. filter/filterconfig
Init (filterconfig)
Dofilter (servletrequest, servletresponse, filterchain)
Destroy ()

<Filter>
<Filter-Name>
<Filter-class>
</Filter>

<Filter-mapping>
<Filter-Name>
<URL-pattern> | <servlet-Name>
</Filter-mapping>

Filters with the same URL-pattern are in a filter chain, and the execution order is in the Order in Web. xml.

2. listener-> servletcontext/httpsession/servletrequest
(1) Lifecycle
Servletcontextlistener: Initialize (read all discussion areas from BBS and store them in application scope or read all blocked IP addresses for storage) and destroy
Httpsessionlistener/httpsessionactivationlistener
Servletrequestlistener
(2) add, delete, and modify attributes
It is usually performed by attributelistener.
<Listener>
<Listener-class> package name. Class Name </listener-class>
</Listener>

4. Four Scopes and nine built-in objects
1. The page scope corresponds to pagecontext, not page
2. built-in objects:
Pagecontext/Request/session/Application
Out-> is a jspwriter instance, which has a cache, while printwriter does not cache. At the end of the page, the printwriter will be generated in response to write the cached content!
If the cache is full, it depends on: If <% @ page autoflush = "true" %>, it will be automatically refreshed. If it is false, an exception will be thrown! Response. getwriter ()
The returned result is printwriter, not jspwriter.
Exception-> iserrorpage = true
Session: Two tracking mechanisms: one is to store the session through the cookie on the client, and the other is to store the sessionid on the server,
Config-> servletconfig
Page-> Object-> This
Response:

5. El expression language:
$ {}
1. Constant, function, and variable expressions
2. A variable is an attribute value of the same name stored in a specific scope.
3. A function is a public and static method of referencing a class declared in the TLD file.
4. 11 built-in objects
(1) Scope: pagination/requestscope/sessionscope/applicationscope
(2) Communication with pages: pagecontext
(3) Request Parameters: Param/paramvalues
(4) header: Header/headervalues
(5) Cookies/initparam --- <context-param>
5. vertex operators and [] operators and empty
For JavaBean, The getter method is called,
For map, the get (object) method is called,
For the scope, getattribute (string) is called)
The getter method is also called for pagecontext to obtain all built-in objects.
For Param, the parameter is obtained.
The header is used to retrieve the header.
For initparam, get the initialization parameter
[] Operators are mainly used for arrays.

6. Custom tags
1. Tag interface (traditional Interface Series)

Jsptag
(1) Tag: dostarttag (Return Value: skip_body/eval_body_include)/doendtag (return skip_page or eval_page)
(2) iterationtag-> tagsupport
Doafterbody: eval_body_again/skip_body
(3) bodytag-> bodytagsupport
Doinitbody
Doafterbody: eval_body_buffered-> bodycontent: There is a pushbody in pagecontext to pack the body. In this case, we can use the getout method to obtain
Instead of jspwriter, It is bodycontent. Bodycontent is generated by calling the pushbody method, which involves another getenclosingwriter ().
It is a bodycontent method that can get the output stream jspwriter in it. However, note that if more than one pushbody is performed but no popbody is performed
The bodycontent is the bodycontent of Libao, but since bodycontent is a subclass of jspwriter, it also complies with the method declaration.

2. simpletag interface (simple interface Series)
Setjspcontext ()
Setparent ()
Setjspbody (jspfragment) Note that jspfragment is the label body and cannot contain script elements! When registering, either the content element is set to empty,
You can either set it to scriptless, that is, the TAG body of a simple tag cannot have script elements!
Dotag ()

Jspfragment. Invoke (null): Get it directly in the output stream
Jspfragment. Invoke (stringwriter out): gets the output stream to stringwriter, and then calls out. tostring to convert the body into a string.

3. Tag file
. Tag
It can be placed in/WEB-INF/tags or/META-INF/tags.
As for the Tag file TLD can be placed in the WEB-INF can also be placed in the META-INF, there is no problem
The tag file describes the tag processing class, And the JSP describes the servlet, both of which are not running. Note that there are <% @ tag %> In the Tag file, and many tag attributes can be set.
You can also use <% @ attribute %>, <% @ taglib %>, and <% @ include %>. The attribute can be jspfragment.

For example: <my: First>
<JSP: attribute>
</Jsp: attribute>
<JSP: Body>
</Jsp: Body>
</My: First>

<JSP: invoke name = var> the calculation result is stored in var.
<JSP: Dobody> processing body

VII. jstl
There are five databases in total. We have discussed the core, SQL, i18n, and FN markup libraries (the function library of the Expression Language)
We have no tag library for XML.

8. Internationalization and Chinese character encoding
Resourcebundle
Two forms:
1. listresourcebundle
2. Write a properties file with the nativetoacⅱ command. refer to the previous note for transcoding.
Base name and Extension: Standard names are required.
Locale: localized object
The getbundle method can be processed by passing in the base name and localization object.
<FMT: bundle>
<FMT: setbundle>
<FMT: Message>
Chinese character encoding: gb2312/gb13000 (GBK)/gb18030
Unicode UCS/UTF-8
 

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.