java实现登录案例
本⽂实例为⼤家分享了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 />
密    ;码:<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 = Writer();
String html = "";
/**
* 接收request域对象的数据 String loginName =
* (Attribute("loginName",userName);
*
*/
/**
* 在⽤户主页,判断session对象不为空且存在指定的属性则登录成功才能访问资源。从session域对象中取出会话数据 *
*
* */
// 2.得到session对象
HttpSession session = Session(false);
// 2.1如果不存在session对象,登录不成功,跳转到登录页⾯
if (session == null) {
response.ContextPath()
+ "/Login.html");
return;
}
// 2.2没有在session对象域中到相应 session唯⼀标识ID 则登录不成功,跳转到登录页⾯
String loginName = (String) Attribute("loginName");
if (loginName == null) {
response.ContextPath() + "/Login.html");
return;
}
html = "<html><body>欢迎回来," + loginName + ",<a href='"
+ ContextPath()
+ "/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 = Parameter("UserName");
String userPwd = Parameter("UserPwd");
// 2.登录是否的逻辑判断
if("eric".equals(userName) && "123456".equals(userPwd)){
/**分析使⽤技术:
* context域对象:不合适,可能会覆盖数据
* request.setAttribute("loginName",userName);
*
* request域对象:不合适,整个⽹站必须得使⽤转发技术来跳转
* RequestDispatcher("/IndexServlet").forward(request,response);
*
* session域对象:合适
* response.ContextPath()+"/IndexServlet")
* */
//2.1 登录成功
// 2.1.1创建session对象⽤于保存数据
HttpSession session = Session();
/
/ 2.1.1把数据保存到session域中
session.setAttribute("loginName", userName); // session对象的唯⼀标识"loginName" 唯⼀标识名称 userName
//session.setMaxInactiveInterval(1*60*60*24*30); // session对象的有效时长可以配置全局的有效时长
//2.1.3跳转到⽤户主页
response.ContextPath() + "/IndexServlet"); //sendRedirect()重定向 getContextPath()请求路径 }else{
//2.2登录失败请求重定向
response.ContextPath() + "/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 = Session(false);
if(session != null){
}
//返回登录页⾯
response.ContextPath() + "/Login.html");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
html实现用户注册登录代码
}
3.6、总结
知道了如何实现前端页⾯与后端的数据交互
疑惑:如果有多个⽤户难道还⼀个⼀个的去判断他存不存在么?
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。