Configuration of the web.xml in our application

As we all known, the configuration of web.xml is the file that our application tells servlets what to do. there are many elements defined in web.xml, next I will tell what's the function of every element.
1, <context-param>
The optional context-param element declares a Web Application's servlet context initialization parameters. You set each context-param within a single context-param element, using <param-name> and <param-value> elements. You can access these parameters in your code using the javax.servlet.ServletContext.getInitParameter() and javax.servlet.ServletContext.getInitParameterNames() methods.
[b]Element Required/ Optional Description
<param-name> Required The name of a parameter.
<param-value> Required The value of a parameter.
<description> Optional A text description of a parameter[/b]

2,<filter> & <filter-mapping>
The <filter> element declares a filter, defines a name for the filter, and specifies the Java class that executes the filter.The <filter-mapping> element specifies which filter to execute based on a URL pattern or servlet name. The <filter-mapping> element must immediately follow the <filter> element(s).
[quote]import javax.servlet.*;
public class Filter1Impl implements Filter
{
private FilterConfig filterConfig;

public void doFilter(ServletRequest req,
ServletResponse res, FilterChain fc)
throws java.io.IOException, javax.servlet.ServletException
{
// Execute a task such as logging.
//...

fc.doFilter(req,res); // invoke next item in the chain --
// either another filter or the
// originally requested resource.

}

public FilterConfig getFilterConfig()
{
// Execute tasks
return filterConfig;
}

public void setFilterConfig(FilterConfig cfg)
{
// Execute tasks
filterConfig = cfg;
}
}[/quote]

3,<listener>
The event declaration defines the listener class that is invoked when the event occurs.There are several servlet based event, so you should extend this event, and then when the event happens, your listener class will be excuted.
[quote]/**
* UserCounterListener class used to count the current number
* of active users for the applications. Does this by counting
* how many user objects are stuffed into the session. It Also grabs
* these users and exposes them in the servlet context.
*/
public class UserCounterListener implements ServletContextListener,
HttpSessionAttributeListener {
public static final String COUNT_KEY = "userCounter";
public static final String USERS_KEY = "userNames";
public static final String EVENT_KEY = HttpSessionContextIntegrationFilter.ACEGI_SECURITY_CONTEXT_KEY;
private final transient Log log = LogFactory.getLog(UserCounterListener.class);
private transient ServletContext servletContext;
private int counter;
private Set users;

public synchronized void contextInitialized(ServletContextEvent sce) {
servletContext = sce.getServletContext();
servletContext.setAttribute((COUNT_KEY), Integer.toString(counter));
}

public synchronized void contextDestroyed(ServletContextEvent event) {
servletContext = null;
users = null;
counter = 0;
}

synchronized void incrementUserCounter() {
counter =
Integer.parseInt((String) servletContext.getAttribute(COUNT_KEY));
counter++;
servletContext.setAttribute(COUNT_KEY, Integer.toString(counter));

if (log.isDebugEnabled()) {
log.debug("User Count: " + counter);
}
}

synchronized void decrementUserCounter() {
int counter =
Integer.parseInt((String) servletContext.getAttribute(COUNT_KEY));
counter--;

if (counter < 0) {
counter = 0;
}

servletContext.setAttribute(COUNT_KEY, Integer.toString(counter));

if (log.isDebugEnabled()) {
log.debug("User Count: " + counter);
}
}

synchronized void addUsername(Object user) {
users = (Set) servletContext.getAttribute(USERS_KEY);

if (users == null) {
users = new HashSet();
}

if (log.isDebugEnabled()) {
if (users.contains(user)) {
log.debug("User already logged in, adding anyway...");
}
}

users.add(user);
servletContext.setAttribute(USERS_KEY, users);
incrementUserCounter();
}

synchronized void removeUsername(Object user) {
users = (Set) servletContext.getAttribute(USERS_KEY);

if (users != null) {
users.remove(user);
}

servletContext.setAttribute(USERS_KEY, users);
decrementUserCounter();
}

/**
* This method is designed to catch when user's login and record their name
* @see javax.servlet.http.HttpSessionAttributeListener#attributeAdded(javax.servlet.http.HttpSessionBindingEvent)
*/
public void attributeAdded(HttpSessionBindingEvent event) {
log.debug("event.name: " + event.getName());
if (event.getName().equals(EVENT_KEY)) {
SecurityContext securityContext = (SecurityContext) event.getValue();
User user = (User) securityContext.getAuthentication().getPrincipal();
addUsername(user);
}
}

/**
* When user's logout, remove their name from the hashMap
* @see javax.servlet.http.HttpSessionAttributeListener#attributeRemoved(javax.servlet.http.HttpSessionBindingEvent)
*/
public void attributeRemoved(HttpSessionBindingEvent event) {
if (event.getName().equals(EVENT_KEY)) {
SecurityContext securityContext = (SecurityContext) event.getValue();
User user = (User) securityContext.getAuthentication().getPrincipal();
removeUsername(user);
}
}

/**
* @see javax.servlet.http.HttpSessionAttributeListener#attributeReplaced(javax.servlet.http.HttpSessionBindingEvent)
*/
public void attributeReplaced(HttpSessionBindingEvent event) {
// I don't really care if the user changes their information
}
}[/quote]

4,<servlet> & <servlet-mapping>
the servlet element configs the actions when servlet context starts,and <servlet-mapping> defines which class to excute when the defined action creates.
[quote]<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>[/quote]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值