servlet:重定向和转发 ServletContext上下文对象
程序员文章站
2022-05-24 14:36:57
...
1、转发和重定向区别?
- 重定向地址栏地址变化,转发不变
- 重定向发生在客户端,转发发生在服务器端,客户端不知道
- 重定向不能携带数据,转发可以
- 重定向时,客户端发送了两次请求,而转发只有一次请求(本质区别)
- 重定向:提交表单,处理成功后重定向到另一个jsp,防止表单重复提交,浏览器里url变了
- 转发:当用户登录错误时,一般会回到登录页并提示信息,这个时候用转发问题不大且方便效率高。但现在开发一般不用这个,一般用ajax异步提交,可以从后台带提示信息过去,所以现在用转发意义不大。
2、ServletContext
-
是什么?
Servlet上下文对象,是所有servlet对象共享的一片内存空间,我们可以把数据存在这个内存空间中,实现多个servlet对象共享数据。 -
在上下文对象中怎么放入对象?
1.在web.xml中配置<context-param> <param-name>testServletContext</param-name> <param-value>testServletContext</param-value> </context-param> 就可以直接在servlet中访问 package com.wyz.selvletContext; import java.io.IOException; import java.util.List; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TestServletContext extends HttpServlet{ private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext application =this.getServletContext(); System.out.println(application.getInitParameter("testServletContext")); System.out.println(application.getInitParameter("testServletContext2")); } public void destroy() { } public void init() throws ServletException { }
}
2.在任意一个servlet里面设置ServletContext的属性,并赋予属性值public class myFirstServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doPost() throws ServletException { ServletContext application=this.getServletContext(); application.setAttribute("name2", "name2"); } }
得到上下文对象
public class TestServletContext extends HttpServlet{ private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext application =this.getServletContext(); application.setAttribute("name", "了就立刻搭街坊"); System.out.println(application.getAttribute("name")); System.out.println(application.getAttribute("name2")); } }
3.系统启动的时候启动某个servlet,在这个servlet中init方法中设置上下文对象, 这样就相当于在系统启动是初始化ServletContext。这里需要在web.xml文件中配置。
<servlet> <servlet-name>myFirstServlet</servlet-name> <servlet-class>com.wyz.prac.myFirstServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet>
public class myFirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init() throws ServletException {
System.out.println("I AM INIT METHOD");
List<String> list=new ArrayList<String>();
list.add("yoyo");
list.add("toco");
list.add("jiojio");
ServletContext context=this.getServletContext();
context.setAttribute("list", list);
}
}
public class TestServletContext extends HttpServlet{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext application =this.getServletContext();
ServletContext context=this.getServletContext();
List<String> list=(List<String>)context.getAttribute("list");
for(String str:list){
System.out.println(str);
}
}
}
方法3可以用于统计用户在线人数:
a:创建InitServlet 把它设置为随系统启动就初始化(load-on-startup)
在InitServlet类 init方法中创建一个空对象,用以存储用户的在线记录
b : 每次用户登录(loginServlet)的时候,
我们就会调用ServletContext上下文对象,并将用户记录放入 我们准备好的对象中(哪个对象请见 InitServlet)
c: 每次用户登出(logoutServelt)的时候,我们也要调用,然后清除此登录用户信息,保持数据的正确性
d:在我们需要展示人数的servlet里面调用ServletContext上下文对象,获取所有用户信息
<!DOCTYPE html>
<html>
<head>
<title>login.html</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<!-- 解决html5乱码问题 http-equiv="Content-Type"-->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<h1>This is my HTML page.</h1> <br>
<form action="loginServlet" name="loginForm" method="post">
用户名:<input type="text" name="username"><br><br>
密码:<input type="password" name="pwd"><br><br>
<input type="submit" value="登录">
<br><br><br><br><br><br>
</form>
</body>
</html>
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
}
public void init() throws ServletException {
ServletContext application=this.getServletContext();
List<String> list=new ArrayList<String>();
application.setAttribute("userList", list);
}
}
public class TestLoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public TestLoginServlet() {
super();
}
public void destroy() {
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext application=this.getServletContext();
List<String> list=(List<String>)application.getAttribute("userList");
String name=request.getParameter("username");
list.add(name);
PrintWriter out=response.getWriter();
out.print("当前用户数:"+list.size());
}
public void init() throws ServletException {
}
}
上一篇: ServletContext上下文
下一篇: Servlet上下文
推荐阅读
-
JSP内置对象和请求转发及重定向
-
Servlet中forward(请求转发)和redirect( 重定向)总结 forwardredirect
-
servlet中 请求转发和重定向的区别
-
Servlet重定向和请求转发的区别
-
JSP内置对象和请求转发及重定向
-
Servlet,Listener和Filter 获取ServletContext (application上下文环境)
-
servlet:重定向和转发 ServletContext上下文对象
-
java servlet 通过上下文对象ServletContext实现单态登录
-
Servlet 知识详解(一)之 —— ServletContext对象 和 ServletConfig对象 学习笔记
-
response对象的转发和重定向