JSP preliminary

xiaoxiao2021-03-06  26

Filter technology FILTER technology is a newly added function of servlet 2.3.Servlet2.3 is released from SUN and October 2000, which developers include many individuals and corporate groups, fully reflecting the code open principles advocated by Sun. Due to the common efforts of many participants, servlet2.3 is much better than previous functions, and performance has also increased significantly.

Its newly added features include:

1. Application lifecycle event control;

2. New internationalization;

3. Clarify the load rules of the class;

4. New errors and security properties;

5. Do not agree with the HTTPUTILS class;

6. Various useful methods;

7. Explain and extend several servlet DTDs;

8. FILTER function.

The most important of these is Filter function. It enables users to change a request and modify a response. Filter is not a servlet, which cannot generate a response, which can prepare the request before a request arrives, or processes when leaving the servlet RESPONSE. The Filter is actually a "servlet chain". A Filter includes:

1. Intercept before the servlet is called;

2. Check the servlet request before servlet is called.

3. Modify the Request header and the request data as needed;

4. Modify the response header and response data as needed;

5. Intercept after servlet is called.

You can configure a Filter to one or more servlets; a single servlet or servlet group can be used by multiple FILTERs. Several useful Filters include: User identifying Filter, Log Filter, auditing FILTER, encryption filter, symbol Filter, can change XML XSLT Filter, etc.

A Filter must implement a javax.servlet.filter interface and define three methods:

1.void setFilterConfig (FilterConfig Config) // Sets the Filter configuration object;

2. FilterConfig getFilterConfig () // Returns the configuration object of the FILTER;

3. Void Dofilter (ServletRequest Req, ServletResponse Res, Filterchain Chain) Executes the Filter's work.

The server only calls the setFilterConfig method once a time to prepare the process; call the DOFILTER method multiple times to handle different requests. The FilterConfig interface can find the Filter name and initialization parameter information. The server can set FilterConfig to indicate that the filter has ended.

Each Filter gets current Request and Response in the Dofilter () method. In this method, you can perform any operations for Request and Response. (Including collecting data, packaging data, etc.). Filter call chain.dofilter () method Give the control right to the next filter. One Filter ends in the DOFILTER () method. If a Filter wants to stop the Request processing, get complete control to Response, then it can call the next filter.

A FILTER can package Request or Response to change several methods and provide user-customized properties. API2.3 provides HTTPSERVLETREQUESTWRAPPER and HTTPSERVLETRESPONSEWRAPPER to achieve. They can assign initial request and response. If you want to change a method, you must inherit WAPper And override. Below is a simple log filter to record all REQUEST durations.

Public class logfilter imports filter {

FilterConfig Config;

Public void setfilterconfig (FilterConfig Config) {

THIS.CONFIG = Config;

Public filterconfig getfilterconfig () {

Return Config;

}

Public void Dofilter (ServletRequest Req,

ServletResponse Res,

Filterchain chain) {

ServletContext context = getFilterConfig (). GetServletContext ();

Long BEF = system.currenttimemillis ();

Chain.dofilter (REQ, RES); // no chain parameter needed here

Long Aft = system.currenttimemillis ();

Context.log ("Request to" Req.GetRequesturi ()

":" (AFT-BEF));

}

}

When Server calls setFilterConfig (), Filter saves config information. Get servletContext by config information in the DOFILTER () method. If you want to run this filter, you must go to Web.xml. Take Tomcat4.01 as an Example:

LOG / / Filter name

Logfilter // Filter Class (servlet in the previous example)

log

servletname

servletname

servletclass

servletname

*

Put this web.xml in Web-INF (see the Tomcat Help Document for details).

When requesting a request (such as index.jsp), first go to the logfilter and call the DOFILTER () method, then go to the respective servlets. If it is a simple servlet (just a page, no output statement ), Then possible outputs are:

Request to /index.jsp: 10There are only four standard URL patterns (and this applies to servletmappings and security constraints as well as filter mappings): * Exact match (/ foo) * Path match (/ foo / *) * Extension match ( * .fau) * Default servlet (/) Which is useful only for servlet mapings there is no legal syntax to say "all but this set of path". cookie technology The Cookie technology of the Internet is extremely simple, but there is a strong vitality. Cookies have begun to attract the attention of everyone in February 2000, with the proposal of online privacy, the relevant debate is still continuing. On the other hand, cookie makes it easier to browse the web page. Almost all of the main website designers have used cookies because they want to provide a better browsing environment for those who browse websites, and can also collect access to visitors more accurately.

There is a very deeply influential newspaper, I have published a deep article about online privacy, which is the case with the definition of cookie:

"Cookie is a program on your hard drive. It is in your computer, collect your information and anything you do on the Internet, it can download all these collections when the Web site needs Information."

The definition like this is quite common in the newspaper. The problem is that its definition has made a big mistake. Cookie is not a program, and it can't run like a program, so it can't collect any information for yourself. It can't get any personal information from your computer.

Cookie's relatively exact definition should look like this:

"Cookie is a text of the web server to save on the user's hard disk. Cookie allows a Web site to save information on the user's computer and then retrieve it. The piece of information is the 'name / value' pair (Name-Value PAIRS) Form storage. "

For example, a Web site may generate a unique ID for each visitor, and then saved on each user's machine as a cookie file.

If you use the IE browser to access the web, you will see all the cookies saved on your hard drive. The most often stored places are: c: / windows / cookies (in Win 2000 is C: / Documents and Settings / Your username / cookies - author note). There are 165 files on my machine. Every file is a text file consisting of "Name / Value", and there is another file saved with information about the corresponding Web site.

Each cookie file in this folder is a simple and ordinary text file. With the file name, you can see which web site places a cookie on your machine (of course, the site information is saved in the file). You can also double-click to open every cookie file.

For example, I have visited goto.com, and this site put a cookie on my computer. Goto.com's cookie file contains this content:

Userid a9a3bece0563982d www.goto.com/

Goto.com stores a single "name / value" pair on my computer. "Name / Value" pair is UserID, "Value" is A9A3Bece0563982d. When I first visited Goto.com, the site assigned a unique ID and there is my computer.

(Note: In addition to the "name / value" pair above, there may be other "names / values" to save simultaneously. That is some internal information of the browser, the general user does not have to do more.)

Amazon.com saves more information on my computer. When I view Amazon created a cookie file on my computer, it contains the following: session-id-time 954242000 Amazon.com/

Session-ID 002-4135256-7625846 Amazon.com/

X-main ekqifwnxuf7qtmx52x6vwaxh @ ih6uo5h amazon.com/

Ubid-main 077-9263437-9645324 Amazon.com/

The above shows that Amazon stores a primary user ID Ubid-main, a tagged session-id and task occurring time session-id-Time. There is also an x-main, I don't know what it is.

Most websites only save a message on your computer, that is, the user ID. But a site can be stored with the maximum number of "name / value" stored in cookie.

A "name / value" is just a named data, it is not a program, and it can't "do" anything. A website can only get information in your computer, it can't get information from other cookie files, and you can't get anything on your computer.

转载请注明原文地址:https://www.9cbs.com/read-69829.html

New Post(0)