• Servlet之编码过滤


    创建CharactorFilter类,实现javax.servlet.Filter接口:

    package com.caiduping;
    
    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;
    
    public class CharactorFilter implements Filter {
    	//字符编码
    	String encoding=null;
    	@Override
    	public void destroy() {
    		// TODO Auto-generated method stub
    		encoding=null;
    	}
    
    	@Override
    	public void doFilter(ServletRequest request, ServletResponse response,
    			FilterChain chain) throws IOException, ServletException {
    		// TODO Auto-generated method stub
    			//设置request的编码格式
    			request.setCharacterEncoding("UTF-8");
    			//设置response的字符编码
    			//response.setContentType(encoding);
    			response.setContentType("text/html;charset=UTF-8");
    		chain.doFilter(request, response);
    	}
    
    	@Override
    	public void init(FilterConfig filterConfig) throws ServletException {
    		// TODO Auto-generated method stub
    		encoding=filterConfig.getInitParameter("encoding");
    	}
    
    }
    

    然后对web.xml进行配置:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 
    	xmlns="http://java.sun.com/xml/ns/javaee" 
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
      <display-name></display-name>	
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      <filter>
      <filter-name>CharactorFilter</filter-name>
      <filter-class>com.caiduping.CharactorFilter</filter-class>
      <init-param>
      <param-name>CharactorFilter</param-name>
    	<param-value>UTF-8</param-value>
      </init-param>
      </filter>
      <filter-mapping>
      <filter-name>CharactorFilter</filter-name>
      <url-pattern>/*</url-pattern>
      </filter-mapping>
      
      <servlet>
      <servlet-name>AddServlet</servlet-name>
      <servlet-class>com.caiduping.AddServlet</servlet-class>
      </servlet>
      <servlet-mapping>
      <servlet-name>AddServlet</servlet-name>
      <url-pattern>/AddServlet</url-pattern>
      </servlet-mapping>
    </web-app>
    

    创建AddServlet类继承HttpServlet:

    package com.caiduping;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class AddServlet extends HttpServlet {
    	//serialVersionUID 用来表明类的不同版本间的兼容性
    	private static final long serialVersionUID = 1L;
    
    	@Override
    	protected void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		//处理GET请求
    		doPost(request, response);
    	}
    
    	@Override
    	protected void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		//当一个Servlet响应的时候将响应信息通过out对象输出到网页上,当响应结束时它自动被关闭
    		PrintWriter out=response.getWriter();
    		String name=request.getParameter("name");
    		String sex=request.getParameter("sex");
    		String age=request.getParameter("age");
    		String fav=request.getParameter("fav");
    		out.print("信息验证<br>");
    		out.print("姓名:"+name+"<br>");
    		out.print("性别:"+sex+"<br>");
    		out.print("年龄:"+age+"<br>");
    		out.print("爱好: "+fav+"<br>");
    		out.flush();
    		out.close();
    	}
    		
    }
    

      

    不努力,还要青春干什么?
  • 相关阅读:
    Dijkstra模版
    Trie树|字典树的简介及实现
    hdoj_2066一个人的旅行
    什么是java对象的强、软、弱和虚引用
    cxf调用客户端的方法
    CXF几种客户端调用性能
    csf几种调用的性能考虑
    cxf生成服务器端
    CXF在jdk1.6中运行异常解决
    CXF几种客户端调用性能
  • 原文地址:https://www.cnblogs.com/caidupingblogs/p/5264247.html
Copyright © 2020-2023  润新知