Servlet过滤器

                                                              实验名称           实验六  Servlet过滤器                      

一、实验目的 

1. 了解过滤器的作用;

2. 掌握过滤器的开发与部署的步骤;

3. 了解过滤器链。

二、实验原理

过滤器是web服务器上的组件,它们对客户和资源之间的请求和响应进行过滤。

过滤器的工作原理是:当servlet容器接收到对某个资源的请求,它要检查是否有过滤器

与之关联。如果有过滤器与该资源关联,servlet容器将把该请求发送给过滤器。在过滤器处

理完请求后,它将做下面3件事:

•  产生响应并将其返回给客户;

•  如果有过滤器链,它将把(修改过或没有修改过)请求传递给下一个过滤器;

•  将请求传递给不同的资源。

当请求返回到客户时,它是以相反的方向经过同一组过滤器返回。过滤器链中的每个过

滤器够可能修改响应。

过滤器API主要包括:Filter、FilterConfig和FilterChain接口。

 

三、实验内容与步骤

编写一个过滤器改变请求编码。

【步骤1】编写一个loginform.html文件,代码如下:

<html>

<head>

<title>使用过滤器改变请求编码</title>

<meta http-equiv="Content-Type" content="text/html;charset=GB2312">

</head>

<body>

<center>

<h2>请输入用户名和口令:</h2>

<form method="post" action="servlet/CheckParamServlet">

<table>

<tr>

<td>用户名:</td>

<td><input name="name" type="text"></td>

</tr>

<tr>

<td>口  令:</td>

<td><input name="pass" type="password"></td>

</tr>

<tr>

<td></td>

<td>

<input name="ok" type="submit" value="提交">

<input name="cancel" type="reset" value="重置">

</td>

</tr>

</table>

</form>

</center>

</body>

</html>

【步骤2】编写处理请求参数的Servlet,代码如下:

packet servlet;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class CheckParamServlet extends HttpServlet{

public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

String name = request.getParameter("name");

String pass = request.getParameter("pass");

response.setContentType("text/html;charset=gb2312");

PrintWriter out = response.getWriter();

out.println("<html><head><title>Param Test</title></head>");

out.println("<h3 align=center>你的用户名为:"+name+"</h3>");

out.println("<h3 align=center>你的口令为:"+pass+"</h3>");

out.println("</body></html>");

}

public void doPost(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

doGet(request,response);

}

}

【步骤3】修改web.xml文件,加入下面代码:

<servlet>

<servlet-name>CheckParamServlet</servlet-name>

<servlet-class>CheckParamServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>CheckParamServlet</servlet-name>

<url-pattern>/servlet/check</url-pattern>

</servlet-mapping>

【步骤4】在浏览器的地址栏中输入下面URL:

http://localhost:8080/ helloapp/loginform.html

下面通过编写一个过滤器改变请求编码。

【步骤5】过滤器代码如下:

package filter;

import java.io.IOException;

import javax.servlet.*;

public class EncodingFilter implements Filter {

protected String encoding = null;

protected FilterConfig config;

public void init(FilterConfig filterConfig) throws ServletException {

this.config = filterConfig;

// 得到在web.xml中配置的编码

this.encoding = filterConfig.getInitParameter("Encoding");

}

public void doFilter(

ServletRequest request,

ServletResponse response,

FilterChain chain)

throws IOException, ServletException {

if (request.getCharacterEncoding() == null) {

// 得到指定的编码

String encode = getEncoding();

if (encode != null) {

//设置request的编码

request.setCharacterEncoding(encode);

response.setCharacterEncoding(encode);

}

}

chain.doFilter(request, response);

}

protected String getEncoding() {

return encoding;

}

public void destroy() {

}

}

【步骤6】在web.xml文件中配置过滤器,加入下面代码:

<filter>

<filter-name>EncodingFilter</filter-name>

<filter-class>filter.EncodingFilter</filter-class>

<init-param>

<param-name>Encoding</param-name>

<param-value>gb2312</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>EncodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

【步骤7】重复第(4)步操作,结果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ifHappyEveryDay@

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

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

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

打赏作者

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

抵扣说明:

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

余额充值