java session操作
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class DemoSessionServlet extends HttpServlet {
private static final long serialVersionUID = 3144436730239445373L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
Integer counter = (Integer) session.getAttribute("counter");
if (counter == null) {
counter = new Integer(1);
} else {
counter = new Integer(counter.intValue() + 1);
}
session.setAttribute("counter", counter);
session.setMaxInactiveInterval(3600);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>session demo servlet</title></head>");
out.println("<body>");
out.println("<h1>使用session的演示</h1>");
out.println("你已經訪問了" + counter + "次.<br><br>");
if (session.isNew()) {
out.println("這是您第一次訪問本站<br><br>");
} else {
out.println("歡迎再次回來.<br><br>");
}
out.println("jsessionid: " + session.getId() + "<br><br>");
out.println("LastAccessedTime: " + session.getLastAccessedTime()
+ "<br>");
out.println("格式轉換以後的LastAccessedTime: "
+ new Date(session.getLastAccessedTime()) + "<br><br>");
out.println("CreationTime: " + session.getCreationTime() + "<br>");
out.println("格式轉換以後的CreationTime: "
+ new Date(session.getCreationTime()) + "<br><br>");
out.println("MaxInactiveInterval: " + session.getMaxInactiveInterval()
+ "秒<br>");
out.println("<br>使用Session物件的getAttributeNames<br>");
Enumeration<String> enumer = session.getAttributeNames();
String name = null;
while (enumer.hasMoreElements()) {
name = (String) enumer.nextElement();
out.println("Session name:" + name + "<br>");
out.println("Session value:" + session.getAttribute(name) + "<br>");
}
//移除指定的元素
session.removeAttribute("counter");
//讓session失效
session.invalidate();
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
}
import java.io.PrintWriter;
import java.sql.Date;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class DemoSessionServlet extends HttpServlet {
private static final long serialVersionUID = 3144436730239445373L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
Integer counter = (Integer) session.getAttribute("counter");
if (counter == null) {
counter = new Integer(1);
} else {
counter = new Integer(counter.intValue() + 1);
}
session.setAttribute("counter", counter);
session.setMaxInactiveInterval(3600);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>session demo servlet</title></head>");
out.println("<body>");
out.println("<h1>使用session的演示</h1>");
out.println("你已經訪問了" + counter + "次.<br><br>");
if (session.isNew()) {
out.println("這是您第一次訪問本站<br><br>");
} else {
out.println("歡迎再次回來.<br><br>");
}
out.println("jsessionid: " + session.getId() + "<br><br>");
out.println("LastAccessedTime: " + session.getLastAccessedTime()
+ "<br>");
out.println("格式轉換以後的LastAccessedTime: "
+ new Date(session.getLastAccessedTime()) + "<br><br>");
out.println("CreationTime: " + session.getCreationTime() + "<br>");
out.println("格式轉換以後的CreationTime: "
+ new Date(session.getCreationTime()) + "<br><br>");
out.println("MaxInactiveInterval: " + session.getMaxInactiveInterval()
+ "秒<br>");
out.println("<br>使用Session物件的getAttributeNames<br>");
Enumeration<String> enumer = session.getAttributeNames();
String name = null;
while (enumer.hasMoreElements()) {
name = (String) enumer.nextElement();
out.println("Session name:" + name + "<br>");
out.println("Session value:" + session.getAttribute(name) + "<br>");
}
//移除指定的元素
session.removeAttribute("counter");
//讓session失效
session.invalidate();
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
}
相關文章
- 用thinkphp操作sessionPHPSession
- Java SessionJavaSession
- laravel操作session和cookieLaravelSessionCookie
- JAVA類中透過Servlet操作JSP中Session變數方法JavaServletJSSession變數
- nodejs操作session和cookieNodeJSSessionCookie
- servlet,filter,sessionListener,cookie,session操作ServletFilterSessionCookie
- Java學習--Cookie 和sessionJavaCookieSession
- Java Hibernate 之 Session 狀態JavaSession
- 利用v$session_longops監控長操作SessionGo
- Java操作WordJava
- java操作excelJavaExcel
- JAVA操作XMLJavaXML
- Java操作MongoDBJavaMongoDB
- Java 操作PDFJava
- 利用v$session_longops監控long RUN操作SessionGo
- Java Servlet session實現登入退出JavaServletSession
- Java Web之Cookie和Session的理解JavaWebCookieSession
- 【菜鳥學Java】11:Session技術JavaSession
- Java檔案操作 讀寫操作Java
- Django筆記三十二之session登入驗證操作Django筆記Session
- [zt] 用v$session_longops監控long RUN操作SessionGo
- 利用v$session_longops監控long RUN操作(轉)SessionGo
- 第74節:Java中的Cookie和SessionJavaCookieSession
- java web 中分散式 session 的實現JavaWeb分散式Session
- Java學習-jsp內建物件SessionJavaJS物件Session
- java操作excel表格JavaExcel
- redis+java操作RedisJava
- Java IO流操作Java
- JAVA 操作檔案Java
- Java操作符Java
- Java 操作 JSONJavaJSON
- java 檔案操作Java
- Java 的 流操作Java
- java操作ftp serverJavaFTPServer
- java時間操作Java
- Java API操作ESJavaAPI
- java操作milvusJava
- ejb 的session bean 和儲存在servlet session中的一個java物件有何不同?SessionBeanServletJava物件