java如何实现登录

发布时间:2021-04-15 10:47:08 作者:小新
来源:亿速云 阅读:153

这篇文章将为大家详细讲解有关java如何实现登录,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

具体内容如下

一、环境搭建

JDK1.8  + Tomcat1.8

二、目录结构

java如何实现登录

三、代码示例

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);
 
 
 }
 
}

关于“java如何实现登录”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. java web如何实现自动登录功能
  2. java中怎么实现动态口令登录

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:java如何实现的根据概率随机中奖测试类

下一篇:Java怎么实现鼠标模拟与键盘映射

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》