1、从浏览器访问网站首页IndexServlet
2、判断用后是否登录
3、若以登录则显示用户信息
4、若未登录,进图登录界面login.html;
4.1、处理用户登录信息(LoginServlet)
4.2、判断用户名和密码是否正确
4.3、若正确则显示用户信息
4.4、若不正确则提示用户名或密码错误
5、若用户退出,则进入LogoutServlet界面
下面我们通过上面的流程来建立代码,
创建一个名为web-chapter05的web项目,并在其中创建cn.itcast.chapter05.session.example02的包,并在包中创建一个User类
代码如下:
package cn.itcast.chapter05.session.example02;
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;
}
}
在cn.itcast.chapter05.session.example02的包中创建IndexServlet类,该类是显示网站的首页
package cn.itcast.chapter05.session.example02;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class IndexServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//解决乱码问题
response.setContentType("text/html;charset=utf-8");
//创建或者获取保存用户信息的session对象
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
if(user==null) {
response.getWriter().print("您还没有登录,请登录");
}else {
response.getWriter().print("您已登录,欢迎你,"+ user.getUsername()+"!");
response.getWriter().print("退出");
//创建cookie存放session的标识号
Cookie cookie = new Cookie("JSESSIONID", session.getId());
cookie.setMaxAge(60*30);
cookie.setPath("/web-chapter05");
response.addCookie(cookie);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
在cn.itcast.chapter05.session.example02的包中创建LoginServlet类,该类是显示网站的登录页
package cn.itcast.chapter05.session.example02;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//解决乱码问题
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
PrintWriter pw = response.getWriter();
if(("itcast").equals(username)&&("123456").equals(password)) {
User user = new User();
user.setUsername(username);
user.setPassword(password);
request.getSession().setAttribute("user", user);
response.sendRedirect("/web-chapter05/IndexServlet");
}else {
pw.write("用户名或者密码错误,登录失败");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
在cn.itcast.chapter05.session.example02的包中创建LogoutServlet类,该类是显示网站的退出及注销页面
package cn.itcast.chapter05.session.example02;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class LogoutServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//将session对象中的user对象移除,之后重定向到首页
request.getSession().removeAttribute("user");
response.sendRedirect("/web-chapter05/IndexServlet");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
在web-chapter05项目的WebContent目录下创建一个名为login.html的页面
Insert title here
IndexServlet
IndexServlet
cn.itcast.chapter05.session.example02.IndexServlet
IndexServlet
/IndexServlet
LoginServlet
LoginServlet
cn.itcast.chapter05.session.example02.LoginServlet
LoginServlet
/LoginServlet
LogoutServlet
LogoutServlet
cn.itcast.chapter05.session.example02.LogoutServlet
LogoutServlet
/LogoutServlet
七、启动tomcat,在浏览器测试
在浏览器网址栏中输入http://localhost:8080/web-chapter05/login.html,进入下面的界面
然后输入之前在程序中设定好的账号itcast,和密码123456,进入下面的界面
点击退出链接就会进入下面的界面
如果点击登录的话就会接着进入登录的界面
至此,一个简单的模拟实现用户登录就完成了