当前位置:网站首页>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");
}
}
边栏推荐
- Chapter 7 - more flexible location of memory addresses
- 2022.6.11-----leetcode. nine hundred and twenty-six
- Technology cloud report: how will the industrial Internet rebuild the security boundary in 2022?
- Notes used by mqtt (combined with source code)
- Get last month, current time and next month
- 第五章-[bx]和Loop指令
- Background fixing effect
- Binlog in mysql:
- MFS explanation (IV) -- MFS management server installation and configuration
- Minimum transfer times
猜你喜欢
![(node:22344) [DEP0123] DeprecationWarning: Setting the TLS ServerName to an IP address is not permit](/img/c1/d56ec09663857afa52f20848aeadac.png)
(node:22344) [DEP0123] DeprecationWarning: Setting the TLS ServerName to an IP address is not permit

Jupyter notebook sets the default browser to open with an error syntaxerror: (Unicode error) 'UTF-8' codec can't decode byte 0xd4

Flink CheckPoint : Exceeded checkpoint tolerable failure threshold

数据库不知道哪里出问题

Flink CheckPoint : Exceeded checkpoint tolerable failure threshold

Popular understanding of time domain sampling and frequency domain continuation

Background position - mixed units

Does database and table splitting cause reading diffusion problems? How to solve it?

Summary of common character sets

Problems that cannot be resolved by tar command
随机推荐
Engineers learn music theory (III) interval mode and chord
Jenkins Pipeline 语法
Analysis of 43 cases of MATLAB neural network: Chapter 8 prediction of GRNN Network - Freight Volume Prediction Based on generalized regression neural network
QT realizes multi screen and multi-resolution adaptation
consul 配置相关
剑指 Offer II 016. 不含重复字符的最长子字符串-滑动窗口
《MATLAB 神經網絡43個案例分析》:第7章 RBF網絡的回歸--非線性函數回歸的實現
(十二)交互组件Selectable
最少换乘次数
torch. logical_ And() method
(十三)文本渲染Text
Chapter VI - procedures with multiple segments
[open source project] easycmd command graphical software
RuntimeError:Input and parameter tensors are not at the same device, found input tensor at cuda:0 an
2022.6.9-----leetcode. four hundred and ninety-seven
Load the source code of 2D 3D virtual anchor in the web page (1: project introduction and source code)
Chapter 8 - two basic problems of data processing
top命令含义
Flink传入自定义的参数或配置文件
The database doesn't know what went wrong