servlet,jsp, filters and listeners, four scopes and nine built-in objects, El expression language, custom tags, jstl__jsp

Source: Internet
Author: User
Tags tld tomcat server

A Servlet
1, servlet, ServletConfig: The former corresponds to a servlet class, which corresponds to the web.xml in the configuration information
(1) Servlet:
Init (ServletConfig): Once a class is loaded and instantiated, it is invoked once.
Service (Servletrequest,servletresponse): will be called multiple times
Destroy (): only once
Getservletconfig ()
Getservletinfo ()
(2) Servletcionfig
Getservletname ()
Getinitparameter (String):---<servlet><init-param></init-param></servlet>
Getinitparameternames ():
Getservletcontext
Note that there is also a getinitparameter (String) in the ServletContext, which corresponds to a nested element in the:<context-param>
This <context-param> and <servlet> tag is the same level, initializing the entire application

2, two categories:
Genericservlet/httpservlet:
Genericservlet implements the servlet and Servletconfig,httpservlet inheritance from Genericservlet, and it is related to the HTTP protocol.
(1) Genericservlet: Has a parameterless Init method to facilitate child class coverage.
(2) HttpServlet: Has two service methods, and has 7 do methods. When overridden, General rewrite do method

3. servlet Configuration
Refers to the information in the Web.xml,
<servlet>
<servlet-name></servlet-name>
<servlet-class></servlet-class>
<init-param></init-param>
</servlet>
The above configuration corresponds to what can be said to be in the servletconfig.
<servlet-mapping>
<servlet-name></servlet-name>
<url-pattern></url-pattern>
</servlet-mapping>

4. Structure of Web Application Folder
--web-inf
|
|------------web.xml
|------------lib folder jar file
|------------Classes class file
|------------tags tagfile tags file
--static resources and JSP files

5, Request and response:
(1) Request Servletrequest->httpservletrequest
GetParameter (String),
GetHeader (),
Getparametervalues (String), for check boxes
Getparameternames,
GETREMOTEADDR, get the remote address
GETLOCALADDR, get local address
GetLocale, get localized information
Getsession/getcookies
Getrequestdispatcher

(2) Response Servletresponse->httpservletresponse
Sendredirect
Senderror
Setheader/addheader/setintheader
Getwriter/getoutputstream: These two methods can never be invoked at the same time ...
Setcontenttype/setcharacterencoding

6. The difference between the forwarding of the request and the redirection of the response should be clear
(1) Forwarding:
A, three ways to forward:
Requestdispatcher.forward/include
<jsp:include>
<jsp:forward> equivalent to Requestdispatcher.forward ();
That is, after using Requestdispatcher.forward (), the following code still executes, but after <jsp:forward> executes, the following code is not executed.
Pagecontext.forward/include
B, how to get RequestDispatcher:
Request.getrequestdispatcher
Servletcontext.getrequestdispatcher

RequestDispatcher request forwarding, instruction element include in the JSP page, and getResourceAsStream in ServletContext (String path)
(used to read the path resources in a folder), you can access the Web-inf folder.
For example, to prevent users from accessing some protected pages (such as the controller servlet), put them in the Web-inf and request forwarding to access them.
Sometimes you need to read some of the configuration resources in Web-inf, and you need the getResourceAsStream method in ServletContext.
and the pages of some sites have the same head and tail images, which can be included in the Web-inf folder with the page element's include instructions.

Note If you are using ServletContext RequestDispatcher, to forward requests to B from the a application, you must set the Crosscontext value of a to true.

