人员管理信息系统-2.编码过滤器和核心控制器
- java编码过滤器的作用:
java过滤器能够对目标资源的请求和响应进行截取,过滤信息执行的优先级高于servlet。 - java过滤器的使用:
(1)编写一个普通的java类,实现Filter接口
package com.imooc.sm.global; import javax.servlet.*; import java.io.IOException; public class EncodingFilter implements Filter { private String encoding = "UTF-8"; public void init(FilterConfig filterConfig) throws ServletException { if (filterConfig.getInitParameter("ENCODING") != null) encoding = filterConfig.getInitParameter("ENCODING"); } public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { servletRequest.setCharacterEncoding(encoding); servletResponse.setCharacterEncoding(encoding); filterChain.doFilter(servletRequest, servletResponse); } public void destroy() { encoding = null; } }
(2) 在web.xml文件中进行配置:主要有两个属性filter和filter-mapping
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <filter> <filter-name>Encoding</filter-name> <filter-class>com.imooc.sm.global.EncodingFilter</filter-class> <init-param> <param-name>ENCODING</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
- 核心控制器Servlet:
作用:当用户向web容器发送一个请求的时候,web容器会根据用户请求的url来判断是否应该由Servlet进行处理,然后应该由哪个Servlet进行处理,判断出来以后,构造这个Servlet对象,然后进行初始化,最后调用service对象对这次请求进行实际处理。
步骤:
- 解析URL
- 创建applicationcontext对象,得到相应的bean对象
- 根据bean对象得到相应的方法,然后得到执行
public class DispatchServlet extends GenericServlet { private ApplicationContext context; public void init() throws ServletException { context = new ClassPathXmlApplicationContext("spring.xml"); } public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { HttpServletRequest request = (HttpServletRequest)servletRequest; HttpServletResponse response = (HttpServletResponse)servletResponse; /* * /staff/add.do /login.do * staffcontroller * public void add(HttpServletRequest,HttpServletResponse) {} * */ String path = request.getServletPath().substring(1); //去掉第一个斜杠,得到staff/add.do String beanName = null; String methodName = null; int index = path.indexOf('/'); if (index != -1) { beanName = path.substring(0,index) + "controller"; methodName = path.substring(index + 1,path.indexOf(".do")); } else { beanName = "selfController"; methodName = path.substring(0,path.indexOf(".do")); } Object obj = context.getBean(beanName); try { Method method = obj.getClass().getMethod(methodName,HttpServletRequest.class,HttpServletResponse.class); method.invoke(obj,request,response); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } }
因为所有的Servlet对象要在web.xml文件中进行配置,所以要进行Servlet配置
<servlet> <servlet-name>Global</servlet-name> <servlet-class>com.imooc.sm.global.DispatchServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Global</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
4.测试
在controller包中创建一个测试类
public class TestController { // test/show.do show.jsp是在根目录底下的,所以上一层的show.jsp public void show(HttpServletRequest request, HttpServletResponse response) { request.setAttribute("NAME","张三"); try { request.getRequestDispatcher("../show.jsp").forward(request,response); } catch (ServletException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
在根目录底下(webapp)创建一个show.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> 你好,世界! ${NAME} </body> </html>
```