Servlet note three

One: connection pool

1. Concept: Create quantitative connection objects for web applications, save them in memory (connection pool), obtain connection objects from the pool each time you access, and put them back into the pool after use.

 

2. Tomcat connection pool development process: //The password cannot be too short

I. Provide the database connection Jar package (ojdbc5.jar) and import it into tomcat/lib.

"ojdbc5.jar" for jdk5.0.

"ojdbc6.jar", for jdk6.0.

"ojdbc14.jar" for JDK 1.4 and 5.0.

Note: If the jdk version is high, you can use "ojdbc6.jar".

 

II. Add the <resource /> tag in tomcat/conf/context.xml.

 

III. Encoding: // 1. Get Context.xml object 2. Find access name

 //java:comp/env/ is a fixed way of writing JNDI in java.

 

IV. Note: It belongs to Tomcat and must be used in the web environment. The main function junit test cannot be used.

 

V. Disadvantages: The coupling with the server is too strong, which is not conducive to maintenance. It is recommended to use third-party open source connection pools and framework connection pools (MyBatis, Hibernate, Spring).

 

3. DBCP connection pool usage process: third-party connection pool

I. Import the Jar package (ojdbc6.jar)

 

II. Import configuration files. (The key cannot be changed---the value can be changed)

 

III. Coding:

 

Two: filter

 

 

1. Concept:

I. The javax.servlet.Filter interface is an implementation class object, an object created when the server starts.

II. The execution status is before the Servlet. When requesting, it will go through the Filter first and then reach the target Servlet; when responding, the Filter will be executed in reverse again according to the execution process.

III. It can solve the redundant problem of common codes of multiple servlets (garbled code processing, login verification).

 

2. Implementation steps:

①: Customize the implementation of the Filter interface

②: Rewrite the three methods in javax.servlet.Filter

init initialization, called when the program loads

The end of the destruction web program

doFilter()  核心拦截办法

	public class MyFilter implements Filter {
	@Override
	public void destroy() {

	}
	// 核心拦截方法
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		// 允许链继续调用下一个Filter,若无过滤器,则负责传递到Servlet
		chain.doFilter(request, response);
	}
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

	}
}


③:配置web.xml

 

 

<filter>  :配置 Filter 名称,实现类以及初始化参数。可以同时配置多个初始化参数。

<filter-mapping> :配置什么规则下使用这个Filter 。

<url-pattern> :配置url的规则,可以配置多个,也可以使用通配符(*)。例如 /jsp/* 适用于本ContextPath下以“/jsp/ ”开头的所有servlet路径

多个filter的配置顺序即过滤器链调用顺序。

<dispatcher> :配置到达servlet的方式,可以同时配置多个。有四种取值:REQUEST、FORWARD、ERROR、INCLUDE。如果没有配置,则默认为REQUEST。它们的区别是:

# REQUEST :表示仅当直接请求servlet时才生效。

# FORWARD :表示仅当某servlet通过forward转发到该servlet时才生效。

# INCLUDE :Jsp中可以通过<jsp:include/>请求某servlet, 只有这种情况才有效。

# ERROR :Jsp中可以通过<%@page errorPage="error.jsp" %>指定错误处理页面,仅在这种情况下才生效。

4. 常用的Filter

I. 统一处理字符乱码:

 

 

II. 所有Servlet的权限验证(除:登录、注册):

 

 

 

 

三:监听器

1. 概念:等待一个特定事件的发生,一旦发生,则会触发监听器的事件执行(某个任务)。

2. HttpSessionListener:

监视HttpSession对象的创建和销毁

 

 

3. ServletContextListener:

监视ServletContext对象的创建与销毁

 

 

Xml文件配置

 

 Session对象的创建意味着服务器与客户端进行了一次会话,我们在进行UV统计的时候,以用户登录的Session创建为基准,统计在线人数。

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325385231&siteId=291194637