Java web 过滤器的简单使用
一、实验目的:
实验课:按课程讲解创建过滤器,并完成相关配置,
使页面1和页面2都使用过滤器,假设页面1为登录页面,页面2为业务页面,
在过滤器中验证有效登录信息,如果没有登录则可以提示并跳转到登录页面。
二、实验代码
目录结构
DealWithServelt.java (业务示例)
package com.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class DealWithServelt extends HttpServlet {
private String stuname ;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
stuname= req.getParameter("stuname");
System.out.println(stuname);
}
}
filter.java(过滤器示例)
package com.filter;
import javax.servlet.*;
import java.io.IOException;
public class filter implements javax.servlet.Filter {
private String name ;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
/*
* 这个方法在路由跳转的是否会执行
* */
System.out.println("过滤器使用中");
String stuname = request.getParameter("stuname");
if (getName().equals(stuname)) {
System.out.println("欢迎张三的到来");
request.getRequestDispatcher("./filter.jsp").forward(request,response);
}else{
System.out.println("你不是张三");
request.getRequestDispatcher("/index.jsp").forward(request,response);
}
}
public void init(FilterConfig config) throws ServletException {
// 这个方法在部署前会提前方法
setName("zhansan");
System.out.println("method had benn doing");
}
}
index.jsp 表单设计
<form action="dealservlet" method="post">
<input type="text" name="stuname"><br>
<input type="submit" value="search">
</form>
filter.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2022/4/21
Time: 10:15
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>过滤器调试</title>
</head>
<body>
<h1 style="text-align: center"> 这是张三的家</h1>
</body>
</html>
配置 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<filter>
<filter-name>Filter</filter-name>
<filter-class>com.filter.filter</filter-class>
</filter>
<filter-mapping>
<filter-name>Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>Deal</servlet-name>
<servlet-class>com.Servlet.DealWithServelt</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Deal</servlet-name>
<url-pattern> /dealservlet</url-pattern>
</servlet-mapping>
</web-app>
显示结果:
method had benn doing
[2022-04-21 12:32:16,134] Artifact program_mybits:war exploded: 工件已成功部署
[2022-04-21 12:32:16,134] Artifact program_mybits:war exploded: Deploy took 3,002 milliseconds
过滤器使用中
欢迎张三的到来
三、 实验截图
zansan 登陆名正确页面
四、实验报错
运行报错的可以改下这个,具体那个报错异常的说明,我修复好了然后不知道怎么把他改成报错的,我的方案就是改下这个然后就成功了,玄学不管.
解决 SpringMVC 中的 java.lang.UnsupportedClassVersionError Preview features are not enabled
解决办法:https://blog.csdn.net/qq_45679819/article/details/107516415
目前尚不明白原因,但是以后遇到此类问题,可以尝试设置不同的语言级别
五、实验总结
<filter>
<filter-name>Filter</filter-name>
<filter-class>com.filter.filter</filter-class>
</filter>
<filter-mapping>
<filter-name>Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>