用session写了一个实现用户登录,但是输入用户名和密码后,却返回还没登录。我个人认为是属性值没有传过去。但是不知道哪里有错。希望大神指点。谢谢
Login1.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form name = "reg" action = "/TheSixSection/LoginServlet" method = "post">
用户名:<input name = "username" type = "text"/><br>
密 码:<input name = "password" type = "password"/><br>
<input type = "submit" value = "登录" id = "bt"/>
</form>
</body>
</html>
IndexServlet1.java
//这个是欢迎页面,把doGet 方法贴出
//防止乱码
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//创建一个session对象
HttpSession session = request.getSession();
User user = (User)session.getAttribute("user");
if(user == null){
out.print("您还没有登录,请<a href = '/TheSixSection/Login1.html'>登录</a>");
}else{
out.println("欢迎登录"+user.getUsername()+"!");
out.println("<a href = '/TheSixSection/LogoutServlet'>退出</a>");
//创建cookie存放session标识符
Cookie cookie = new Cookie("JSESSIONID", session.getId());
cookie.setMaxAge(60*30);
cookie.setPath("/TheSixSection");
response.addCookie(cookie);
}
LoginServlet.java
//显示doGet方法
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
//假设正确的用户名是:youli,密码是123
if(("youli").equals(username)&&("123").equals(password)){
User user = new User();
user.setUsername(username);
user.setPassword(password);
request.setAttribute("user", user);
response.sendRedirect("/TheSixSection/IndexServlet1");
}else{
out.write("用户名或密码错误,请重新登录");
}