,
nike dunks
| Back to logs list
Reprinted from 304510237 at 10:04 on July 29, 2010 Reading (loading. ..) Comments (0) Category: Technical Articles
jsp development of the listener and filter, the two things to say it,
dunk sb high, very, very complicated. Today I wanted to use a practical example to illustrate the role of these two stuff.
Case Description
Suppose you usually live in Beijing,
nike dunk, one day you go to a very remote and very poor village to experience life to the village, you want something, the village Long would agree, and then arrange for you the villagers,
nike dunk shoes, but one exception is that once you ask them for money, the village head are determined not to, will lead the villagers to fight back to Beijing to you.
where you take away from the villagers for many things as possible, so you prepare before departure of a large bag,
cheap dunk sb, hoping to give everything to the villagers in the bag Lane; and then after you return to the city, this bag is useless,
high dunk sb, and you put this bag to throw it away.
code simulation
Well, I would like to use the following simple code to simulate the above case, we can think about what role the actor listener? What is the role filter?
1. Create a java web project;
2. default index.jsp page with two links to simulate the sweet potato and money to villagers to, the following:
Welcome to fall into the unnamed village, haha
3. to create a new big bag category: BigBag.java, as follows:
package chb.demo.web;
import java.util.HashMap;
public class BigBag ...{
public static HashMap bag; / / what the villagers to put this bag
}
4. Create a new listener: MyListener. java, as follows:
package chb.demo.web;
import java.util.HashMap;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyListener implements ServletContextListener ...{
public void contextDestroyed (ServletContextEvent arg0) ...{
BigBag.bag = null;
System.out.println (\;
}
public void contextInitialized (ServletContextEvent arg0) ...{
BigBag.bag = new HashMap ();
System.out.println (\, and prepared to cloth bags \
}
5. and then create a filter: MyFilter.java, as follows:
package chb.demo.web;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyFilter implements Filter ...{
public void destroy () ...{
}
public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException ...{
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse rsp = (HttpServletResponse) response;
if (req.getRequestURI (). toString (). endsWith (\Fight back to Beijing to
rsp.sendRedirect (\>} else if (req.getRequestURI (). toString (). endsWith (\> int num = BigBag.bag.get (\sweet potato \}
/ / mayor agreed, and here are the homes of villagers took. .
chain.doFilter (request, response);
} else ...{
/ / if not the money line, release
chain.doFilter (request, response);
}
}
public void init (FilterConfig filterConfig) throws ServletException ...{
}
}
6. listener configuration in web.xml, and filters, as follows:
xsi: schemaLocation = \
myFilter
chb.demo.web.MyFilter ;
myFilter
/ *
chb.demo.web.MyListener
index.jsp
Well, done, the deployment operation, observe the result?
results
We will observe the following results:
process when the service starts, the console output \
when click \
when click \Understand a little bit?
In fact, the listener is to do a good job in the service assurance services before starting preparations for the normal operation of the service end and then look at the aftermath; corresponds to the case situation in which the bag before you hit the road ready to throw the bag back out; similar nanny work, child care, get up, and then take care of the child asleep.
filter is a request for the server, to filter constraints; corresponds to the case situation in which the village head, and holds sway, nothing too me off Caixing ^_^
in the project will always encounter some problems on the priority of loading, also recently encountered similar, so look for information on their summary, the following are reproduced in some other who, after all, people write good, they would not repeat the wheel, and only slightly modified his point.
First, be sure to load the order with their order in the web.xml file has nothing to do. That will not filter written in front of the listener will first load the filter. The final conclusion is: listener -> filter -> servlet
also the existence of such a configuration section: context-param, which is used to provide to the ServletContext value pairs, that is, the application context information. Our listener, filter, etc. will be used in the initialization information in these contexts, then the context-param configuration section is not written in the listener should be configured before the holiday it? In fact context-param configuration section can be written in any position, so the real load in the order: context-param -> listener -> filter -> servlet
configuration section for certain terms and they appear in the order are related. The filter example, web.xml can of course define multiple filter, associated with a configuration section filter is filter-mapping, we must pay attention here, with the same filter-name for the filter and filter-mapping configuration section is concerned, filter-mapping must appear after the filter, or when the resolution to the filter-mapping, the corresponding filter-name it has not been defined. web container startup time to initialize each filter, filter configuration section in accordance with the order to initialize, and when the requested resource matches when multiple filter-mapping, filter blocking resource is in accordance with the filter-mapping configuration section in the order to order call the doFilter () method.
servlet filter with a similar, not repeat them here.
Thus, we can see, web.xml load order is: context-param -> listener -> filter -> servlet, and between the same two types of calls when the actual process The order is based on the order of the corresponding mapping calls.
Web.xml common elements
defines the name of WEB applications
Statement WEB application descriptions
context-param element declaration within the scope of application initialization parameter.
filter element will be a name and an implementation class associated javax.servlet.Filter interface.
once named a filter, filter-mapping element must use it with one or more of the servlet or JSP page is associated.
servlet API version 2.3 adds support for the event listener, event listener to establish, modify, and delete the session or be notified when the servlet environment.
Listener element that the event listener class.
servlet or JSP page in the development of the initialization parameters or custom URL, you must first name the servlet or JSP page. Servlet element is used to complete this task.
server is generally a default servlet URL:
, however, often change the URL, so you can access the servlet initialization parameters, or more easily treatment relative URL. When you change the default URL, use the servlet-mapping element.
If a session has not been accessed within a certain period of time, the server can discard it in order to save memory.
setMaxInactiveInterval by using the method of HttpSession explicitly set the timeout value of a single session object, or session-config element can be used to develop the default timeout value.
If the Web application has thought of a special file, hoping to ensure that they are assigned to a specific MIME type, mime-mapping element to provide such assurance.
the server to reference a directory name in the received file name instead of URL, using which file.
HTTP status code returned to a specific, or specific type of exception is thrown, can develop the page to be displayed.
of the tag library descriptor file (Tag Libraryu Descriptor file) specified alias. This feature allows you to change the TLD file location,
use these files without having to edit the JSP page.
statement and resources related to a management object.
to declare a resource factory to use external resources.
development should protect the URL. It is with the login-config element in combination with
how to specify the server tries to access a protected page, the user authorization. With the sercurity-constraint element is used in combination.
gives a list of security roles, these roles will appear in the servlet element in the security-role-ref role-name element
the child element. Statement respectively Advanced IDE can handle the role of safety information more easily.
Statement Web application environment entry.
declare an EJB reference to the main directory.
declare an EJB's local home directory of the application.
corresponding element configuration
1, Web Application icon: IDE and GUI tools that Web application used to represent large icon and small icon
/ images / app_small.gif
/ images / app_large.gif
2, Web application name: Provides GUI tools that may be used to mark a particular Web application name
Tomcat Example
3, Web Application Description: This paper presents a descriptive text related to this
Tomcat Example servlets and JSP pages.
4, context parameters: the initial statement within the scope of application initialization parameters.
ContextParameter
test
It is a test parameter.
in the servlet which can getServletContext (). getInitParameter (\configuration: a name and an implementation class associated javaxs.servlet.Filter interface.
setCharacterEncoding
com.myTest.setCharacterEncodingFilter
encoding
GB2312
setCharacterEncoding
/ *
6, listener configuration
listener.SessionListener
7 , Servlet configuration
basic configuration
snoop
SnoopServlet
snoop
/ snoop
Advanced Configuration
snoop
SnoopServlet ;
foo
bar
Security role for anonymous access
tomcat
snoop
/ snoop
elements that
The data is used to declare a servlet, mainly in the following sub-elements:
specifies the name of the servlet
specified servlet class name
; specify the web site in the full path to a JSP page
to define the parameters, can have more than one init-param. In the servlet class by getInitParamenter (String name) method to access initialization parameters
specified when the Web application starts, Servlet load order.
when the value is positive or zero: Servlet container load value of the first small servlet, then in turn loads a large number of other servlet.
when the value is negative or undefined: Servlet container will be the first Web client visit the servlet to load it
servlet is used to define the corresponding URL, contains two child elements
specifies the name of the servlet
corresponding to the specified servlet URL
8, session timeout configuration (in minutes)
120
9, MIME type configuration
htm
text / html
10, specify the welcome page file configuration
index.jsp
index.html
; index.htm
11,
a configuration error page by error code to configure the error-page
404
/ NotFound.jsp
when the above configuration 404 system error occurs, jump to the error handling page NotFound.jsp.
Second, the type of configuration by exception error-page
java.lang.NullException
/ error.jsp
above occurs when the system is configured java.lang.NullException (the null pointer exception), the jump to the error handling page error.jsp
12, TLD configuration
/ WEB-INF/jsp/debug-taglib.tld
if MyEclipse has been in error and should be put into in
/ WEB-INF/pager-taglib.tld
13, Resource Management Object Configuration
jms / StockQueue
14, the resource factory configuration
mail / Session
javax.mail.Session
Container
configure the database connection pool to in this configuration:
JNDI JDBC DataSource of shop
jdbc / sample_db
javax.sql.DataSource
Container
15, security restrictions configuration
Example Security Constraint
Protected Area
/ jsp / security / protected / *
DELETE
GET
POST
PUT
tomcat
role1
16, login authentication scheme
FORM
Example-Based Authentiation Area
; / jsp / security / protected / login.jsp
/ jsp / security / protected / error.jsp
17, security roles: security-role element is given a list of security roles, these roles will appear in the servlet element of security -role-ref element in the role-name child element.
statement respectively Advanced IDE can handle the role of safety information easier.
tomcat
18, Web environment parameters: env-entry element declares Web application environment entry
minExemptions
1
java.lang.Integer
19, EJB statement
Example EJB reference
ejb / Account
Entity
com.mycompany.mypackage.AccountHome
com.mycompany.mypackage.Account
20, the local EJB statement
Example Loacal EJB reference
ejb / ProcessOrder
Session
com.mycompany.mypackage.ProcessOrderHome
com.mycompany.mypackage.ProcessOrder
21, configure DWR
dwr-invoker
uk.ltd.getahead.dwr.DWRServlet
dwr-invoker
/ dwr / *
22, configure Struts
Struts Blank Application
action
org.apache.struts.action.ActionServlet
; detail
2
debug
2
config
/ WEB-INF/struts-config.xml
application
ApplicationResources
2
action
*. do
index.jsp
struts-bean
/ WEB-INF/tld/struts-bean.tld
struts-html
/ WEB-INF/tld/struts-html.tld
struts-nested
/ WEB-INF/tld/struts- nested.tld
struts-logic
; / WEB-INF/tld/struts-logic.tld
struts-tiles
/ WEB-INF/tld/struts-tiles.tld
23, configure the Spring (basically in Struts configured)
contextConfigLocation
/ WEB-INF/applicationContext.xml, / WEB-INF/action-servlet.xml
org.springframework.web.context.ContextLoaderListe ner
org.springframework.web.context.request.RequestCon textListener