Home Java javaTutorial JavaWeb uses Cookie simulation to implement automatic login function (no username and password required)

JavaWeb uses Cookie simulation to implement automatic login function (no username and password required)

Jan 18, 2017 pm 02:17 PM

JavaWeb uses Cookie simulation to implement automatic login function (no username and password required)

It contains two jsp files, namely login.jsp and index.jsp

The code is as follows:

login.jsp

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>登录界面</title>

</head>

<body>

<form action="index.jsp" method="post">

用户名:<input type="text" name="name"/>

<input type="submit" value="提交"/>

</form>

</body>

</html>

Copy after login

index.jsp

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>欢迎您</title>

</head>

<body>

<%

String name = request.getParameter("name");

if(name != null && !name.trim().equals("")){

Cookie cookie = new Cookie("name",name);

cookie.setMaxAge(30); //设置cookie有效期为30s

response.addCookie(cookie);

}else{

Cookie[] cookies = request.getCookies();

if(cookies != null && cookies.length > 0){

for(Cookie cookie:cookies){

String cookieName = cookie.getName();

if("name".equals(cookieName)){

String val = cookie.getValue();

name = val;

}

}

}

}

if(name != null && !name.trim().equals("")){

out.print("hello: " + name);

}else{//否则重定向到登录界面

response.sendRedirect("login.jsp");

}

%>

</body>

</html>

Copy after login

The above is the JavaWeb use of cookies introduced by the editor I simulate the automatic login function. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!

For more articles related to JavaWeb using Cookie simulation to implement automatic login function (no user name and password required), please pay attention to the PHP Chinese website!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

Hot tools Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does Java's classloading mechanism work, including different classloaders and their delegation models? How does Java's classloading mechanism work, including different classloaders and their delegation models? Mar 17, 2025 pm 05:35 PM

How does Java's classloading mechanism work, including different classloaders and their delegation models?

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? Mar 17, 2025 pm 05:46 PM

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Mar 07, 2025 pm 06:09 PM

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte

How can I implement functional programming techniques in Java? How can I implement functional programming techniques in Java? Mar 11, 2025 pm 05:51 PM

How can I implement functional programming techniques in Java?

Iceberg: The Future of Data Lake Tables Iceberg: The Future of Data Lake Tables Mar 07, 2025 pm 06:31 PM

Iceberg: The Future of Data Lake Tables

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? Mar 17, 2025 pm 05:43 PM

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? Mar 17, 2025 pm 05:44 PM

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?

How do I use Java's NIO (New Input/Output) API for non-blocking I/O? How do I use Java's NIO (New Input/Output) API for non-blocking I/O? Mar 11, 2025 pm 05:51 PM

How do I use Java's NIO (New Input/Output) API for non-blocking I/O?

See all articles