过滤器Filter(拦截jsp页面的跳转)案例:
创建一个 Filter , class类: 其继承于 接口 Filte(接口导包:import javax.servlet.Filter;)
在 web.xml 文件中配置并映射该 Filter. 其中 url-pattern 指定该 Filter 可以拦截哪些资源, 即可以通过哪些 url 访问到该 Filter,并进行拦截;
案例:username=Tom,password=1234,设计Filter类,及jsp页面实现,输入username和password是否等于Tom和1234,不等拦截index.jsp页面跳转到hello.jsp页面,等的或,在在hello,显示;
1.建立UserNameFilter类:继承于接口Filter(接口导包:import javax.servlet.Filter;)
package com.lanqiao.javatest; 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 org.apache.catalina.connector.Request; public class UserNameFilter implements Filter{ //构造方法
public UserNameFilter(){ }
public void destroy() { } @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
String initUser=filterConfig.getInitParameter("username");
String username=request.getParameter("username"); if(!initUser.equals(username)){
request.setAttribute("message", "用户名不正确!!!");
request.getRequestDispatcher("/login.jsp").forward(request, response);
return ;
}
chain.doFilter(request, response); } private FilterConfig filterConfig;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig=filterConfig; } }
2.建立PasswordFilter类:继承于接口Filter(接口导包:import javax.servlet.Filter;)
package com.lanqiao.javatest; 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 PasswordFilter implements Filter { public PasswordFilter() {
// TODO Auto-generated constructor stub
}
@Override
public void destroy() { } @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
String initPassword=filterConfig.getServletContext().getInitParameter("password");
String password=request.getParameter("password");
if(!initPassword.equals(password)){
request.setAttribute("message", "密码不正确!!!");
request.getRequestDispatcher("/login.jsp").forward(request, response);
return ;
}
chain.doFilter(request, response); } private FilterConfig filterConfig;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig=filterConfig; } }
3.在WEB-FIN下的web.xml文件下的配置和映射:
<?xml version="1.0" encoding="UTF-8"?>
<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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>day-12</display-name> <filter>
<filter-name>UserNameFilter</filter-name>
<filter-class>com.lanqiao.javatest.UserNameFilter</filter-class>
<init-param>
<param-name>username</param-name>
<param-value>Tom</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>UserNameFilter</filter-name>
<url-pattern>/hello.jsp</url-pattern>
</filter-mapping> <filter>
<filter-name>password</filter-name>
<filter-class>com.lanqiao.javatest.PasswordFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>password</filter-name>
<url-pattern>/hello.jsp</url-pattern>
</filter-mapping>
<context-param>
<param-name>password</param-name>
<param-value>1234</param-value>
</context-param> </web-app>
4.index.jsp页面:输入username和password;
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<font color="red">${message }</font>
<br><br>
<form action="hello.jsp" method="post">
username:<input type="text" name="username" /><br>
password:<input type="password" name="password"/><br>
<input type="submit" value="Submit"/>
</form> </body>
</html>
5.hello.jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
Hello:${param.username }
</body>
</html>
过滤器Filter(拦截jsp页面的跳转)案例:的更多相关文章
- springmvc登录拦截jsp页面
web.xml配置 <filter> <filter-name>LoginFilter</filter-name> //编写拦截的类的全类名 <filter- ...
- 过滤器会拦截 前端页面加载 js文件的请求
学艺不精啊.....之前就总结过博客: JAVA中解决Filter过滤掉css,js,图片文件等问题 结果现在又犯了老错误~ 情况如下: index.jsp 页面的验证码输入栏绑定了异步验证(jQur ...
- JSP页面的跳转及传值
1.response.sendRedirect("跳转到页面的URL"); 该方法通过修改HTTP协议的HEADER部分,对浏览器下达重定向指令的,使浏览器显示重定向网页的内容. ...
- j2ee中如何拦截jsp页面?
加filter: public class RightFilter implements Filter { public void init(FilterConfig filterConfig) th ...
- Spring Boot jsp页面无法跳转问题
可能的情况如下: 1.未在pom.xml中添加依赖 <!-- jsp 视图支持--> <dependency> <groupId>org.apache.tom ...
- jsp页面的跳转取值
<p >工单管理 >> <c:if test="${code eq 0}">全部工单>>详情页</c:if> <c ...
- springmvc 拦截器,不拦截jsp文件
spring mvc的拦截器只拦截controller不拦截jsp文件,如果不拦截jsp文件也会给系统带安全性问题. 解决方案有两种: 1.将所有的jsp文件放入到WEB-INF文件夹下,这样用户是直 ...
- SpringBoot入坑指南之六:使用过滤器或拦截器
在Web应用中,常常存在拦截全部或部分请求进行统一处理的应用场景,如权限校验.参数校验.性能监控等. 在SpringMVC框架中,我们可以通过过滤器或拦截器实现相关功能,spring-boot-sta ...
- SpringBoot项目中加入jsp页面
根据我们之前搭建好的SpringBoot+SSm的项目的基础上,来增加webapp/WEB-INF的文件,由此来完成jsp页面的跳转. 先增加jsp的pom依赖: <!-- https://mv ...
随机推荐
- MySQL半同步复制的安装和配置
(1)检查master/slave是否支持动态加载插件 > show variables like 'have_dynamic_loading'; +---------------------- ...
- Java基础之访问文件与目录——获取与文件存储有关的信息(GetFileStores)
控制台程序,列出存储在系统中的文件的详细信息 import java.nio.file.FileStore; import java.nio.file.FileSystems; import java ...
- 理解JDBC和JNDI
下面的英文是我找过来的,因为是英文所以不敢翻译出来误导别人,但是它描述的确实恰到好处,比所谓网上的JNDI和JDBC云云的解释要精辟很多,如果遇到不认识的单词,用有道吧~~:) The Java Na ...
- 树链剖分(单点更新,求区间最值,区间求和Bzoj1036)
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MB Submit: 5759 Solved: 2383 [Submi ...
- Java基础(7):二维数组初始化时需要注意的问题
二维数组可以先指定行,再指定列:但不能先指定列,再指定行 没有说明二维数组的行的个数,在定义二维数组时也可以只指定行的个数,然后再为每一行分别指定列的个数.如果每行的列数不同,则创建的是不规则的二维数 ...
- c++之路进阶——codevs4543(普通平衡树)
4543 普通平衡树 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 这是一道水题 顺便祝愿LEZ和ZQQ 省 ...
- sql xpath 查找包含
select xcontent.query('/root//*[contains(text()[1], ''中'')]'), column1 from table
- linux c
#include <stdio.h>#include <string.h>#include <strings.h> int main(){ char buf[ ...
- angularjs---$http.post发的数据,后台取不到
我用$http.post(url, data). 后台用play框架,不知道为什么总是取不到data数据.如果直接用$.post(url, data); 就可以! 后台Play的一个action: 打 ...
- Fatal error: Call to a member function bind_param() on a non-object in
今天在练习 mysql是出现错误: Fatal error: Call to a member function bind_param() on a non-object in 解决步骤: 1. ...