java实现登录案例

时间:2021-05-19

本文实例为大家分享了java实现登录案例的具体代码,供大家参考,具体内容如下

一、环境搭建

JDK1.8 + Tomcat1.8

二、目录结构

三、代码示例

3.1、fail.html页面

<!DOCTYPE html><html><head><title>faill.html</title><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="this is my page"><meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css" rel="external nofollow" rel="external nofollow" >--> </head> <body> <font color='red' size='3'>亲, 你的用户名或密码输入有误!请重新输入!</font> <br /> <a href="/project03/login.html" >返回登录页面</a></body></html>

3.2、Login.htm页面

<!DOCTYPE html><html><head><title>Login.html</title><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="this is my page"><meta http-equiv="content-type" content="text/html; charset=UTF-8"><!--<link rel="stylesheet" type="text/css" href="./styles.css" >--></head><body> <form action="/project03/LoginServlet" method="post"> 用户名:<input type="text" name="UserName" /><br /> 密&nbsp;&nbsp;&nbsp;&nbsp;码:<input type="password" name="UserPwd" /><br /> <input type="submit" value="登录" /> </form></body></html>

3.3、IndexServlet.java

package cn.itcase.servlet; import java.io.IOException;import java.io.PrintWriter; import javax.servlet.ServletException;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession; /** * 用户主页逻辑 * */public class IndexServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置编码格式 response.setContentType("text/html;charset=utf-8");// setContentType设置浏览器的编码格式 // 1.信息输出至浏览器 PrintWriter writer = response.getWriter(); String html = ""; /** * 接收request域对象的数据 String loginName = * (String)request.getAttribute("loginName",userName); * */ /** * 在用户主页,判断session对象不为空且存在指定的属性则登录成功 才能访问资源。从session域对象中取出会话数据 * * * */ // 2.得到session对象 HttpSession session = request.getSession(false); // 2.1如果不存在session对象,登录不成功,跳转到登录页面 if (session == null) { response.sendRedirect(request.getContextPath() + "/Login.html"); return; } // 2.2没有在session对象域中找到相应 session唯一标识ID 则登录不成功,跳转到登录页面 String loginName = (String) session.getAttribute("loginName"); if (loginName == null) { response.sendRedirect(request.getContextPath() + "/Login.html"); return; } html = "<html><body>欢迎回来," + loginName + ",<a href='" + request.getContextPath() + "/LogoutServlet'>安全退出</a></body></html>"; writer.write(html); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

3.4、LoginServlet.java

package cn.itcase.servlet; import java.io.IOException; import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;/** * 登录的逻辑 * 设置编码格式 * 根据参数名获取参数值 * 判断逻辑(使用session域对象) * * */public class LoginServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置编码格式 request.setCharacterEncoding("utf-8");// setCharacterEncoding设置服务器的编码格式 // 1.根据参数名获取参数值 String userName = request.getParameter("UserName"); String userPwd = request.getParameter("UserPwd"); // 2.登录是否的逻辑判断 if("eric".equals(userName) && "123456".equals(userPwd)){ /**分析使用技术: * context域对象:不合适,可能会覆盖数据 * request.setAttribute("loginName",userName); * * request域对象:不合适,整个网站必须得使用转发技术来跳转 * request.getRequestDispatcher("/IndexServlet").forward(request,response); * * session域对象:合适 * response.sendRedirect(request.getContextPath()+"/IndexServlet") * */ //2.1 登录成功 // 2.1.1创建session对象 用于保存数据 HttpSession session = request.getSession(); // 2.1.1把数据保存到session域中 session.setAttribute("loginName", userName); // session对象的唯一标识"loginName" 唯一标识名称 userName //session.setMaxInactiveInterval(1*60*60*24*30); // session对象的有效时长 可以配置全局的有效时长 //2.1.3跳转到用户主页 response.sendRedirect(request.getContextPath() + "/IndexServlet"); //sendRedirect()重定向 getContextPath()请求路径 }else{ //2.2登录失败 请求重定向 response.sendRedirect(request.getContextPath() + "/fail.html"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); doGet(request,response); } }

3.5、LogoutServlet.java

package cn.itcase.servlet; import java.io.IOException; import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession; /** * 退出逻辑 * */public class LogoutServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * 安全退出 * 删除session对象中指定的loginName属性即可 * */ HttpSession session = request.getSession(false); if(session != null){ session.removeAttribute("loginName"); } //返回登录页面 response.sendRedirect(request.getContextPath() + "/Login.html"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }

3.6、总结

知道了如何实现前端页面与后端的数据交互

疑惑:如果有多个用户难道还一个一个的去判断他存不存在么?

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章