8、JSP
8.1、什么是JSP
Java Server Pages : Java服务器端页面,也和Servlet一样,用于动态Web技术!
最大的特点:
- 写JSP就像在写HTML
- 区别:
- HTML只给用户提供静态的数据
- JSP页面中可以嵌入JAVA代码,为用户提供动态数据;
8.2、JSP原理
思路:JSP到底怎么执行的!
-
代码层面没有任何问题
-
服务器内部工作
tomcat中有一个work目录;
IDEA中使用Tomcat的会在IDEA的tomcat中生产一个work目录地址为:C:\Users\59478\AppData\Local\JetBrains\IntelliJIdea2020.1\tomcat\Unnamed_javaweb-session-cookie\work\Catalina\localhost\ROOT\org\apache\jsp
发现页面转换成了Java程序:
浏览器向服务器发送请求,不管访问什么资源,其实都是在访问Servlet!
JSP最终也会被转换成为一个Java类!
JSP本质上就是一个Servlet!
-
index_jsp.java内容:
public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
导入jasper.jar包,找到HttpJspBase,发现:
public abstract class HttpJspBase extends HttpServlet
因此说JSP本质上就是一个Servlet
//初始化 public void _jspInit() { } //销毁 public void _jspDestroy() { } //jspService public void _jspService(HttpServletRequest request, HttpServletResponse response)
jspService做了哪些事情?
(1)判断请求
(2)内置了一些对象
final javax.servlet.jsp.PageContext pageContext; //页面上下文 javax.servlet.http.HttpSession session = null; final javax.servlet.ServletContext application; //applicationContext final javax.servlet.ServletConfig config; javax.servlet.jsp.JspWriter out = null; final java.lang.Object page = this; //当前页 HttpServletRequest request //请求 HttpServletResponse response //响应
(3)输出页面前增加的代码
response.setContentType("text/html;charset=UTF-8");//设置页面的响应类型 pageContext = _jspxFactory.getPageContext(this, request, response, null, true, 8192, true); _jspx_page_context = pageContext; application = pageContext.getServletContext(); config = pageContext.getServletConfig(); session = pageContext.getSession(); out = pageContext.getOut(); _jspx_out = out;
(4)以上的这些个对象我们可以在JSP页面中直接使用
访问JSP流程:
在JSP页面中:
-
只要是JAVA代码就会原封不动的输出;
-
如果是HTML代码,就会被转换为:
out.write("<html>\r\n");
这样的格式,输出到前端!
8.3、JSP基础语法
任何语言都有自己的语法,JSP作为java技术的一种应用,它拥有一些自己扩充的语法(了解,知道即可!) , Java所有语法都支持!
JSP表达式
<%--JSP表达式
作用:用来将程序的输出,输出到客户端
形式:<%= %>
--%>
<%= new java.util.Date() %>
JSP脚本片段
<%--JSP脚本片段--%>
<%
int sum = 0;
for (int i = 0; i < 100; i++) {
sum+=i;
}
out.println("<h1>"+sum+"</h1>");
%>
脚本片段的再实现
<%
int x = 10;
out.println(x);
%>
<p>这是一个JSP文档</p>
<%
int y = 20;
out.println(y);
%>
<%--在代码中嵌入HTML元素--%>
<%
for (int i = 0; i < 5; i++) {
%>
<h1>Hello,world <%=i%></h1>
<%
}
%>
JSP声明
<%!
static {
System.out.println("Loading Servlet!");
}
private int globalVar = 0;
public void kuang(){
System.out.println("进入kuang");
}
%>
区别:
- JSP声明会被编译到SP生成ava的类中!其他的,就会被生成到_jspService方法中!
总结:JSP语法
脚本程序: <% 代码片段 %>
JSP声明: <%! declaration %>
JSP表达式:<%= 表达式 %>
JSP注释:<%-- 注释 --%>
JSP的注释,不会在客户端显示,但是HTML就会!
8.4、JSP指令
(1)定制错误页面
方法一:
首先是jsp2.jsp:
<%--定制错误页面--%>
<%@ page errorPage="error/500.jsp" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
int i = 1/0;
%>
</body>
</html>
然后是500.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--当前500.jsp所在目录是error,而500错误的图片在image,需要先..回到上级目录--%>
<img src="../image/500.png" alt="500">
</body>
</html>
需要在web文件夹下创建500.png
注:图片不显示问题
删除idea中out文件夹,重新启动即可。
网上找的解决办法:
在tomcat的conf文件下面的server.xml加上:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
disableUploadTimeout="true"//新添加
URIEncoding="utf-8"//新添加
redirectPort="8443" />
方法二:
将jsp2.jsp中<%@ page errorPage=“error/500.jsp” %>注销掉,使用web.xml来跳转到500.jsp
web.xml
<error-page>
<error-code>500</error-code>
<location>/error/500.jsp</location>
</error-page>
(2)定制网页头部尾部
头部jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<h1>我是header</h1>
尾部jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<h1>我是footer</h1>
方法一:
jsp3.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--@include会将页面都合并到一起显示,三合一--%>
<%@include file="common/header.jsp"%><%--网页头部--%>
<h1>网页主体</h1>
<%@include file="common/footer.jsp"%><%--网页尾部--%>
<hr>
</body>
</html>
方法二:
jsp3.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--
JSP标签
jsp:include 作用:拼接页面,本质上还是三个页面
更常使用,因为灵活性高
--%>
<jsp:include page="/common/header.jsp"/>
<h1>网页主体</h1>
<jsp:include page="/common/footer.jsp"/>
</body>
</html>
注:
当头部或者尾部jsp文件中跟方法一中有冲突时,运行访问页面会报500错误,而方法二则不会
如:尾部jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<h1>我是footer</h1>
<%int i = 10;%>
方法一中jsp3.jsp变为:
<%@include file="common/header.jsp"%><%--网页头部--%>
<h1>网页主体</h1>
<%int i = 10;%>
<%@include file="common/footer.jsp"%><%--网页尾部--%>
此时访问jsp3.jsp就会报错,但同样将<%int i = 0;%>添加到方法二中同样位置则能正常显示
8.5、9大内置对象
- PageContext 存东西
- Request 存东西
- Response
- session 存东西
- Application 【SerlvetContext) 存东西
- config 【SerlvetConfig】
- out
- page 几乎不用
- exception
使用场景:
//从底层到高层(作用域):pageContext->request->session->application
pageContext.setAttribute("name1","秦疆1号");//保存的数据只在一个页面中有效
request.setAttribute("name2" , "秦疆2号");//保存的数据只在一次请求中有效,请求转发会携带这个数据
session.setAttribute("name3","秦疆3号");//保存的数据只在一次会话中有效,从打开浏觉器到关闭泌览器
application.setAttribute( "name4","秦疆4号");//保存的数据只在服务器中有效,从打开服务器到关闭服务器
request:客户端向服务器发送请求,产生的数据,用户看完就没用了,比如:新闻,用户看完没用的!
session:客户端向服务器发送请求,产生的数据,用户用完一会还有用, 比如:购物车;
application:客户端向服务器发送请求,产生的数据,一个用户用完了,其他用户还可能使用,比如:聊天数
据;
代码实例:
pageContextDemo01.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--内置对象--%>
<%
//从底层到高层(作用域):pageContext->request->session->application
pageContext.setAttribute("name1","秦疆1号");//保存的数据只在一个页面中有效
request.setAttribute("name2" , "秦疆2号");//保存的数据只在一次请求中有效,请求转发会携带这个数据
session.setAttribute("name3","秦疆3号");//保存的数据只在一次会话中有效,从打开浏觉器到关闭泌览器
application.setAttribute( "name4","秦疆4号");//保存的数据只在服务器中有效,从打开服务器到关闭服务器
%>
<%--脚本片段中的代码,会被原封不动生成到.JSP.java
要求:这里面的代码:必须保证Java语法的正确性
)--%>
<%
//从pageContext取出,我们通过寻找的方式来
//从底层到高层(作用域):pageContext->request->session->application
String name1 = (String) pageContext.findAttribute("name1");
String name2 = (String) pageContext.findAttribute("name2");
String name3 = (String) pageContext.findAttribute("name3");
String name4 = (String) pageContext.findAttribute("name4" );
String name5 = (String) pageContext.findAttribute("name5"); //不存在
%>
<%--使用EL衣达式输出${} --%>
<h1>取出的值为: </h1>
<h3>${name1}</h3>
<h3>${name2}</h3>
<h3>${name3}</h3>
<h3>${name4}</h3>
<h3>${name5}</h3> <%--界面会直接过滤掉不存在的name5--%>
<h3> <%=name5%> </h3> <%--界面会显示null,难看,推荐上述方法--%>
</body>
</html>
pageDemo02.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--脚本片段中的代码,会被原封不动生成到.JSP.java
要求:这里面的代码:必须保证Java语法的正确性
)--%>
<%
//从pageContext取出,我们通过寻找的方式来
/*
* 从底层到高层(作用域),先访问pageContextDemo01.jsp,会产生数据,再访问pageDemo02.jsp,会发现
* 页面中只有name3和name4
* */
String name1 = (String) pageContext.findAttribute("name1");
String name2 = (String) pageContext.findAttribute("name2");
String name3 = (String) pageContext.findAttribute("name3");
String name4 = (String) pageContext.findAttribute("name4" );
String name5 = (String) pageContext.findAttribute("name5"); //不存在
%>
<%--使用EL衣达式输出${} --%>
<h1>取出的值为: </h1>
<h3>${name1}</h3>
<h3>${name2}</h3>
<h3>${name3}</h3>
<h3>${name4}</h3>
<h3>${name5}</h3> <%--界面会直接过滤掉不存在的name5--%>
<h3> <%=name5%> </h3> <%--界面会显示null,难看,推荐上述方法--%>
</body>
</html>
pageDemo03.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--
scope:作用域
public static final int PAGE_SCOPE = 1;
public static final int REQUEST_SCOPE = 2;
public static final int SESSION_SCOPE = 3;
public static final int APPLICATION_SCOPE = 4;
public void setAttribute(String name, Object attribute, int scope) {
switch(scope) {
case 1:
this.mPage.put(name, attribute);
break;
case 2:
this.mRequest.put(name, attribute);
break;
case 3:
this.mSession.put(name, attribute);
break;
case 4:
this.mApp.put(name, attribute);
break;
default:
throw new IllegalArgumentException("Bad scope " + scope);
}
--%>
<%
pageContext.setAttribute("hello1","hello1",PageContext.SESSION_SCOPE);
//上面的代码等价于session.setAttribute("hello1","hello1");
%>
</body>
</html>
pageContextDemo04.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
//页面跳转
pageContext.forward("/index.jsp");
//等同于request.getRequestDispatcher("/index.jsp").forward(request,response);
%>
</body>
</html>
8.6、JSP标签、JSTL标签、EL表达式
8.6.1、EL表达式:
重点是前三个,最后一个不重要
- 获取数据
- 执行运算
- 获取web开发的常用对象
调用java方法
8.6.2、JSP标签
jsptag.jsp
<body>
%--<jsp:include>--%>
<jsp:forward page="/jsptag2.jsp">
<jsp:param name="name" value="kaungshen"/>
<jsp:param name="age" value="12"/>
</jsp:forward>
<%--等同于访问时路径:http://localhost:8080/jsptag.jsp?name=kaungshen&age=12--%>
</body>
jsptag2.jsp
<body>
<%--取出参数--%>
名字:<%=request.getParameter("name")%><br>
年龄:<%=request.getParameter("age")%><br>
</body>
8.6.3、JSTL标签
JSTL标签库的使用就是为了弥补HTML标签的不足;它自定义许多标签,可以供我们使用,标签的功能和java代码一样!分为:
-
核心标签(掌握部分)
步骤:
- (1)引用核心标签库的语法如下:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- (2)使用下面的语法
-
(3)在tomcat中也要引入JSTL的包,否则会报错
案例:
if标签
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--引用 JSTL核心标签库,我们才能使用 JSTL标签--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h4>if测试</h4>
<hr>
<form action= "coreif.jsp" method= "get">
<%--
EL表达式获取表单中的数据
${param.参数名}
--%>
<input type="text" name="username" value="${param.username}">
<input type="submit" value="登录">
</form>
<%--.判断如果提交的用户名是管理员,则登录成功--%>
<c:if test="${param.username=='admin'}" var="isAdmin">
<c:out value="管理员欢迎您! "/>
</c:if>
<c:out value=" ${isAdmin}"/>
</body>
</html>
when标签
**注:**when标签是从上到下截断式判断,当test=“${score>=80}” 在test="{score>=90}"前面时,输出结果则变为:你的成绩为一般
<body>
<%--定义一个变量score,值为95--%>
<c:set var="score" value="95"/>
<c:choose>
<c:when test="${score>=90}">
你的成绩为优秀
</c:when>
<c:when test="${score>=80}">
你的成绩为一般
</c:when>
<c:when test="${score>=70}">
你的成绩为良好
</c:when>
<c:when test="${score<=60}">
你的成绩为不及格
</c:when>
</c:choose>
</body>
foreach标签
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
ArrayList<String> people = new ArrayList<>();
people.add(0,"张三");//下标从0开始
people.add(1,"李四");
people.add(2,"王五");
people.add(3,"赵六");
people.add(4,"田六");
request.setAttribute("list",people);
%>
<%--
var:每一次遍历出来的变量,类似于 for (String person : people) {}
items:要遍历的对象
begin:哪里开始
end:到哪里
step:步长
--%>
<c:forEach var="people" items="${list}">
<c:out value="${people}"/> <br>
</c:forEach>
<hr>
<c:forEach var="people" items="${list}" begin="1" end="3" step="2" >
<c:out value="${people}"/> <br>
</c:forEach>
</body>
</html>
- 格式化标签(不常用)
- SQL标签(不常用)
- XML标签(不常用)
9、JavaBean
实体类
JavaBean有特定的写法:
- 必须要有一个无参构造
- 属性必须私有化
- 必须有对应的get/set方法;
一般用来和数据库的字段做映射 ORM;
ORM:对象关系映射
-
表–>类
-
字段–>属性
-
行记录—>对象
people表
id name age address 1 秦疆1号 3 西安 2 秦疆2号 18 西安 3 秦疆3号 111 西安 class Peop1e{ private int id; private string name ; private int id; private String address; } class A{ new Peop1e(1,"秦疆1号",3, "西安"); new Peop1e(2,"秦疆2号",3, "西安"); new Peop1e(3,"秦疆3号",3, "西安"); }
表与代码字段一一对应
javabean.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <jsp:useBean id="people" class="com.kuang.pojo.People" scope="page"/><%--等价于People people = new People();--%> <jsp:setProperty name="people" property="address" value="西安"/><%--people.setAddress();--%> <jsp:setProperty name="people" property="id" value="1"/><%--people.setId();--%> <jsp:setProperty name="people" property="age" value="3"/><%--people.setAge();--%> <jsp:setProperty name="people" property="name" value="小狂神呀"/><%--people.setName();--%> 姓名: <jsp:getProperty name="people" property="name"/> id: <jsp:getProperty name="people" property="id"/> 年龄: <jsp:getProperty name="people" property="age"/> 地址: <jsp:getProperty name="people" property="address"/> </body> </html>
10、MVC三层结构
什么是MVC :Model View Controller 模型、视图、控制器
10.1、早些年的架构
用户直接访问控制层,控制层就可以直接操作数据库;
servlet–CRUD–>数据库
弊端:程序十分臃肿,不利于维护
servlet的代码中:处理请求、响应、视图跳转、处理JDBC、处理业务代码、处理逻辑代码架构:没有什么是加一层解决不了的!
程序猿调用—>JDBC—>Mysql oracle sqlServer …
10.2、MVC三层结构
Model
- 业务处理: 业务逻辑(Service)
- 数据持久层: CRUD (Dao)
View
- 展示数据
- 提供链接发起Servlet请求(a链接, form, img…)
Controller (Servlet)
- 接收用户的请求: (req: 请求参数、Session信息…)
- 交给业务层处理对应的代码
- 控制视图的跳转
登录--->接收用户的登录请求--->处理用户的请求(获取用户登录的参数:username, password) ----> 交给业务层处理登录业务(判断用户名密码是否正确:事务)--->Dao层查询用户名和密码是否正确---->数据库
11、Filter(重点)
Filter:过滤器,用来过滤网站的数据;
- 处理中文乱码
- 登录验证…
Filter开发步骤:
1.导包
2.编写过滤器
(1)导包不要错
(2)实现Filter接口,重写对应的方法即可
ShowServlet.java
public class ShowServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//中文乱码问题
//方法一:resp.setCharacterEncoding("utf-8");
resp.getWriter().write("你好,世界!");
//方法二:编写一个过滤器,在过滤器中设置中文乱码问题,并在web.xml中设置过滤路径
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
过滤器:CharacterEncodingFilter
package com.kuang.filter;
import javax.servlet.*;
import java.io.IOException;
public class CharacterEncodingFilter implements Filter {
//初始化: web服务器启动,就已经初始化了,随时等待过滤对象出现!
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("CharacterEncodingFilter初始化");
}
//chain:链
/*
1.过滤中的所有代码,在过滤特定请求的时候都会执行
2.必须要让过滤器继续同行
chain. doFilter(request, response);
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
System.out.println("CharacterEncodingFilter执行前....");
chain.doFilter(request,response);//让我们的请求继续走,如果不写,程序到这里就被拦截停止!
System.out.println("CharacterEncodingFilter执行后....");
}
//销毁: web服务器关闭的时候,过滤会销毁
public void destroy() {
System.out.println("CharacterEncodingFilter销毁");
}
}
(3)web.xml中配置filter
<?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">
<servlet>
<servlet-name>ShowServlet</servlet-name>
<servlet-class>com.kuang.servlet.ShowServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ShowServlet</servlet-name>
<url-pattern>/servlet/show</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ShowServlet</servlet-name>
<url-pattern>/show</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.kuang.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<!--只要是/servlet的任何请求,都会经过这个过滤器-->
<url-pattern>/servlet/*</url-pattern>
</filter-mapping>
</web-app>
此时,页面访问时,http://localhost:8080/show出来的就是乱码,而http://localhost:8080/servlet/show则是ShowServlet.java中的要输出的中文。
12、监听器
实现一个监听器的接口; (有N种)
(1)编写一个监听器
实现监听器的接口…
package com.kuang.listener;
//统计网站在线人数:即统计session
public class OnlineCountListener implements HttpSessionListener {
//创建session监听
//一旦创建session就会触发一次这个事件!
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
ServletContext ctx = httpSessionEvent.getSession().getServletContext();
System.out.println(httpSessionEvent.getSession().getId());
Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");
if (onlineCount == null) {
onlineCount = new Integer(1);
} else {
int count = onlineCount.intValue();
onlineCount = new Integer(count + 1);
}
ctx.setAttribute("OnlineCount", onlineCount);
}
//销毁session监听
//一旦销毁session就会触发一次这个事件!
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
ServletContext ctx = httpSessionEvent.getSession().getServletContext();
Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");
if (onlineCount == null) {
onlineCount = new Integer(0);
} else {
int count = onlineCount.intValue();
onlineCount = new Integer(count - 1);
}
ctx.setAttribute("OnlineCount", onlineCount);
}
}
(2)在web.xml中注册监听器
<!--注册监听器-->
<listener>
<listener-class>com.kuang.listener.OnlineCountListener</listener-class>
</listener>
<session-config>
<session-timeout>1</session-timeout>
</session-config>
13、过滤器常见应用
用户登录之后才能进入主页!用户注销后就不能进入主页了!
- 用户登录之后,向Sesison中放入用户的数据:
- 进入主页的时候要判断用户是否已经登录;
代码:
login.jsp
<h1>登陆</h1>
<form action="/servlet/login" name="post">
<input type="text" name="username">
<input type="submit">
</form>
</body>
success.jsp(在web/sys目录下)
<body>
<%--在写SysFilter之前,可以用以下代码让页面点击注销之后不能再访问该success.jsp
<%
Object userSession = request.getSession().getAttribute("USER_SESSION");
if (userSession==null){
//pageContext.forward("login.jsp");
response.sendRedirect("/login.jsp");
}
%>--%>
<h1>主页</h1>
<p><a href="/servlet/logout">注销</a> </p>
</body>
error.jsp
<body>
<h1>错误</h1>
<h3>没有权限或者密码错误</h3>
<a href="/login.jsp">返回登陆页面</a>
</body>
LoginServlet.java
package com.kuang.servlet;
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取前端请求的参数
String username = req.getParameter("username");
if (username.equals("admin")) { //登录成功
req.getSession().setAttribute("USER_SESSION", req.getSession().getId());
resp.sendRedirect("/sys/success.jsp");
} else { //登录失败
resp.sendRedirect("/error.jsp");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
LogoutServlet.java
package com.kuang.servlet;
public class LogoutServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Object user_session = req.getSession().getAttribute("USER_SESSION");
if (user_session != null) {
req.getSession().removeAttribute("USER_SESSION");
resp.sendRedirect("/login.jsp");
}else {
resp.sendRedirect("/login.jsp");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
过滤器:SysFilter.java
package com.kuang.filter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class SysFilter implements Filter {
//初始化: web服务器启动,就已经初始化了,随时等待过滤对象出现!
public void init(FilterConfig filterConfig) throws ServletException {}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;//因为req没有getSession,需强转
HttpServletResponse response = (HttpServletResponse) resp;
if (request.getSession().getAttribute("USER_SESSION")==null){
response.sendRedirect("/error.jsp");
}
chain.doFilter(request,response);//让我们的请求继续走,如果不写,程序到这里就被拦截停止!
}
//销毁: web服务器关闭的时候,过滤会销毁
public void destroy() {}
}
web.xml
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.kuang.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/servlet/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>com.kuang.servlet.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/servlet/logout</url-pattern>
</servlet-mapping>
<filter>
<filter-name>SysFilter</filter-name>
<filter-class>com.kuang.filter.SysFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SysFilter</filter-name>
<url-pattern>/sys/*</url-pattern>
</filter-mapping>