时间:2023-05-24 22:18:02 | 来源:网站运营
时间:2023-05-24 22:18:02 来源:网站运营
大数据从入门到深入:JavaEE 之 动态网页开发基础 过滤器的简单使用:package com.hnxy.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;/** * Servlet Filter implementation class EncodingFitler */public class EncodingFitler implements Filter { /** * Default constructor. */ public EncodingFitler() { // TODO Auto-generated constructor stub } /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub // place your code here // pass the request along the filter chain chain.doFilter(request, response); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub }}
<filter> <display-name>EncodingFitler</display-name> <filter-name>EncodingFitler</filter-name> <filter-class>com.hnxy.web.EncodingFitler</filter-class> </filter> <filter-mapping> <filter-name>EncodingFitler</filter-name> <url-pattern>/EncodingFitler</url-pattern> </filter-mapping>
urlPattern 设置拦截过则 /* 代表任何对servlet的请求 过滤器都要先拦截处理后再交给具体 servlet <filter> <display-name>EncodingFitler</display-name> <filter-name>EncodingFitler</filter-name> <filter-class>com.hnxy.web.EncodingFitler</filter-class> </filter> <filter-mapping> <filter-name>EncodingFitler</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2) 编写doFilter方法 /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // 设置请求流编码 request.setCharacterEncoding("UTF-8"); chain.doFilter(request, response); }
完成效果package com.hnxy.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.annotation.WebFilter;/** * Servlet Filter implementation class EncodingFitler */public class EncodingFitler implements Filter { /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // 设置请求流编码 request.setCharacterEncoding("UTF-8"); chain.doFilter(request, response); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub }}
3) 测试<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><div> <form action="Test1Action" method="post"> <p> 请输入名称 : <input type="text" name="userName" /> </p> <p> <button type="submit">提交</button> </p> </form></div></body></html>
servlet :package com.hnxy.web;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class Test1Action */public class Test1Action extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println(request.getParameter("userName")); }}
我们可以看到,现在servlet没有对数据做任何参数上的处理,如果没有过滤器是这样的:package com.hnxy.utils;import java.sql.SQLException;import com.alibaba.druid.pool.DruidDataSource;/** * 数据库连接工具类 * @author My * */public class JdbcUtil { // 创建数据库的连接对象 private static final String CONN_DRIVER = "com.mysql.jdbc.Driver"; private static final String CONN_URL = "jdbc:mysql://127.0.0.1:3306/userdb?characterEncoding=UTF-8"; private static final String CONN_USER = "root"; private static final String CONN_PASSWORD = "root"; // 创建数据源对象 private static DruidDataSource dataSource = new DruidDataSource(); // 赋值 static{ dataSource.setDriverClassName(CONN_DRIVER); dataSource.setUrl(CONN_URL); dataSource.setUsername(CONN_USER); dataSource.setPassword(CONN_PASSWORD); // 开启SQL语句的统计功能 try { dataSource.addFilters("stat"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 添加获取数据源的方法 * @return */ public static DruidDataSource getDataSource() { return dataSource; }}
2) web.xml中加入druid的核心servlet和过滤器即可 <!-- Druid,监控数据库,以及WEB访问连接信息 --> <filter> <filter-name>DruidWebStatFilter</filter-name> <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class> </filter> <filter-mapping> <filter-name>DruidWebStatFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置 Druid 监控信息显示页面 --> <servlet> <servlet-name>DruidStatView</servlet-name> <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class> <init-param> <!-- 允许清空统计数据 --> <param-name>resetEnable</param-name> <param-value>true</param-value> </init-param> <init-param> <!-- 用户名 --> <param-name>loginUsername</param-name> <param-value>admin</param-value> </init-param> <init-param> <!-- 密码 --> <param-name>loginPassword</param-name> <param-value>admin123</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DruidStatView</servlet-name> <url-pattern>/druid/*</url-pattern> </servlet-mapping>
配置好之后正常开启tomcat,正常访问项目,你会发现加不加都没有什么问题!你可以先玩一会 儿项目,然后在访问下面的地址关键词:基础,过滤,使用,简单,入门,数据,深入,动态