JSP application和session作用域 (实现application实现计数器和session实现cookie禁用登录)

news/2024/5/18 15:27:43 标签: java, cookie, session, jsp, 数据库

文章目录

  • 一、application作用域
    • application实现计数器
  • 二、session作用域


一、application作用域

application作用域中存储的数据所有的客户端是共享的。
当项目从服务器移走,或者服务关闭后重新启动,application才失效。

application实现计数器

实现的jsp代码

java"><% 
		Object str = application.getAttribute("count");
		if(str==null){
		//当第一个人访问时,将初始值为1
			application.setAttribute("count", 1);
		}else{
		//当不是第一个人时,先将类型转换,再加一
			Integer i = (Integer)str;
			application.setAttribute("count", i+1);
		}	
%>
	<p>当前访问的人数为:<%=application.getAttribute("count") %></p>

session_41">二、session作用域

session意思是会话,客户端和服务器端建立的一次会话。
打开浏览器,在地址栏输入url地址访问服务器的时候,会话创建成功了。即在服务器端存储也在客户端存储。

存储的内容是:sessionid。
客户端存储一份,服务器端存一份
sessionid的获取主要是通过cookie,浏览器允许cookie服务器就会在客户端写入。

  1. 对于cookie可用时

    session.setAttribute("username", name);
    response.sendRedirect(url);
    
就会产生新的问题: 在客户端,禁用cookie,还能正常使用session吗?
  1. 对于cookie禁用时

    session.setAttribute("username", name);
    String address = request.getContextPath()+"需要跳转的url";
    String url = response.encodeURL(address); 
    response.sendRedirect(url);
    

    1 获取新的访问路径: request.getContextPath()
    2 通过: response.encodeURL() 生成新的jsessionid

sessioncookie_70">session实现cookie禁用登录

需要三个jsp文件 login,dologin,welcome;
login

java"><%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		String address =  request.getContextPath()+"/dologin.jsp";
	//带jsessionid的url
		String url = response.encodeURL(address);
	%>
	<form action="<%=url %>" method="post">
		<table>
			<tr>
				<td>用户名:<input type="text" name="username"/></td>
			</tr>
			<tr>
				<td>&nbsp;&nbsp;&nbsp;密码:<input type="password" name="pwd"/></td>
			</tr>
			<tr>
				<td >
					<input type="submit" name="login" value="登录"/>
					<input type="button" name="exit" value="退出"/>
				</td>
			</tr>
		</table>
	</form>
</body>
</html>

dologin

java"><%@page import="com.openlab.dao.UserDao"%>
<%@page import="com.openlab.bean.User"%>
<%@page import="com.openlab.dao.imp.UserDaoImp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
			//1 设置格式
			response.setCharacterEncoding("UTF-8");		
			//2 获得参数
			String name = request.getParameter("username");
			String pwd = request.getParameter("pwd");
			//UserDao是数据库与用户登录之间的接口,实现类在UserDaoImp中
			UserDao ud = new UserDaoImp();
			//组件user对象 ,testName是校验数据库中的用户信息
			User user = ud.testName(name);
			//3 判断是否为空
			if(user!=null){
				//用户名正确
				if(pwd.equals(user.getPwd())){
				//密码正确
				//登录成功后
				session.setAttribute("username", name);
				String address = request.getContextPath()+"/welcome.jsp";
				String url = response.encodeURL(address);
				response.sendRedirect(url);
				}else{
				//密码不正确
				response.sendRedirect("login.jsp");
				}			
			}else{
			//用户名为错误
			response.sendRedirect("login.jsp");
			}
	%>
</body>
</html>

welcome

java"><%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

	<%	int i=0;
		Object name = session.getAttribute("username");
		//防止不登录,直接进入欢迎页面
		if(name==null){
			response.sendRedirect("login.jsp");
		}
	%>
		<p>欢迎您:<%=name%></p>
		
</body>
</html>

http://www.niftyadmin.cn/n/1803454.html

相关文章

杯子的对话

两个儿子在客厅打闹&#xff0c;一个是猪猪侠&#xff0c;一个是奥特曼。 茶几跑到沙发&#xff0c;沙发窜到茶几。 “啪--”的一声&#xff0c;茶几上的水杯掉地上&#xff0c;碎了一地玻璃渣。 儿子嗫嗫的看着我&#xff1a;“嗷&#xff0c;不好意思。” 我问&#xff1a;“…

使用SwitchHosts时提示“切换hosts失败!没有修改‘C:\WINDOWS\system32\drivers\etc\hosts‘的权限问题

问题描述&#xff1a; 使用Nginx时&#xff0c;使用SwitchHosts对域名的修改进行测试时&#xff0c;出现的无法切换Hosts的问题。 原因分析&#xff1a; 除了需要修改Host文件的权限以后&#xff0c;如果提示无法修改&#xff0c;则&#xff1a; 需要以管理员身份运行SwitchH…

4.16 | 学习笔记

本题注意两件事情&#xff1a; 1.对于阶乘数组&#xff0c;直接存起来&#xff0c;不要再写一个阶乘函数。。。 2.特判零&#xff0c;否则什么也不会输出。 转载于:https://www.cnblogs.com/MissCold/p/10721073.html

Java - BlockingQueue

https://juejin.im/post/5aeebd02518825672f19c546 https://www.infoq.cn/article/java-blocking-queue blockingQueue 基本操作&#xff1f; 阻塞队列的四种阻塞处理方法&#xff1f; 常用的blockingqueue? SynchronousQueue特性&#xff1f; 什么叫公平访问队列&#xff1f;…

SpringBoot连接虚拟机中Redis的五个关键点

文章目录前言一、导入pom文件二、配置文件&#xff08;application.yml中&#xff09;三、启动类增加EnableCaching四、创建配置类&#xff08;关键&#xff09;五、注解添加前言 springboot项目中引入redis的几个关键点&#xff0c;开始之前要确保redis所在虚拟机在开启状态。…

使用Spring Boot接受HTTP GET/POST请求的一个SQL并返回结果

这里说的意思是&#xff1a;我向我的Web服务器发送一个请求&#xff08;因为GET请求的一些限制&#xff0c;所以最好使用POST请求&#xff0c;不过这里作为测试还是使用GET请求&#xff09;&#xff0c;请求中带一个sql参数&#xff0c;它对应查询的数据。然后我的Spring Boot服…

Keras实现LSTM

一、先看一个Example 1、描述&#xff0c;输入为一个字母&#xff0c;输出为这个字母的下一个顺序字母 A->BB->CC->D2、Code import numpy from keras.models import Sequential from keras.layers import Dense from keras.layers import LSTM from keras.utils impo…

解决浏览器访问GitHub响应时间长,速度慢的问题

文章目录前言一、设置域名ip二、注释Host文件其他内容&#xff08;关键&#xff09;注&#xff1a;推荐使用SwitchHost软件三、刷新DNS前言 提示&#xff1a;访问Git慢的解决方案&#xff0c;只需设置域名ip即可。 一、设置域名ip 进入网址 199.232.69.194就是需要修改的地址…