1、概述
2、使用session保存用户名
session工作方式:
会话的清除与过期:
createUser.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 id="" action="doCreateUser.jsp" method="post">
用户名:<input type="text" name="userName"/>
<input type="submit" value="提交"/>
</form>
<%
// 取回提示信息
Object oMess = request.getAttribute("mess");
if (oMess != null) {
out.print(oMess.toString());
}
%>
</body>
</html>
doCreateUser.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 userName = request.getParameter("userName");
//out.print(userName);
if (userName.equals("admin")) {
// 加入提示信息
request.setAttribute("mess","注册失败,更换用户名。");
request.getRequestDispatcher("createUser.jsp").forward(request, response);
//response.sendRedirect("createUser.jsp");
} else {
session.setAttribute("user",userName);
//request.getRequestDispatcher("default.jsp").forward(request, response);
response.sendRedirect("default.jsp");
}
%>
</body>
</html>
default.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.getAttribute("mess")).toString() %> --%>
<%
Object o = session.getAttribute("user");
if (o == null) {
%>
<form id="" action="doCreateUser.jsp" method="post">
用户名:<input type="text" name="userName"/>
<input type="submit" value="提交"/>
</form>
<%
} else {
out.print("欢迎你," + o.toString());
%>
<a href="doLoginOut.jsp">注销</a>
<%
}
%>
</body>
</html>
doLoginOut.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.removeAttribute("user");// 释放session
//session.invalidate();
response.sendRedirect("default.jsp");
%>
</body>
</html>
3、使用cookie自动填写用户名
doCreateUser.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 userName = request.getParameter("userName");
//out.print(userName);
if (userName.equals("admin")) {
// 加入提示信息
request.setAttribute("mess","注册失败,更换用户名。");
request.getRequestDispatcher("createUser.jsp").forward(request, response);
//response.sendRedirect("createUser.jsp");
} else {
// 创建cookie
Cookie cookie = new Cookie("user",userName);
// 设置cookie的有效期,单位秒
cookie.setMaxAge(10);
response.addCookie(cookie);
session.setAttribute("user",userName);
//request.getRequestDispatcher("default.jsp").forward(request, response);
response.sendRedirect("default.jsp");
}
%>
</body>
</html>
default.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.getAttribute("mess")).toString() %> --%>
<%
// 获取cookie
Cookie[] cookies = request.getCookies();
String user = "";
for (int i = 0 ; i < cookies.length ; i++) {
if (cookies[i].getName().equals("user")) {
user = cookies[i].getValue();
}
}
Object o = session.getAttribute("user");
if (o == null) {
%>
<form id="" action="doCreateUser.jsp" method="post">
用户名:<input type="text" name="userName" value="<%=user%>"/>
<input type="submit" value="提交"/>
</form>
<%
} else {
out.print("欢迎你," + o.toString());
%>
<a href="doLoginOut.jsp">注销</a>
<%
}
%>
</body>
</html>
查看cookie文件:
cookie文件中的内容:
user
qwe
localhost/news/jsp/
1024
1578087680
30438420
1482467680
30438420
*
4、application实现计数器
<%@ 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.getAttribute("mess")).toString() %> --%>
<%
// 获取cookie
Cookie[] cookies = request.getCookies();
String user = "";
for (int i = 0 ; i < cookies.length ; i++) {
if (cookies[i].getName().equals("user")) {
user = cookies[i].getValue();
}
}
Object o = session.getAttribute("user");
if (o == null) {
%>
<form id="" action="doCreateUser.jsp" method="post">
用户名:<input type="text" name="userName" value="<%=user%>"/>
<input type="submit" value="提交"/>
</form>
<%
} else {
out.print("欢迎你," + o.toString());
%>
<a href="doLoginOut.jsp">注销</a>
<%
}
%>
<%
Object count = application.getAttribute("count");
if (count == null) {
// 第一次访问
application.setAttribute("count", new Integer(1));
} else {
Integer i = (Integer)count;
// 每一次访问+1
application.setAttribute("count", i.intValue() + 1);
}
Integer icount = (Integer)application.getAttribute("count");
out.print("页面被访问了" + icount.intValue() + "次");
%>
</body>
</html>
5、三个对象的对比
6、jsp页面的组成部分
7、常用内置对象
8、数据保存
9、客户端请求新页面
注:修改οnclick="return fun();"
10、处理中文乱码