(2) redirect
Response.sendredirect: For example, when you go to the next page after registering, be sure to use redirection to change the URL of the browser.
The requested forwarding cannot be separated from the scope of the Tomcat server and can only be redirected by responding if you want to detach from the server. Redirect to generate a temporary response, (response
Once the build request is over, the browser does not display anything but sends a request to find a new address after receiving this temporary response.


Second, JSP
1, templates and elements:
For JSP, it is not executable, must be translated into a servlet to execute, must be the container special processing called elements,
To print directly to the output stream is the template

Elements are divided into the following categories:
1 Scripting elements
2 Directive elements
3 Action Elements

1 Scripting elements:
(1) Script fragment: The translation is left intact in the service method inside
(2) Script declaration: Placed inside the class, but outside the service method,
(3) Script expression: put it intact in the out.print, the script expression can not add a semicolon.

Built-in objects must not be used in script declarations. Because their role is in the service method.

2 Directive elements:
Page directive:
Include instruction <% @include file= ""%>, the included file must be read in plain text format, there is a coding problem when reading the file, this is the pageencoding
Set up the problem.
Taglib directives

3 page garbled: Pageencoding/contenttype
The former sets the form of the JSP page read, which sets a response header, telling the browser what encoding format to display
If you do not set pageencoding but only contenttype, then pageencoding is affected by ContentType and vice versa.
That is to say the two only set one words will be followed by a code to display.

No matter the request or the response, the default is to follow the iso-8859-1 to decode. Make sure that the code for both reading and writing is consistent.

4 Action elements:
<jsp:usebean>: Main function: Open a script variable and save a property to the scope
<jsp:setProperty>
<jsp:getProperty>
<jsp:forward>
<jsp:include>
<jsp:param>
The first six are more important.
<jsp:invoke>
<jsp:doBody>
<jsp:plugin>
<jsp:fallback>
<jsp:params>

Third, 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>

Url-pattern the same filter is on a chain of filters, and the order of execution is performed exactly in the order of Web.xml.

2. Listener->servletcontext/httpsession/servletrequest
(1) Life cycle
Servletcontextlistener: Initialize (read all discussion areas from the BBS and store them in the scope of the application or read out all blocked IP storage) and destroy
Httpsessionlistener/httpsessionactivationlistener
Servletrequestlistener
(2) Modification of attributes
It's usually done by Attributelistener.
<listener>
<listener-class> package name. Class Name </listener-class>
</listener>

四、四个 scopes and nine built-in objects
1, the page scope corresponds to the PageContext, not page
2. Built-in objects:
Pagecontext/request/session/application
Out-> is an example of JspWriter, it has caching, and PrintWriter does not cache, at the end of the page will certainly be generated by the response to generate printwriter to write the cached content.
If the cache is full, look: if <% @page autoflush= "true"%>, it will automatically refresh and, if False, throw an exception. Response.getwriter ()
The return is PrintWriter, not jspwriter.
It's only useful when exception->iserrorpage=true.
Session: Two kinds of tracking mechanism: one is through the client's cookie storage, the other is to have the SessionID server side,
Config->servletconfig
Page->object->this
Response

V. El expression language:
${}
1, constants, functions, variables of the expressions
2. A variable is a property value that is stored in a scope with the same name
3. A function is a public and static method of referencing a class declared in a TLD file
4, 11 built-in objects
(1) Scope: Pagescope/requestscope/sessionscope/applicationscope
(2) and page communication: PageContext
(3) Request parameter: Param/paramvalues
(4) Header: Header/headervalues
(5) Cookies/initparam---<context-param>
5, dot operator and [] operator and empty
For JavaBean, the getter method is invoked,
For the map, it is called the Get (Object) method,
To the scope, is to call GetAttribute (String)
For PageContext, it is also called the Getter method to get all the built-in objects
For Param, is to get the parameters
For header, it's to get the header.
For Initparam, it's going to take the initialization parameters
[] operators are primarily for arrays


Six Custom labels
1, Tag interface (traditional interface series)

Jsptag
(1) Tag:dostarttag (return value is 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
There is a pushbody inside the Doafterbody:eval_body_buffered->bodycontent:pagecontext, which is used to wrap the body, and then the Getout method is used to get
is no longer a jspwriter, but a bodycontent. The generation of Bodycontent is called the Pushbody method, which in turn involves a getenclosingwriter (), which
is a method of bodycontent, you can get it in the output stream of bread jspwriter, but to note that if more than once pushbody but did not carry out popbody, then
It is the bodycontent of the bread, but since bodycontent is a subclass of JspWriter, it is also a method declaration.


2, Simpletag interface (simple interface series)
Setjspcontext ()
SetParent ()
Setjspbody (jspfragment) Note that Jspfragment is a label body and there is absolutely no scripting element in it. When registering, the content element is either set to empty,
Either set to Scriptless, that is, the label body of the simple label is not allowed to have script elements.
Dotag ()

Jspfragment.invoke (NULL): directly to the output stream
Jspfragment.invoke (StringWriter out): The output stream into the StringWriter, and then call out.tostring can turn the body into a string form

3, tag file tag files
. Tag
It can be in the/web-inf/tags or inside the/meta-inf/tags.
As for the tag file of the TLD can be placed in the Web-inf can also be placed in the Meta-inf, are no problem
Tag file describes the label processing class, JSP describes the servlet, both are not operational, note that the label file has <% @tag%>, which can set a number of tag properties
There are also <% @attribute%>,<% @taglib%> and <% @include%> can also be used, properties can be jspfragment

Like:<my:first>.
<jsp:attribute>
</jsp:attribute>
<jsp:body>
</jsp:body>
</my:first>

<jsp:invoke name=var> The result of the operation is in Var.
<jsp:doBody> Treatment Body

Seven, JSTL
A total of 5 libraries, we said core, SQL, i18n, FN tag library (is the expression Language function library)
We don't speak XML tag library viii. internationalization and Chinese character encoding
ResourceBundle
Two kinds of forms:
1, ListResourceBundle
2, write a properties file, which has a nativetoacsii command, referring to the previous note-transcoding
Base name and extension: All names have to be standard.
Locale: Localizing Objects
The Getbundle method can be processed by passing in the base name and the localized object
<fmt:bundle>
<fmt:setBundle>
<fmt:message>
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.