源码自取:https://gitee.com/P_n_ing/JSP.git
内容
归根结地还是6大内置对象,链接里面有个jsp.docx文档,这个才是重点,下面不过是简单的样例而已
- JSP的三种标签
- JSP实现登陆和注册的页面跳转和数据提交
- cookie的怎么来的
- 用cookie自动记住用户名
5. JSP通过Session实现登陆
login.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>
<form action="check.jsp" method="post">
用户名:<input type="text" name="username">
密码:<input type="password" name="userpassword">
<input type="submit" value="登陆">
</form>
</body>
</html>
check.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>
<%
request.setCharacterEncoding("utf-8");
String name = request.getParameter("username");
String password = request.getParameter("userpassword");
if(name.equals("123") && password.equals("123")){
session.setAttribute("username", name);
session.setAttribute("userpassword", password);
//用于验证sessionid和JESSIONID是否一样
System.out.println("session "+session.getId());
Cookie c = new Cookie("username",name);
response.addCookie(c);
request.getRequestDispatcher("welcom.jsp").forward(request, response);
}else{
response.sendRedirect("login.jsp");
}
%>
</body>
</html>
lost.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.invalidate();//这个会将整个session都失效,一般用这个
//session.removeAttribute("username");//让session的个别属性失效
%>
</body>
</html>
test.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对象
这也是为什么我们在一个浏览器里面登陆京东后,无论打开多少个京东的其他网页都会保持登录状态
但注意的时不同浏览器之间因为JSESSIONID不同,所以你在谷歌登陆京东,但是你的ie仍然时未登录状态
-->
<%
out.print(session.getAttribute("username"));
//测试JSESSIONID与sessionid是否一致
Cookie[] c = request.getCookies();
for(Cookie cookies:c){
if(cookies.getName().equals("JSESSIONID")){
System.out.println("JESSION "+cookies.getValue());
}
}
%>
</body>
</html>
welcome.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>
欢迎你
<%
String name = (String)session.getAttribute("username");
//如果用toString()方法,那当对象为空时,则无法执行toString()方法而报错
if(name != null){
out.print(name);
//session.setMaxInactiveInterval(60*10);
%>
<a href="lost.jsp">让sessionid失效</a>
<%
}else{
response.sendRedirect("login.jsp");
}
%>
</body>
</html>
- 在JSP中获得 Web 应用的路径