实验五、过滤器与监听器
- 格式:doc
- 大小:48.50 KB
- 文档页数:7
第10章过滤器和监听器主要内容✓过滤器✓监听器1.过滤器从过滤器这个名字上可以得知就是在源数据和目标数据之间起到过滤作用的中间组件。
例如家里用的纯净水过滤器,将自来水过滤为纯净水。
在JSP的Web应用程序中,过滤器是一种在服务端运行的Web组件程序,它可以截取客户端给服务器发的请求,也可以截取服务器给客户端的响应。
图1 过滤器在Web程序中的位置当Web容器获得一个对资源的请求时,Web容器判断是否存在过滤器和这个资源关联。
如果有存在关联就把请求交给过滤器去处理,在过滤器中可以对请求的内容做出改变,然后再将请求转交给被请求的资源。
当被请求的资源做出响应时,Web容器同样会将响应先转发给过滤器,在过滤器中可以对响应做出处理然后再将响应发送给客户端。
在这整个过程中客户端和目标资源是不知道过滤器的存在的。
在一个Web应用程序中可以配置多个过滤器,从而形成过滤器链。
在请求资源时,过滤器链中的过滤器依次对请求作出处理。
在接受到响应时再按照相反的顺序对响应作出处理。
图2 过滤器链需要注意的是在过滤器中不一定必须将请求发送给被请求资源,也可以直接给客户端做出响应。
开发一个过滤器必须实现javax.servlet.Filter接口:代码演示:FristFilter.javapublic class FirstFilter implements Filter {public void init(FilterConfig arg0) throws ServletException { ①}public void destroy() { ②}public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException { ③}}代码分析:①由Web容器来调用init方法完成过滤器的初始化工作。
实验五:过滤器与监听器一、实验目的1.掌握过滤器的创建与配置方法;2.掌握监听器的创建与配置方法;二、实验内容2.1 创建Web项目1. 打开MyEclipse,创建一个Web Project,命名为ServletTest。
2.1 SecurityFilter详细过程见securityFilter.rmvb1.过滤器SecurityFilter的代码如下:package filters;import java.io.IOException;import java.io.PrintWriter;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;public class SecurityFilter implements Filter {private FilterConfig filterConfig;public void destroy() {}public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request;// 获得用户输入的密码String pwdInput = httpRequest.getParameter("password");// 获得filter配置参数中的rightpass的值String rightPwd = filterConfig.getInitParameter("rightpass");if (!rightPwd.equals(pwdInput)) {PrintWriter out = response.getWriter();out.println("<h3>Wrong password! Please try again.</h3>");out.flush();return;}filterChain.doFilter(request, response);}public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig;}}2.LoginServlet的主要代码如下:package servlets;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;import javax.servlet.http.HttpSession;public class LoginServlet extends HttpServlet {/*** Constructor of the object.*/public LoginServlet() {super();}/*** Destruction of the servlet. <br>*/public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}/*** The doGet method of the servlet. <br>** This method is called when a form has its tag value method equals to get.** @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();HttpSession session = request.getSession();session.setAttribute("username", request.getParameter("username"));out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.println("<h3>Welcome, " + request.getParameter("username") + ", you are logged in.</h3>");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}/*** The doPost method of the servlet. <br>** This method is called when a form has its tag value method equals to post.** @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.print(" This is ");out.print(this.getClass());out.println(", using the POST method");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}/*** Initialization of the servlet. <br>** @throws ServletException if an error occurs*/public void init() throws ServletException {// Put your code here}}3.新建login.jsp,用于进行登录测试,代码如下:<%@page language="java"import="java.util.*"pageEncoding="GBK"%><!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>登录页面</title></head><body><p>This example shows you how the security filter is used to protect the LoginServlet. Pleaseenter the password and click the submit button to see results(correct password is "hello"):</p><form action="servlet/LoginServlet"method="get"><input type="text"name="username"/> User Name<br/><input type="password"name="password"/> Password<br/><input type="submit"value="Login"/></form></body></html>4.测试运行:打开http://localhost:8080/ServletTest/login.jsp,输入正确的用户名与密码,查看网页结果回到login页面,输入错误的密码,再次查看网页结果。
第1篇一、实验目的1. 了解监听仪的基本原理和功能。
2. 掌握监听仪的使用方法和技巧。
3. 通过实验验证监听仪在音频信号处理中的应用。
二、实验器材1. 监听仪一台2. 音频信号发生器一台3. 音频功率放大器一台4. 音频线若干5. 麦克风一个6. 耳机一副7. 计时器一个三、实验原理监听仪是一种用于音频信号监测、处理和传输的设备。
它通过放大、滤波、均衡等处理,将原始音频信号转换为适合人耳听觉的信号。
本实验主要验证监听仪在音频信号处理中的应用,包括放大、滤波、均衡等功能。
四、实验步骤1. 连接实验器材将音频信号发生器、监听仪、音频功率放大器、耳机等设备按照图示连接好,确保所有连接正常。
2. 设置音频信号发生器打开音频信号发生器,设置输出频率为1kHz,输出幅度为0dB。
3. 调整监听仪将监听仪音量调至适中,打开均衡器,调整高低音平衡。
4. 进行实验(1)放大实验将音频信号发生器的输出信号接入监听仪的输入端,观察监听仪的输出幅度。
逐渐增加音频信号发生器的输出幅度,观察监听仪输出幅度随输入幅度变化的规律。
(2)滤波实验打开监听仪的滤波功能,设置滤波器类型为低通滤波器,截止频率为3kHz。
观察监听仪输出信号的频率响应,验证滤波效果。
(3)均衡实验打开监听仪的均衡功能,调整高低音平衡,观察监听仪输出信号的频响变化,验证均衡效果。
5. 实验数据记录记录实验过程中监听仪输出信号的幅度、频率响应、均衡效果等数据。
五、实验结果与分析1. 放大实验随着音频信号发生器输出幅度的增加,监听仪输出幅度也相应增加,验证了监听仪的放大功能。
2. 滤波实验开启低通滤波器后,监听仪输出信号中高于3kHz的频率成分被滤除,验证了监听仪的滤波功能。
3. 均衡实验调整高低音平衡后,监听仪输出信号的频响得到改善,验证了监听仪的均衡功能。
六、实验结论通过本次实验,我们了解了监听仪的基本原理和功能,掌握了监听仪的使用方法和技巧。
实验结果表明,监听仪在音频信号处理中具有放大、滤波、均衡等功能,能够有效改善音频信号质量。
过滤器创建一个Filter 只需两个步骤:(1)创建Filter 处理类:(2)在web.xml 文件中配置Filter 。
创建Filter 必须实现javax.servlet.Filter 接口,在该接口中定义了三个方法。
• void init(FilterConfig config): 用于完成Filter 的初始化。
• void destroy(): 用于Filter 销毁前,完成某些资源的回收。
• void doFilter(ServletRequest request, ServletResponse response,FilterChain chain): 实现过滤功能,该方法就是对每个请求及响应增加的额外处理。
过滤器Filter也具有生命周期:init()->doFilter()->destroy(),由部署文件中的filter元素驱动。
在servlet2.4中,过滤器同样可以用于请求分派器,但须在web.xml中声明,<dispatcher>INCLUDE或FORWARD或REQUEST或ERROR</dispatcher>该元素位于filter-mapping中。
一、理解Struts2拦截器1. Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现.解析:--------------------------------------------------------------------------------------------拦截器和AOP的关系拦截器与AOP (Aspect Orient Program ,面向切面编程)是密切相关的,AOP 从程序运行角度来考虑程序的流程,取得业务处理过程的切面,在特定切面通过系统自动插入特定方法。
案例1:统一设置字符集案例2:拦截URL,不登录不允许直接访问页面监听器监听器顾名思义就是监听某种事件的发生,即当某个事件发生时,就触发了某个设置好的监听器,但是这里触发的原因不同。
例如地震监测仪器监听地震的发生,大气监测仪,监测空气的变化以提供预警。
在servlet中因监听对象的不同也分为不同的监听器,这里指的监听对象是application、session对象,每种对象有各自的监听器。
1.application监听器:ServletContextListener需要实现的方法:(1)public void contextInitialized(ServletContextEvent e); //在application创建时就调用(2)public void contextDestroyed(ServletContextEvent e); //当application销毁时调用ServletContextEvent的getServletContext()方法可以取得application对象;创建完成监听器后需要在web.xml中做如下配置标签是单独出现的包含一个子标签,指定了监听类的全称(包名+类名)2.application属性监听器:ServletContextAttributeListener需要实现的方法:(1)public void attributeAdded(ServletContextAttributeEvent e); //当调用application.setAttribute()时调用(2)public void attributeRemoved(ServletContextAttributeEvent e); //当调用applcaition.removeAttribute()时调用(3)public void attributeReplaced(ServletContextAttributeEvent e); //当调用两次application.setAttribute()赋予相同属性时调用参数ServletContextAttributeEvent可以获得触发该监听器的属性名称和属性值,方法有:(1)getName(); 取得属性的名称;(2)getValue(); 取得属性的值;(注意:返回的是Object,必须转型)3.session监听器:HttpSessionListener需要实现的方法:(1)public void sessionCreated(HttpSessionEvent e); //当打开一个浏览器时,就会触发这个方法;(2)public void sessionDestroyed(HttpSessionEvent e); //当调用session.invalidate();或超时时调用HttpSessionEvent的方法getSession()获得触发监听器的session对象;销毁session常用的有2种方式:(1)session.invalidate();//直接销毁(2)在web.xml中设置超时时间://所有session起作用<session-config><session-timeout>5</session-timeout><!-- 5分钟 --></session-config>注意:如果会话超时时间设置为-1,则表示会话永远不会超时;4.session属性监听器:HttpSessionAttributeListener需要实现的方法:(1)public void attributeAdded(HttpSessionBindingEvent e); //当调用session.setAttribute()时调用(2)public void attributeRemoved(HttpSessionBindingEvent e); //当调用session.removeAttribute()时调用(3)public void attributeReplaced(HttpSessionBindingEvent e); //当调用两次session.setAttribute()赋予相同属性时调用HttpSessionBindingEvent 方法:(1)getSession();//获取触发监听器的session(2)getName();//获取属性名称(3)getValue();//获取属性值监听器应用场景 ServletContextListener:在任何Servlet提供服务之前执行、在Servlet销毁时执行,用于提前初始化一些资源,比如数据库连接、销毁一些资源,比如数据库连接;ServletContextAttributeListener:上下文中添加、删除、替换了属性;HttpSessionListener:多少个在线用户,即跟踪会话;HttpSessionAttributeListener:会话属性添加、删除、替换;综合案例:做一个网站在线人数统计,可以通过ServletContextListener监听,当Web应用上下文启动时,在ServletContext中添加一个List.用来准备存放在线的用户名,然后通过HttpSessionAttributeListener监听,当用户登录成功,把用户名设置到Session中。
实验五:过滤器与监听器一、实验目的1.掌握过滤器的创建与配置方法;2.掌握监听器的创建与配置方法;二、实验内容2.1 创建Web项目1. 打开MyEclipse,创建一个Web Project,命名为ServletTest。
2.1 SecurityFilter详细过程见securityFilter.rmvb1.过滤器SecurityFilter的代码如下:package filters;import java.io.IOException;import java.io.PrintWriter;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;public class SecurityFilter implements Filter {private FilterConfig filterConfig;public void destroy() {}public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request;// 获得用户输入的密码String pwdInput = httpRequest.getParameter("password");// 获得filter配置参数中的rightpass的值String rightPwd = filterConfig.getInitParameter("rightpass");if (!rightPwd.equals(pwdInput)) {PrintWriter out = response.getWriter();out.println("<h3>Wrong password! Please try again.</h3>");out.flush();return;}filterChain.doFilter(request, response);}public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig;}}2.LoginServlet的主要代码如下:package servlets;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;import javax.servlet.http.HttpSession;public class LoginServlet extends HttpServlet {/*** Constructor of the object.*/public LoginServlet() {super();}/*** Destruction of the servlet. <br>*/public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}/*** The doGet method of the servlet. <br>** This method is called when a form has its tag value method equals to get.** @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();HttpSession session = request.getSession();session.setAttribute("username", request.getParameter("username"));out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.println("<h3>Welcome, " + request.getParameter("username") + ", you are logged in.</h3>");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}/*** The doPost method of the servlet. <br>** This method is called when a form has its tag value method equals to post.** @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");out.print(" This is ");out.print(this.getClass());out.println(", using the POST method");out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}/*** Initialization of the servlet. <br>** @throws ServletException if an error occurs*/public void init() throws ServletException {// Put your code here}}3.新建login.jsp,用于进行登录测试,代码如下:<%@page language="java"import="java.util.*"pageEncoding="GBK"%><!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>登录页面</title></head><body><p>This example shows you how the security filter is used to protect the LoginServlet. Pleaseenter the password and click the submit button to see results(correct password is "hello"):</p><form action="servlet/LoginServlet"method="get"><input type="text"name="username"/> User Name<br/><input type="password"name="password"/> Password<br/><input type="submit"value="Login"/></form></body></html>4.测试运行:打开http://localhost:8080/ServletTest/login.jsp,输入正确的用户名与密码,查看网页结果回到login页面,输入错误的密码,再次查看网页结果。