当前位置:网站首页>Cookies and sessions
Cookies and sessions
2022-06-12 09:01:00 【continueLR】
Chapter one :cookie And session Use
1.cookie Use
1: stay index.jsp Make a login form as login request
<h1> Sign in </h1>
<form action="login" method="post">
user name :<input type="text" name="username" value="${unamecookie}"><br>
password :<input type="password" name="pass"><br>
<input type="submit" value=" Sign in ">
</form>2: establish servlet Processing class processing login request
package servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/login")
public class loginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// Answer the parameters
String username = req.getParameter("username");
String pass = req.getParameter("pass");
// Write logic : Right or wrong
if ("admin".equals(username)&&"123456".equals(pass)){
// Login successful
resp.sendRedirect("success.jsp");
}else{
// Login failed
//1. Create in the background cookie
Cookie cookie = new Cookie("uname",username);
//2. Back to the front end
resp.addCookie(cookie);
resp.sendRedirect("index.jsp");
}
}
}
3: Front end use cookie
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%
Cookie[] cookies = request.getCookies();
String value="";
if (cookies.length>0){// This if Judge to avoid errors caused by traversing empty data
for (Cookie cookie : cookies) {// Traverse cookies Array
if (cookie.getName().equals("uname")){
value = cookie.getValue();
break;
};
}
}
pageContext.setAttribute("unamecookie",value);
%>
<h1> Sign in </h1>
<form action="login" method="post">
user name :<input type="text" name="username" value="${unamecookie}"><br>
password :<input type="password" name="pass"><br>
<input type="submit" value=" Sign in ">
</form>
</body>
</html>2.session Use
1. Login successful session Store value , Set the expiration time
package servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
@WebServlet(urlPatterns = "/login")
public class loginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// Answer the parameters
String username = req.getParameter("username");
String pass = req.getParameter("pass");
// Write logic : Right or wrong
if ("admin".equals(username)&&"123456".equals(pass)){
// Login successful
//session Store value
HttpSession session = req.getSession();
session.setAttribute("username",username);
session.setAttribute("psssword",pass);
// Set up session Failure time
session.setMaxInactiveInterval(60*30);// The unit is seconds
// Redirect or forward ,session Data is not lost , But when redirecting ,requet The data will be lost
resp.sendRedirect("success.jsp");
}else{
// Login failed
//1. Create in the background cookie
Cookie cookie = new Cookie("uname",username);
//2. Back to the front end
resp.addCookie(cookie);
resp.sendRedirect("index.jsp");
}
}
}
2. Show session
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>testsession.jsp</h1>
Currently logged in user :${sessionScope.username}<br>
</body>
</html>
3. Exit empty session request
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>success.jsp</h1>
Currently logged in user :${sessionScope.username}<br>
<a href="loginout"> sign out </a>
</body>
</html>
4.servlet The processing class handles the exit request
package servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/loginout")
public class LoginOutServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getSession().invalidate();// send session invalid
resp.sendRedirect("index.jsp");
}
}
边栏推荐
- Get last month, current time and next month
- 最少换乘次数
- torch. logical_ And() method
- 重启Kubernetes Pod的几种方式
- API handling Android security distance
- The database doesn't know what went wrong
- Offer:[day 8 dynamic planning (simple)] --- > maximum profit of stock
- Close asymmetric key
- Flink传入自定义的参数或配置文件
- Node sample background setup
猜你喜欢

Box model border

Analysis of 43 cases of MATLAB neural network: Chapter 8 prediction of GRNN Network - Freight Volume Prediction Based on generalized regression neural network

Binlog in mysql:

通俗理解时域采样与频域延拓

About weights exercise

The classic dog contract of smart contract (I)

node示例后台搭建

MFS详解(四)——MFS管理服务器安装与配置
![[open source project] easycmd command graphical software](/img/7b/4b4d7059a5dd520f3491cc376f41fc.jpg)
[open source project] easycmd command graphical software

Adjust SVG width and height
随机推荐
第四章-第一个程序
Handling abnormal data
[advanced pointer I] character array & array pointer & pointer array
Use NVM to dynamically adjust the nodejs version to solve the problem that the project cannot be run and packaged because the node version is too high or too low
UMI packaging and subcontracting, and compressing to gzip
MFS详解(四)——MFS管理服务器安装与配置
128. 最長連續序列-哈希錶
【字符集八】char8_t、char16_t、char32_t、wchar、char
Dynamically create and submit forms
Technology cloud report: how will the industrial Internet rebuild the security boundary in 2022?
sql中的Exists用法
xshell启动遇到“由于找不到mfc110.dll,无法继续执行代码的解决方法”
《MATLAB 神經網絡43個案例分析》:第7章 RBF網絡的回歸--非線性函數回歸的實現
Inheritance of row height
Shell basic syntax -- arithmetic operation
Difference between binary GB and gib
Construction of memcached cache service under Linux:
[open source project] easycmd command graphical software
Flink passes in custom parameters or profiles
【无标题】Task3 多路召回