通过浏览器对象response传值的使用,创建\赋值及注销
//登录成功
response.sendRedirect("/success.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>欢迎页面</title>
</head>
<body>
<%
//从Request获取id、pwd、name的值
String id= request.getParameter("id");
String pwd= request.getParameter("pwd");
String name= request.getParamete("name");
//获取Session对象
String path = request.getContextPath();
HttpSession session=request.getSession();
//向Session对象内放属性
session.setAttribute("id", id);
session.setAttribute("pwd", pwd);
session.setAttribute("name", name);
//页面输出
out.println("<span>欢迎"+name+"登录</span>");
//注销链接
<a href="out.jsp">注销</a>
%>
</body>
</html>
注销页面(out.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>
<%
//获取Session对象
String path = request.getContextPath();
HttpSession session=request.getSession();
//清空Session中的登录信息相关属性
session.removeAttribute("id");
session.removeAttribute("pwd");
session.removeAttribute("name");
//上下文路径
String path = request.getContextPath();
//重定向到首页(未登录状态)
response.sendRedirect(path+"/index.jsp");
%>
</body>
</html>