<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>欢迎页</title>
</head>
<body>
<h1>用户登录</h1>
<hr>
<form action="<%=request.getContextPath()%>/user/login" method="post">
username:<input type="text" name="username"><br>
password:<input type="password" name="password"><br>
10天免登录:<input type="checkbox" name="flag" value="1"><br>
<input type="submit" value="login">
</form>
<h1>
<a href="<%=request.getContextPath()%>/cookie/generate">
服务器生成Cookie,然后把Cookie传给浏览器,浏览器收到,再放到客户端上
</a>
</h1>
<h1>
<a href="<%=request.getContextPath()%>/sendCookie">
浏览器发送Cookie给服务器
</a>
</h1>
<%-- 前端发送请求路径的时候,如果请求路径是绝对路径,要以/开始,加项目名--%>
<%-- <h1><a href="<%=(request.getContextPath())%>/dept/list">查看部门列表</a></h1>--%>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>欢迎页</title>
</head>
<body>
<h1>用户登录</h1>
<hr>
<form action="<%=request.getContextPath()%>/user/login" method="post">
username:<input type="text" name="username"><br>
password:<input type="password" name="password"><br>
10天免登录:<input type="checkbox" name="flag" value="1"><br>
<input type="submit" value="login">
</form>
<h1>
<a href="<%=request.getContextPath()%>/cookie/generate">
服务器生成Cookie,然后把Cookie传给浏览器,浏览器收到,再放到客户端上
</a>
</h1>
<h1>
<a href="<%=request.getContextPath()%>/sendCookie">
浏览器发送Cookie给服务器
</a>
</h1>
<%-- 前端发送请求路径的时候,如果请求路径是绝对路径,要以/开始,加项目名--%>
<%-- <h1><a href="<%=(request.getContextPath())%>/dept/list">查看部门列表</a></h1>--%>
</body>
</html>
package com.bjpowernode.oa.web.action;
import com.bjpowernode.oa.utils.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@WebServlet("/Welcome")
public class Welcome extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
Cookie[] cookies = request.getCookies();
String username = null;
String password = null;
if (cookies != null)
{
for(int i = 0; i < cookies.length; i++)
{
if("username".equals(cookies[i].getName()))
{
username = cookies[i].getValue();
}
if("password".equals(cookies[i].getName()))
{
password = cookies[i].getValue();
}
}
}
if(username != null && password != null)
{
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
boolean flag = false;
try
{
connection = DBUtil.getConnection();
String sql = "select * from t_user where username = ? and pssword = ?";
statement = connection.prepareStatement(sql);
statement.setString(1,username);
statement.setString(2,password);
resultSet = statement.executeQuery();
if(resultSet.next())
{
flag = true;
}
}
catch (SQLException e)
{
throw new RuntimeException(e);
}
finally
{
DBUtil.close(connection,statement,resultSet);
}
if(flag)
{
HttpSession session = request.getSession();
session.setAttribute("username",username);
response.sendRedirect(request.getContextPath() + "/dept/list");
}
else
{
response.sendRedirect(request.getContextPath() + "/index.jsp");
}
}
else
{
response.sendRedirect(request.getContextPath() + "/index.jsp");
}
}
}
package com.bjpowernode.oa.web.action;
import com.bjpowernode.oa.utils.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@WebServlet("/Welcome")
public class Welcome extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
Cookie[] cookies = request.getCookies();
String username = null;
String password = null;
if (cookies != null)
{
for(int i = 0; i < cookies.length; i++)
{
if("username".equals(cookies[i].getName()))
{
username = cookies[i].getValue();
}
if("password".equals(cookies[i].getName()))
{
password = cookies[i].getValue();
}
}
}
if(username != null && password != null)
{
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
boolean flag = false;
try
{
connection = DBUtil.getConnection();
String sql = "select * from t_user where username = ? and pssword = ?";
statement = connection.prepareStatement(sql);
statement.setString(1,username);
statement.setString(2,password);
resultSet = statement.executeQuery();
if(resultSet.next())
{
flag = true;
}
}
catch (SQLException e)
{
throw new RuntimeException(e);
}
finally
{
DBUtil.close(connection,statement,resultSet);
}
if(flag)
{
HttpSession session = request.getSession();
session.setAttribute("username",username);
response.sendRedirect(request.getContextPath() + "/dept/list");
}
else
{
response.sendRedirect(request.getContextPath() + "/index.jsp");
}
}
else
{
response.sendRedirect(request.getContextPath() + "/index.jsp");
}
}
}
package com.bjpowernode.oa.web.action;
import com.bjpowernode.oa.utils.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
@WebServlet({"/user/login","/user/exit"})
public class UserServlet extends HttpServlet
{
//JSessionID=??这个键值对数据就是cookie保存的信息
//对于session关联的cookie来说,这个cookie是保存在浏览器的运行内存中的
//用户再次发送请求的时候,会自动把运行内存中的cookie放到请求头内一并发送
//服务器就是根据这个值找到对应的对象
//session存储在服务器上的,cookie是储存在电脑机器上的
//cookie可以存在浏览器运行内存中,也可以放在硬盘内保存
//cookie和session都是为了保存会话的状态
//这两个机制存在的目的就是为了解决我们的http协议的无状态无连接协议的问题
//(无状态无连接)没有请求和响应时,双方不知道对方是否还在
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String servletPath = request.getServletPath();
if("/user/login".equals(servletPath))
{
doLogin(request,response);
}
else if("/user/exit".equals(servletPath))
{
doExit(request,response);
}
}
//将购物车中的商品编号放到cookie当中,cookie保存到硬盘
//下一次再打开,自动读取本地cookie,就还会显示这些商品
//购物网站未登录不允许查看商品和保存购物车的原理是,为了记录数据,什么用户喜欢什么样的东西
//和针对不同用户给出不同的优惠力度(大数据杀熟),并且可以防止爬虫,和减轻服务器压力(没有购买欲望的客户不让你看)
//十天免登录的实现原理如下,保存一个记录了用户名和密码的cookie到本地
//当十天内再次访问该网站时,会自动在请求头内发送这个账号用户名和密码到服务器,服务器接收到这个cookie
//解密后实现登录
//cookie和session都是Http协议的一部分,因为Http协议是无状态和无连接的协议
//Http协议规定,任何一个cookie都是由name和value组成的
//在java的servlet中,对cookie提供了那些支持?
//jakarta.servlet.http.HttpCookie
//Cookie是带有路径的,不同的cookie会带不同的路径,方便对应的提交信息和用于握手机制的实现
//我们java的cookie主要是在response中将信息作为回传给浏览器的,在服务器中只是做一个信息的处理
//在Http协议中规定如下,在浏览器发送请求时,会自动携带该路径下的cookie在请求头中给服务器
//cookie的name和value都是字符串,cookie的name和value都是字符串
private void doExit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
HttpSession session = request.getSession(false);
if(session != null && session.getAttribute("username") != null)
{
//销毁session的方法,session.invalidate()
session.invalidate();
response.sendRedirect(request.getContextPath() + "/index.jsp");
}
}
protected void doLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String loginSet = request.getParameter("flag");
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
boolean flag = false;
try
{
connection = DBUtil.getConnection();
String sql = "select * from t_user where username = ? and pssword = ?";
statement = connection.prepareStatement(sql);
statement.setString(1,username);
statement.setString(2,password);
resultSet = statement.executeQuery();
if(resultSet.next())
{
flag = true;
}
}
catch (SQLException e)
{
throw new RuntimeException(e);
}
finally
{
DBUtil.close(connection,statement,resultSet);
}
if(flag)
{
HttpSession session = request.getSession();
session.setAttribute("username",username);
if(loginSet != null && "1".equals(loginSet))
{
//存储登录名
Cookie name = new Cookie("username",username);
//存储密码
Cookie LoginPassword = new Cookie("password",password);
//生命周期都设为1小时,实验够了
name.setMaxAge(60 * 60);
LoginPassword.setMaxAge(60 * 60);
//保存到浏览器中
name.setPath(request.getContextPath());
LoginPassword.setPath(request.getContextPath());
//添加到响应中去
response.addCookie(name);
response.addCookie(LoginPassword);
}
response.sendRedirect(request.getContextPath() + "/dept/list");
}
else
{
response.sendRedirect(request.getContextPath() + "/Error.jsp");
}
}
}
package com.bjpowernode.oa.web.action;
import com.bjpowernode.oa.utils.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
@WebServlet({"/user/login","/user/exit"})
public class UserServlet extends HttpServlet
{
//JSessionID=??这个键值对数据就是cookie保存的信息
//对于session关联的cookie来说,这个cookie是保存在浏览器的运行内存中的
//用户再次发送请求的时候,会自动把运行内存中的cookie放到请求头内一并发送
//服务器就是根据这个值找到对应的对象
//session存储在服务器上的,cookie是储存在电脑机器上的
//cookie可以存在浏览器运行内存中,也可以放在硬盘内保存
//cookie和session都是为了保存会话的状态
//这两个机制存在的目的就是为了解决我们的http协议的无状态无连接协议的问题
//(无状态无连接)没有请求和响应时,双方不知道对方是否还在
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String servletPath = request.getServletPath();
if("/user/login".equals(servletPath))
{
doLogin(request,response);
}
else if("/user/exit".equals(servletPath))
{
doExit(request,response);
}
}
//将购物车中的商品编号放到cookie当中,cookie保存到硬盘
//下一次再打开,自动读取本地cookie,就还会显示这些商品
//购物网站未登录不允许查看商品和保存购物车的原理是,为了记录数据,什么用户喜欢什么样的东西
//和针对不同用户给出不同的优惠力度(大数据杀熟),并且可以防止爬虫,和减轻服务器压力(没有购买欲望的客户不让你看)
//十天免登录的实现原理如下,保存一个记录了用户名和密码的cookie到本地
//当十天内再次访问该网站时,会自动在请求头内发送这个账号用户名和密码到服务器,服务器接收到这个cookie
//解密后实现登录
//cookie和session都是Http协议的一部分,因为Http协议是无状态和无连接的协议
//Http协议规定,任何一个cookie都是由name和value组成的
//在java的servlet中,对cookie提供了那些支持?
//jakarta.servlet.http.HttpCookie
//Cookie是带有路径的,不同的cookie会带不同的路径,方便对应的提交信息和用于握手机制的实现
//我们java的cookie主要是在response中将信息作为回传给浏览器的,在服务器中只是做一个信息的处理
//在Http协议中规定如下,在浏览器发送请求时,会自动携带该路径下的cookie在请求头中给服务器
//cookie的name和value都是字符串,cookie的name和value都是字符串
private void doExit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
HttpSession session = request.getSession(false);
if(session != null && session.getAttribute("username") != null)
{
//销毁session的方法,session.invalidate()
session.invalidate();
response.sendRedirect(request.getContextPath() + "/index.jsp");
}
}
protected void doLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String loginSet = request.getParameter("flag");
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
boolean flag = false;
try
{
connection = DBUtil.getConnection();
String sql = "select * from t_user where username = ? and pssword = ?";
statement = connection.prepareStatement(sql);
statement.setString(1,username);
statement.setString(2,password);
resultSet = statement.executeQuery();
if(resultSet.next())
{
flag = true;
}
}
catch (SQLException e)
{
throw new RuntimeException(e);
}
finally
{
DBUtil.close(connection,statement,resultSet);
}
if(flag)
{
HttpSession session = request.getSession();
session.setAttribute("username",username);
if(loginSet != null && "1".equals(loginSet))
{
//存储登录名
Cookie name = new Cookie("username",username);
//存储密码
Cookie LoginPassword = new Cookie("password",password);
//生命周期都设为1小时,实验够了
name.setMaxAge(60 * 60);
LoginPassword.setMaxAge(60 * 60);
//保存到浏览器中
name.setPath(request.getContextPath());
LoginPassword.setPath(request.getContextPath());
//添加到响应中去
response.addCookie(name);
response.addCookie(LoginPassword);
}
response.sendRedirect(request.getContextPath() + "/dept/list");
}
else
{
response.sendRedirect(request.getContextPath() + "/Error.jsp");
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<session-config>
<!-- 代表session的超时时间是30分钟,如果30分钟到,session对象就会被销毁-->
<session-timeout>30</session-timeout>
</session-config>
<!-- 使用欢迎页机制跳转到cookie登录-->
<welcome-file-list>
<welcome-file>Welcome</welcome-file>
</welcome-file-list>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<session-config>
<!-- 代表session的超时时间是30分钟,如果30分钟到,session对象就会被销毁-->
<session-timeout>30</session-timeout>
</session-config>
<!-- 使用欢迎页机制跳转到cookie登录-->
<welcome-file-list>
<welcome-file>Welcome</welcome-file>
</welcome-file-list>
</web-app>
还没有评论,来说两句吧...