功能展示
功能:
1.一个登陆页面,当用户输入账号密码后(该账号密码为已给条件,定死了的值),如果用户登陆正确则提示该用户登陆成功。
2.如果账号/密码错误,则在此次回到登录页面,且在登陆页面提示账号/密码错误。
3.如果用户直接访问登陆成功页面则提示:还未登录,并引导用户登录。
4.用户登录成功后需要显示:”‘用户的账号名’欢迎您”,且在后面添加退出登录按钮。
5.当用户点击退出登录按钮后需将用户退出。Admin
提示:注解都在代码中,如有错误或不了解请评论或联系博主.
代码展示
index.jsp
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2021/4/1 Time: 11:19 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>用户登录页</title> </head> <body> <form action="/LoginServlet" method="post"> 用户名:<input type="text" name="username"/><br> 密码:<input type="password" name="password"/><br> <input type="submit" value="登录"> <br> <hr /> <% //获取LoginServlet中32行error的值并赋值给变量"a". String a=(String) session.getAttribute("error"); //实现消除主页面一直提示为"null"情况: //判断变量"a"返回值是否为空[如果为空则证明登陆成功,不为空则登陆失败]. if (a!=null){ //如果不为空 out.print(a); //输出变量"a"的值. } else{ //反之输出"",[即为空,什么都没有] out.print(""); } %> </form> </body> </html>
Welcome.jsp
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2021/4/1 Time: 11:24 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Welcome</title> </head> <body> <% HttpSession session1=request.getSession(); String username=(String) session1.getAttribute("username"); if (username!=null){ out.print(username+"欢迎您!<a href='/LogOutServlet'>退出登录</a>"); }else{ out.print("您还未登录!请返回<a href=http://localhost:8080>登录</a>"); } %> </body> </html>
LoginServlet
package Servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import java.io.IOException; import java.io.PrintWriter; @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");//设置编码格式,否则输出为乱码或"???" PrintWriter out=response.getWriter(); request.setCharacterEncoding("utf-8"); // -----分割线----- //获取的值为<index.jsp>页面中提交的值的信息: //获取username的值并赋值给变量"username". String username=request.getParameter("username"); //获取password的值并赋值给变量"password". String password=request.getParameter("password"); // -----分割线----- //判断获取到的"username"和"password"是否等于"admin"和"123". if (username.equals("admin")&&password.equals("123")){ //声明调用方法 HttpSession session=request.getSession(); // -----分割线----- //以username命名将username的值存入session域中. session.setAttribute("username",username); //重定向至"Welcome.jsp"页面 response.sendRedirect("/Welcome.jsp"); } else { //反之 HttpSession session=request.getSession(); //存入session域信息 session.setAttribute("error","账号或密码错误,请重新输入!(っ °Д °;)っ [该提示3秒后刷新消失]"); //重写Cookie重新设置JSESSIONID的声明周期: //新建Cookie命名为"JSESSIONID"值为获取到的session的ID,并赋值给变量"a". Cookie c=new Cookie("JSESSIONID", session.getId()); c.setPath(request.getContextPath()); c.setMaxAge(3);//生存时间,以秒为单位. response.addCookie(c);//将变量"c"添加到浏览器请求头 response.sendRedirect("http://localhost:8080");//重定向至URL:"http://localhost:8080" } // if (username!=null){ // out.print(username+"欢迎您!<a href='/LogOutServlet'>退出登录</a>"); // }else{ // out.print("您还未登录!请返回<a href=http://localhost:8080/index.jsp>登录</a>"); // } } }
LogOutServlet
package Servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.io.PrintWriter; @WebServlet("/LogOutServlet") public class LogOutServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");//设置编码格式,否则输出为乱码或"???" PrintWriter out=response.getWriter(); request.setCharacterEncoding("utf-8"); // -----分割线----- HttpSession session=request.getSession(); session.removeAttribute("username");//删除session域中存入的数据 out.print("退出成功,即将返回登录页面!"); response.setHeader("Refresh","3;URL=http://localhost:8080"); } }
项目打包下载