servlet中session簡(jiǎn)介和使用例子
HttpServletRequest有兩個(gè)重載的getSession()方法,一個(gè)接受一個(gè)boolean的類型的值,另一個(gè)不帶任何參數(shù),getSession()方法和getSession(true)方法功能一樣,就是如果對(duì)應(yīng)的客戶端已經(jīng)產(chǎn)生過(guò)一個(gè)session,那么就會(huì)返回這個(gè)舊的session,否則,這個(gè)方法將會(huì)產(chǎn)生一個(gè)session ID并且和對(duì)應(yīng)的客戶端綁定在一起,而如果getSession(false)表示如果對(duì)應(yīng)的客戶端已經(jīng)有對(duì)應(yīng)的session,那么返回這個(gè)舊的session,否則不會(huì)產(chǎn)生新的session。可以使用HttpSession對(duì)象上的isNow()方法來(lái)判定這個(gè)session是否為新建的
HttpSession常用方法
public void setAttribute(String name,Object value)
將value對(duì)象以name名稱綁定到會(huì)話
public object getAttribute(String name)
取得name的屬性值,如果屬性不存在則返回null
public void removeAttribute(String name)
從會(huì)話中刪除name屬性,如果不存在不會(huì)執(zhí)行,也不會(huì)拋處錯(cuò)誤.
public Enumeration getAttributeNames()
返回和會(huì)話有關(guān)的枚舉值
public void invalidate()
使會(huì)話失效,同時(shí)刪除屬性對(duì)象
public Boolean isNew()
用于檢測(cè)當(dāng)前客戶是否為新的會(huì)話
public long getCreationTime()
返回會(huì)話創(chuàng)建時(shí)間
public long getLastAccessedTime()
返回在會(huì)話時(shí)間內(nèi)web容器接收到客戶最后發(fā)出的請(qǐng)求的時(shí)間
public int getMaxInactiveInterval()
返回在會(huì)話期間內(nèi)客戶請(qǐng)求的最長(zhǎng)時(shí)間為秒
public void setMaxInactiveInterval(int seconds)
允許客戶客戶請(qǐng)求的最長(zhǎng)時(shí)間
ServletContext getServletContext()
返回當(dāng)前會(huì)話的上下文環(huán)境,ServletContext對(duì)象可以使Servlet與web容器進(jìn)行通信
public String getId()
返回會(huì)話期間的識(shí)別號(hào)
一個(gè)保存信息到session的簡(jiǎn)單例子
sessionlogin.html
<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>
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 {
//如果用戶輸入過(guò)了用戶名 則將其放在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>頁(yè)面</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>
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)取到,說(shuō)明已經(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ǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
- java中Servlet Cookie取不到值原因解決辦法
- 全面了解servlet中cookie的使用方法
- Java Servlet及Cookie的使用
- servlet之session簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
- servlet之session工作原理簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
- 淺談Servlet的Cookie和Session機(jī)制
相關(guān)文章
jsp項(xiàng)目中更改tomcat的默認(rèn)index.jsp訪問(wèn)路徑的方法
如何更改tomcat的默認(rèn)index.jsp訪問(wèn)路徑,jsp的工程下有一個(gè)叫做WEB-INF文件夾下的web.xml打開(kāi)它,按照下面的方法即可修改2013-11-11
JSP入門(mén)教程之客戶端驗(yàn)證、常用輸出方式及JSTL基本用法
這篇文章主要介紹了JSP入門(mén)教程之客戶端驗(yàn)證、常用輸出方式及JSTL基本用法,較為詳細(xì)的分析了JSP實(shí)現(xiàn)客戶端驗(yàn)證的方法、常用輸出方式及JSTL基本用法,并輔以實(shí)例說(shuō)明,需要的朋友可以參考下2015-09-09
ssi框架學(xué)習(xí)總結(jié)(mvc三層架構(gòu))
相信大家對(duì)于mvc的三層架構(gòu)已經(jīng)灰常熟悉了,在這就不細(xì)講了,個(gè)人感覺(jué)ssi的框架結(jié)構(gòu)還是比較典型的mvc三層架構(gòu),還是比較容易上手的2014-09-09
JSP XMLHttpRequest動(dòng)態(tài)無(wú)刷新及其中文亂碼處理
最近用到了XMLHttpRequest 動(dòng)態(tài)無(wú)刷新技術(shù) 不刷新當(dāng)前頁(yè)面發(fā)送請(qǐng)求,并得到返回結(jié)果 主要是jsp頁(yè)面內(nèi)的js與后臺(tái)的servlet交互,返回值為文本~~~2009-07-07
JSP動(dòng)態(tài)網(wǎng)頁(yè)開(kāi)發(fā)技術(shù)概述
這篇文章主要介紹了JSP動(dòng)態(tài)網(wǎng)頁(yè)開(kāi)發(fā)技術(shù)概述,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
JSP for循環(huán)中判斷點(diǎn)擊的是哪個(gè)按鈕
做留言板時(shí)遇到數(shù)據(jù)庫(kù)中for循環(huán),判斷點(diǎn)擊的是哪個(gè)按鈕的情況,下面是具體的實(shí)現(xiàn),大家可以參考下2014-04-04
jsp之間傳參數(shù)接受中文有亂碼問(wèn)題解決方法
這篇文章主要介紹了jsp之間傳參數(shù)接受中文有亂碼問(wèn)題解決方法,需要的朋友可以參考下2014-06-06
jsp中獲取狀態(tài)怎么寫(xiě)(兩種實(shí)現(xiàn)方式)
由于框架是2005年的,jsp中不能存放標(biāo)簽,只能有java代碼來(lái)寫(xiě)了,接下來(lái)為大家介紹下兩種實(shí)現(xiàn)獲取狀態(tài)寫(xiě)法,感興趣的各位可以參考下哈,希望可以幫助到你2013-04-04
jsp include引用非本級(jí)目錄網(wǎng)頁(yè)實(shí)現(xiàn)代碼
include的出現(xiàn)方便了文件之間的引用,降低了開(kāi)發(fā)難度.它的常用法是引用同級(jí)目錄,本文主要介紹引用非本級(jí)目錄網(wǎng)頁(yè),如果沒(méi)有遇到這樣情況下引用的朋友可以參考下,或許本文對(duì)你有所幫助2013-02-02

