更新时间:2021-04-28 13:39:02 来源:动力节点 浏览676次
JDK1.8 + Tomcat1.8
(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>
(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 />
密 码:<input type="password" name="UserPwd" /><br />
<input type="submit" value="登录" />
</form>
</body>
</html>
(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);
}
}
(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);
}
}
(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登录代码案例"的内容,希望对大家有帮助,如有疑问,请在线咨询,有专业老师随时为您服务。
0基础 0学费 15天面授
Java就业班有基础 直达就业
业余时间 高薪转行
Java在职加薪班工作1~3年,加薪神器
工作3~5年,晋升架构
提交申请后,顾问老师会电话与您沟通安排学习