天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

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
復(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 {

 //如果用戶輸入過(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>


復(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)取到,說(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>

jsp技術(shù)servlet中session簡(jiǎn)介和使用例子,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 99在线观看视频免费 | 精品无码久久久久久动漫 | 色欲人妻无码AV精品一区二区 | 综合一区无套内射中文字幕 | 麻豆国产96在线日韩麻豆 | 久久精品国产免费播高清无卡 | 亚洲人成7777 | 国产成人精品久久久久婷婷 | 亚婷婷洲AV久久蜜臀无码 | 国内精品视频久久久久免费 | 亚洲色综合中文字幕在线 | 国产精品久久久久久免费字体 | 国产激情视频在线观看 | 一边啪啪的一边呻吟声口述 | 伊人无码高清 | 97在线看视频福利免费 | 三级黄色在线观看 | 欧美一区二区视频高清专区 | 中文字幕亚洲乱码熟女在线 | 日本aa大片 | 欧美高跟镣铐bdsm视频 | 亚洲国产精品一区二区动图 | 中文字幕在线观看网站 | 久久久精品成人免费看 | 国产一区二区三区影院 | QVOD理论| 花蝴蝶免费观看影视 | 夜夜躁日日躁狠狠 | 男人J进入女人P免费狂躁 | 我的年轻漂亮继坶三级 | 原神美女被超污app 御姐被吸奶 | 杨幂视频在线观看1分30秒 | 一区二区三区国产亚洲网站 | 国产亚洲中文字幕视频 | 青青草原成人 | 国产大片51精品免费观看 | 亚洲精品AV无码喷奶水糖心 | 九九久久国产精品大片 | 国产97精品久久久天天A片 | 无码不卡中文字幕在线观看 | 亚洲男人片片在线观看 |