IndexServlet.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.io.*" import="com.liu.User" %>
<!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>
<%
response.setContentType("text/html;charset=utf-8");
User user = (User) session.getAttribute("user");
if(user == null){
response.getWriter().print("您還沒有登錄,請<a href=https://bbs.csdn.net/topics/ 'login.jsp'>登錄</a>");
}
else{
response.getWriter().print("您已登陸,歡迎你,"+user.getUsername() +"!");
response.getWriter().print("<a href='https://bbs.csdn.net/topics/LogoutServlet.jsp'>退出</a>");
Cookie cookie = new Cookie("JSESSIONID",session.getId());
cookie.setMaxAge(60*30);
cookie.setPath("/L");
response.addCookie(cookie);
}
%>
</body>
</html>
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 name="reg" action="LoginServlet.jsp" method="post">
姓名<input type="text" name="username" /><br />
密碼<input type="password" name="password" /><br />
<input type="submit" value="https://bbs.csdn.net/topics/提交" id="bt" />
</form>
</body>
</html>
LoginServlet.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.io.*" import="com.liu.User" %>
<!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>
<%
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
PrintWriter pw = response.getWriter();
if(("luo".equals(username))&&("123456").equals(password)){
User user = new User();
user.setUsername(username);
user.setPassword(password);
request.getSession().setAttribute("user", user);
response.sendRedirect("IndexServlet.jsp");
}else{
pw.write("用戶名或密碼錯誤,登錄失敗");
}
%>
</body>
</html>
LogoutServlet.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.getSession().removeAttribute("user");
response.sendRedirect("IndexServlet.jsp");
%>
</body>
</html>
User.java
package com.liu;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/138881.html
標籤:其他
