动态web项目使用Filter过滤器统一编码(装饰者模式)

本文深入解析Servlet和Filter的工作原理及应用,通过实例代码展示了如何在Java Web应用中使用Servlet接收HTTP请求并处理用户数据,同时介绍了Filter如何进行请求预处理,如统一编码,以及装饰者模式在Filter中的应用。
摘要由CSDN通过智能技术生成

Servlet

package com.it.code;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CodeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		System.out.println(username);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		System.out.println(username);
		
	}

}

Filter过滤器

package com.it.code;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

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.HttpServletRequestWrapper;

//如何使用装饰者模式:
//装饰者和被装饰者继承同一个类或者实现同一个接口
//装饰者中需要有被装饰者的引用
//对需要增强的方法进行增强
//对不需要增强的方法,直接使用父类的方法 
//统一编码
public class CodeFilter implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		chain.doFilter(new code(request), res);
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

}

class code extends HttpServletRequestWrapper {

	public code(HttpServletRequest request) {
		super(request);

	}

	@Override
	public String getParameter(String name) {
		String method = super.getMethod();
		if ("get".equalsIgnoreCase(method)) {
			String value = super.getParameter("username");
			try {
				value = new String(value.getBytes("iso-8859-1"), "utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return value;
		} else if ("post".equalsIgnoreCase(method)) {
			try {
				super.setCharacterEncoding("utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return super.getParameter(name);
		}
		return super.getParameter(name);

	}
}

xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>EnCodingFilter</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>CodeServlet</display-name>
    <servlet-name>CodeServlet</servlet-name>
    <servlet-class>com.it.code.CodeServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>CodeServlet</servlet-name>
    <url-pattern>/code</url-pattern>
  </servlet-mapping>
  <filter>
  	<filter-name>CodeFilter</filter-name>
  	<filter-class>com.it.code.CodeFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>CodeFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Exception.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值