JSP built-in objects Session and application

One, session

1. Overview : The Session object is an HttpSession object related to the request, which encapsulates all information pertaining to the client ’s response. The ID of the Session object refers to the session object generated by the JSP engine when a client visits a JSP page on the server for the first time. assign a character type of ID, JSP engine while the ID number will be sent to the client, stored in a cookie in

2. To put it simply , the Session server gives the client an ID and a browser an ID, so that a one-to-one correspondence is established between the session object and the client

3. For example : when the user puts the item in the shopping cart, the user can store the product information selected in the session, and when the payment and other operations are required, the information in the session can be taken out

Serial number Method name effect
01 getAttribute(String name) Get the attribute with the specified name
02 getAttributeNames() Return each property of the object stored in the object back into session
03 getCreationTime() Returns the creation time of the session object
04 getId() Returns the number of the current session object
05 getLastAccessedTime Returns the last time the current session object was operated
06 getMaxInactiveInterval() Returns the lifetime of the session object
07 removeAttribute(String name) Delete the attribute value and attribute name of the specified attribute
08 setAttribute(String name,Object obj) Set the attribute of the specified name
09 Invalidate() Cancel the current session
10 isNew() Determine if it is a new session

4. Recommend a few good site
exercises for built-in object sessions

```login.html记录用户表单
<meta http-equiv="Content-Type" content="text/html charset=UTF-8">
<html>
<head>
<title>用户登录</title>
</head>
<body>
<form method="POST" action="login1.jsp">
<p>用户名:<input type="text" name="user" size="18"></p>
<p>密码:<input type="text" name="pass" size="18"></p>
<p><input type="submit" value="提交" name="ok">
<input type="reset" value="重置" name="cancel"></p>
</from>
</body>
</html>
login1.jsp记录表单

<%@ page contentType="text/html;charest=UTF-8" pageEncoding="UTF-8"%>
<html>
<head><title>session 应用演示</title></head>
<%
if(request.getParameter("user")!=null && request.getParameter("pass")!=null)
{
String strName=request.getParameter("user");
String strPass=request.getParameter("pass");
if(strName.equals("QIE") && strPass.equals("QIE"))
{
session.setAttribute("login","OK");
session.setAttribute("me",strName);
response.sendRedirect("welcome.jsp");
}
else{
out.println("<h2>登录错误,请输入正确的用户名和密码</h2>");
}
}
%>
</html>

session读取

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head><title>欢迎光临</title></head>
<body>
<%
String strLogin=(String)session.getAttribute("login");
String strUser=(String)session.getAttribute("me");
if(strLogin==null)
{
out.println("<h2>请先登录,谢谢!<h2>");
out.println("<h2>5秒钟后,自动跳转到登录页面!</h2>");
response.setHeader("Refresh","5;URL=login1.html");
}
else
{
if(strLogin.equals("OK"))
{
out.println(strUser+"<h2>欢迎进入我们的网站!</h2>");
}
else
{
out.println("<h2>用户名或密码错误,请重新登录!<h2>");
out.println("<h2>5秒钟后,自动跳转到登录页面!</h2>");
response.setHeader("Refresh","5;URL=login1.html");
}
}
%>
</body>
</html>

二,application

1. Overview: session is to store user information, then application is to store server information. When the server is shut down, the application life cycle ends.

2. Instance application counter

创建applicationdemo1.jsp
<%@ page contentType="text/html;charest=UTF-8" %>
<html>
<head><title>application应用演示</title></head>
<body>
<% 
application.setAttribute("user","QIE");
application.setAttribute("pass","QIE518");
%>
<jsp:forward page="applicationdemo2.jsp"/>
</body>
</html>
创建applicationdemo2.jsp
<%@ page contentType="text/html;charest=UTF-8;pageEncoding=UTF-8" %>
<html>
<head><title>application应用演示</title></head>
<body>
<% 
String strName=(String) application.getAttribute("user");
String strPass=(String) application.getAttribute("pass");
out.println("user="+strName);
out.println("pass="+strPass);
%>
</body>
</html>
创建applicationdemo3.jsp
<%@ page contentType="text/html;charset=UTF-8" import="java.util.Date"%>
<html>
<head><title>计数器应用</title></head>
<body>
<center>
<font size=5 color=blue >application计数器</font>
</center>
<hr>
<%
String strNum=(String) application.getAttribute("num");
int num=0;
if(strNum !=null)
num=Integer.parseInt(strNum)+1;

application.setAttribute("num", String.valueOf(num));
%>
访问次数为:
<font color=red><%=num%></font></br>
</body>
</html>

Published 15 original articles · Likes0 · Visits 141

Guess you like

Origin blog.csdn.net/qq_44230959/article/details/105428641