主頁 > 知識庫 > servlet中session簡介和使用例子

servlet中session簡介和使用例子

熱門標(biāo)簽:電銷機器人是有一些什么技術(shù) 商洛電銷 四川保險智能外呼系統(tǒng)商家 北票市地圖標(biāo)注 地圖標(biāo)注線上教程 高德地圖標(biāo)注樣式 杭州語音電銷機器人軟件 杭州ai語音電銷機器人功能 電銷機器人好賣么

HttpServletRequest有兩個重載的getSession()方法,一個接受一個boolean的類型的值,另一個不帶任何參數(shù),getSession()方法和getSession(true)方法功能一樣,就是如果對應(yīng)的客戶端已經(jīng)產(chǎn)生過一個session,那么就會返回這個舊的session,否則,這個方法將會產(chǎn)生一個session ID并且和對應(yīng)的客戶端綁定在一起,而如果getSession(false)表示如果對應(yīng)的客戶端已經(jīng)有對應(yīng)的session,那么返回這個舊的session,否則不會產(chǎn)生新的session。可以使用HttpSession對象上的isNow()方法來判定這個session是否為新建的

HttpSession常用方法

public void setAttribute(String name,Object value)
將value對象以name名稱綁定到會話

public object getAttribute(String name)
取得name的屬性值,如果屬性不存在則返回null

public void removeAttribute(String name)
從會話中刪除name屬性,如果不存在不會執(zhí)行,也不會拋處錯誤.

public Enumeration getAttributeNames()
返回和會話有關(guān)的枚舉值

public void invalidate()
使會話失效,同時刪除屬性對象

public Boolean isNew()
用于檢測當(dāng)前客戶是否為新的會話

public long getCreationTime()
返回會話創(chuàng)建時間

public long getLastAccessedTime()
返回在會話時間內(nèi)web容器接收到客戶最后發(fā)出的請求的時間

public int getMaxInactiveInterval()
返回在會話期間內(nèi)客戶請求的最長時間為秒

public void setMaxInactiveInterval(int seconds)
允許客戶客戶請求的最長時間

ServletContext getServletContext()
返回當(dāng)前會話的上下文環(huán)境,ServletContext對象可以使Servlet與web容器進行通信

public String getId()
返回會話期間的識別號

一個保存信息到session的簡單例子

sessionlogin.html

復(fù)制代碼 代碼如下:

meta name="keywords" content="keyword1,keyword2,keyword3" />
meta name="description" content="this is my page" />
meta name="content-type" content="text/html; charset=UTF-8" />

 !--    link rel="stylesheet" type="text/css" href="./styles.css">-->/pre>
form action="servlet/saveinfo" method="post">
 用戶名:
 input type="text" name="username" /> input type="submit" />

 密碼:
 input type="password" name="userpasswd" />

 /form>
pre>

/pre>
/div>
div>

復(fù)制代碼 代碼如下:

package chap03;

import java.io.IOException;
import java.io.PrintWriter;

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 saveinfo extends HttpServlet {

/**
 * Constructor of the object.
 */
 public saveinfo() {
 super();
 }

/**
 * Destruction of the servlet.

 */
 public void destroy() {
 super.destroy(); // Just puts "destroy" string in log
 // Put your code here
 }

/**
 * The doGet method of the servlet.

 *
 * This method is called when a form has its tag value method equals to get.
 *
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {

 //如果用戶輸入過了用戶名 則將其放在session中
 if(request.getParameter("username")!=null);
 {
 HttpSession session = request.getSession();
 session.setAttribute("username",request.getParameter("username"));
 }
 response.setContentType("text/html;charset=GBK");
 PrintWriter out = response.getWriter();
 out.println("session已經(jīng)創(chuàng)建");
 out.println("
");
 out.println("跳轉(zhuǎn)到其他a>頁面/a>");

 }

/**
 * The doPost method of the servlet.

 *
 * This method is called when a form has its tag value method equals to post.
 *
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {

 doGet(request,response);
 }

/**
 * Initialization of the servlet.

 *
 * @throws ServletException if an error occurs
 */
 public void init() throws ServletException {
 // Put your code here
 }

}/pre>
/div>
div>


復(fù)制代碼 代碼如下:

package chap03;

import java.io.IOException;
import java.io.PrintWriter;

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 getsession extends HttpServlet {

/**
 * Constructor of the object.
 */
 public getsession() {
 super();
 }

/**
 * Destruction of the servlet.

 */
 public void destroy() {
 super.destroy(); // Just puts "destroy" string in log
 // Put your code here
 }

/**
 * The doGet method of the servlet.

 *
 * This method is called when a form has its tag value method equals to get.
 *
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {

response.setContentType("text/html;charset=GBK");
 PrintWriter out = response.getWriter();

 String username = "";
 //此處不是創(chuàng)建session 而是去取已經(jīng)創(chuàng)建的session
 HttpSession session = request.getSession();
 //如果已經(jīng)取到,說明已經(jīng)登錄
 if(session!=null)
 {
 username = (String)session.getAttribute("username");
 out.println("獲得創(chuàng)建的Session");
 out.println("
");
 out.println("登錄名:"+username);
 }
 else
 {
 response.sendRedirect("../sessionlogin.html");
 }
 }

/**
 * The doPost method of the servlet.

 *
 * This method is called when a form has its tag value method equals to post.
 *
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 doGet(request,response);
 }

/**
 * Initialization of the servlet.

 *
 * @throws ServletException if an error occurs
 */
 public void init() throws ServletException {
 // Put your code here
 }

}/pre>
/div>
div>/div>
div>

您可能感興趣的文章:
  • servlet Cookie使用方法詳解(六)
  • servlet之cookie簡介_動力節(jié)點Java學(xué)院整理
  • java中Servlet Cookie取不到值原因解決辦法
  • 全面了解servlet中cookie的使用方法
  • Java Servlet及Cookie的使用
  • servlet之session簡介_動力節(jié)點Java學(xué)院整理
  • servlet之session工作原理簡介_動力節(jié)點Java學(xué)院整理
  • 淺談Servlet的Cookie和Session機制

標(biāo)簽:宿州 西藏 云浮 紅河 丹東 青島 江西 貴州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《servlet中session簡介和使用例子》,本文關(guān)鍵詞  servlet,中,session,簡介,和,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《servlet中session簡介和使用例子》相關(guān)的同類信息!
  • 本頁收集關(guān)于servlet中session簡介和使用例子的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章