servlet中的字符编码过滤器的使用

时间:2021-12-06 09:00:02

一:简介

Servlet过滤器是客户端和目标资源的中间层组件,主要是用于拦截客户端的请求和响应信息。如当web容器收到一条客户端发来的请求

web容器判断该请求是否与过滤器相关联,如果相关联就交给过滤器进行处理,处理完可以交给下一个过滤器或者其他业务,当其他业务完成

需要对客户端进行相应的时候,容器又将相应交给过滤器进行处理,过滤器处理完响应就将响应发送给客户端。

注意:上面话中的几个问题

1:web容器是如何判断请求和过滤器相关联。

2:过滤器是如何将处理完的请求交给其他过滤器的

前提知识:过滤器有三个接口

1:Filter接口中有三个方法

1>public void init(FilterConfig filterConfig)//这个参数中含有web.xml文件中的初始化参数,可以用来对请求进行处理

2>public void doFilter(ServletRequest request,ServletRequest response,FilterChain chain)//要注意第三个参数它主要是用来将处理完的请求发送到下一个过滤器

3>public void destroy()//销毁过滤器

2:FilterChain

1>void doFilter(ServletRequest request,ServletRequest response)

3:FilterConfig这个暂时不介绍

二:举例

字符编码过滤器:

1:创建字符编码过滤器类:

 public class FirstFilter implements Filter {
protected String encoding = null;
protected FilterConfig filter = null; /**
* Default constructor.
*/
public FirstFilter() {
// TODO Auto-generated constructor stub
} /**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
this.encoding = null;
this.filter = null; } /**
* @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
if(encoding != null)
{
request.setCharacterEncoding(encoding);
response.setContentType("text/html; charset="+encoding);
} // pass the request along the filter chai
chain.doFilter(request, response);//来将请求发送给下一个过滤器
} /**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {//初始化将web.xml中的初始化参数的信息赋给encoding
// TODO Auto-generated method stub
this.filter = fConfig;
this.encoding = filter.getInitParameter("encoding");
} }

2:在web.xml中配置相关信

   <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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>MyfirstServlet</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的配置信息-->
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>com.johnny.test.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/helloworld</url-pattern>
</servlet-mapping>
<!-- filter的配置信息-->
<filter>
<filter-name>FirstFilter</filter-name>
<filter-class>com.johnny.test.FirstFilter</filter-class>
<!-- 初始化参数-->
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>FirstFilter</filter-name>
<url-pattern>/*</url-pattern> <!--关联所有的url该使用该过滤器-->
<dispatcher>REQUEST</dispatcher><!--当客户端直接请求时进行过滤器处理-->
<dispatcher>FORWARD</dispatcher><!-- 当客户端用forward()方法请求时。。。。-->
</filter-mapping> </web-app>

3:login.jsp登录界面:

 <%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<!--action中放的是web.xml中映射servlet的url,它会直接将信息发送到servlet中进行处理-->
<form action = "helloworld" method = "post">
<table>
<tr>
<td> 登录名:</td><td><input type = "text" name = "usrname"></td>
</tr>
<tr>
<td>密码:</td><td><input type = "password" name = "password"></td> </tr> </table>
<input type = "submit" name = "submit" value = "登录">
</form>
</body>
</html>

4:编写servlet来进行处理login.jsp发来的信息

package com.johnny.test;

import java.io.IOException;
import java.io.PrintWriter; 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 HelloWorld
*/
@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public HelloWorld() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out = response.getWriter();
String name = request.getParameter("usrname");
if(name != null && !name.equals(""))
{
out.println("你好"+name);
out.println("欢迎来到英雄联盟");
//request.getRequestDispatcher("index.jsp").forward(request, response);
}
else
{
out.println("请输入用户名");
}
out.print("<br><a href = index.jsp>返回</a>");
out.flush();
out.close(); } }

5:index.jsp页面来处理用户没有输入信息的处理

<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body> <form action = "helloworld" method = "post">
<p>
请你输入你的中文名字:
<input type = "text" name = "usrname">
<input type = "submit" name = "submit">
</p> </form> </body>
</html>

6:上面的问题答案在web.xml和filter类中可以找到答案