首页 > 其他分享 >【JSP开发】自己写的过滤器Filter例子

【JSP开发】自己写的过滤器Filter例子

时间:2023-03-19 12:01:50浏览次数:51  
标签:http request response Filter JSP 过滤器 import servlet javax


目的是让浏览网站的用户所接收到的信息的编码方式统一为UTF-8,防止乱码的产生


1.没加过滤器之前:

拿Jsp工程(名叫web)中的两个Servlet做实验

ChineseServlet.java:

package cn.edu.hpu.servlet;

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 ChineseServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data="我的中国心";

request.setAttribute("data", data);
request.getRequestDispatcher("/result").forward(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

}


}



ResultServlet.java


package cn.edu.hpu.servlet;

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 ResultServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String text=(String)request.getAttribute("data");
PrintWriter out=response.getWriter();

out.write("传过去的那句话是(The word get is):"+text);
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


}


}


在web.xml中配置:


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<servlet>
<servlet-name>ChineseServlet</servlet-name>
<servlet-class>cn.edu.hpu.servlet.ChineseServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>ResultServlet</servlet-name>
<servlet-class>cn.edu.hpu.servlet.ResultServlet</servlet-class>
</servlet>


<servlet-mapping>
<servlet-name>ChineseServlet</servlet-name>
<url-pattern>/chin</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ResultServlet</servlet-name>
<url-pattern>/result</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>



启动服务器,在地址栏上输入:http://localhost:8080/web/chin,得到:


?????????The word get is?:?????


出现乱码,说明没有指定编码。




解决办法:


两边servlet同时设置(这里以UTF-8为编码标准):


request.setCharacterEncoding("UTF-8");


response.setCharacterEncoding("UTF-8");




需要显示的时候:


String text=(String)request.getAttribute("data")+"是传过来的那句话";




System.out.println(text);


OutputStream out =response.getOutputStream(); 


out.write("<html>".getBytes());




//用html技术中meta标签模拟了一个http响应头,来控制浏览器的行为


out.write("<meta http-equiv='content-type' content='text/html;charset=UTF-8'>".getBytes());


out.write(text.getBytes("UTF-8"));


out.write("</html>".getBytes());


或者直接:


response.setContentType("text/html;charset=UTF-8");




以上方法很臃肿,而且非常不简介,无法应对多个界面,所以要使用过滤器来统一设置编码。



2.加过滤器之后:

ChineseServlet.java:

package cn.edu.hpu.servlet;


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 ChineseServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data="我的中国心";

request.setAttribute("data", data);
request.getRequestDispatcher("/result").forward(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


}


}


ResultServlet .java:


package cn.edu.hpu.servlet;


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 ResultServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String text=(String)request.getAttribute("data");
PrintWriter out=response.getWriter();

out.write("传过去的那句话是(The word get is):"+text);
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


}


}



过滤器:


package cn.edu.hpu.filter;

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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class TestFilter implements Filter {
private String characterEncoding=null;

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

}


public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterchain) throws IOException, ServletException {
request.setCharacterEncoding(characterEncoding);
response.setCharacterEncoding(characterEncoding);
response.setContentType("text/html;charset="+characterEncoding);

//获取URL的路径和参数
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
String path=req.getServletPath();
String param=req.getQueryString();
if(path!=null&¶m!=null){
path=path+"?"+param;//全请求路径
}
System.out.println("Path:"+path);
//跳转或带参数跳转
request.getRequestDispatcher(path).forward(request, response);
}


public void init(FilterConfig filterConfig) throws ServletException {
characterEncoding = filterConfig.getInitParameter("characterEncoding");

}


}



在web.xml中配置:


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<filter>
<filter-name>TestFilter</filter-name>
<filter-class>cn.edu.hpu.filter.TestFilter</filter-class>
<init-param>
<param-name>characterEncoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>TestFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


<servlet>
<servlet-name>ChineseServlet</servlet-name>
<servlet-class>cn.edu.hpu.servlet.ChineseServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>ResultServlet</servlet-name>
<servlet-class>cn.edu.hpu.servlet.ResultServlet</servlet-class>
</servlet>




<servlet-mapping>
<servlet-name>ChineseServlet</servlet-name>
<url-pattern>/chin</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ResultServlet</servlet-name>
<url-pattern>/result</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


启动服务器,在地址栏上输入:http://localhost:8080/web/chin,得到:


传过去的那句话是(The word get is):我的中国心


没有发生乱码,说明过滤成功!!



标签:http,request,response,Filter,JSP,过滤器,import,servlet,javax
From: https://blog.51cto.com/u_16012040/6131102

相关文章

  • 【JSP开发】URL路径的规范
    packagecn.edu.url;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjava......
  • 数据结构-布隆过滤器
    1.布隆过滤器的概念定义布隆过滤器:是⼀种概率型数据结构,特点是⾼效的插⼊和查询,能明确告知某个字符串⼀定不存在或者可能存在;优点和缺点优点:布隆过滤器相⽐传......
  • Filter过滤器
    Filter介绍Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp,Servlet,静态图片文件或静态html文件......
  • 排序组件的使用--OrderingFilter模块的使用
    1.排序组件(OrderingFilter)的用法:  2.响应结果的传参格式:ordering=字段名(不带‘-’是正序,带‘-’是倒序):  3.路由:  ......
  • 过滤组件的使用--django_filter模块实现多条件组合查询
    1.过滤组件的使用(实现条件查询)_第一步_安装django_filter:  2.过滤组件的使用(实现条件查询),第二步:注册django_filters:  3.过滤组件的使用(实现条件查询),第三步:过......
  • 【软件】基于JSP和Bootstrap的潇湘博客平台
    潇湘博客平台XiaoXiangBlog说明Eclipse项目-Version:2020-06(4.16.0)。JDK8。潇湘博客(XiaoXiangBlog)一个简单的JavaWeb项目,使用JSP、Servlet、MySQL、Boots......
  • ElasticSearch 实现分词全文检索 - filter查询
    目录ElasticSearch实现分词全文检索-概述ElasticSearch实现分词全文检索-ES、Kibana、IK安装ElasticSearch实现分词全文检索-Restful基本操作ElasticSearch......
  • Filter FilterChain FilterGraph
    基础filter:数据处理器filter_chain:链表链接链接所有的filterfilter_graph:数据处理管理器初始化过滤器创建graph//avfilter_register_all();//4.0后被删......
  • PebbleTemplates 自定义tag&filter&function 开发简单说明
    PebbleTemplates的扩展能力还是很强大的,开发起来也比较方便,以下是一个简单的说明,更加复杂的推荐查看官方源码学习filter开发参考代码此filter直接返回rongfenglian......
  • SSM jsp众筹平台
    SSMjsp众筹平台系统功能首页登录注册图片轮播新闻公告众筹资讯网站公告众筹项目发起众筹 在线留言关于我们合作伙伴后台管理首页系统用户管理新闻数据管......