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

servlet中session簡介和使用例子

HttpServletRequest有兩個重載的getSession()方法,一個接受一個boolean的類型的值,另一個不帶任何參數(shù),getSession()方法和getSession(true)方法功能一樣,就是如果對應的客戶端已經(jīng)產(chǎn)生過一個session,那么就會返回這個舊的session,否則,這個方法將會產(chǎn)生一個session ID并且和對應的客戶端綁定在一起,而如果getSession(false)表示如果對應的客戶端已經(jī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()
返回和會話有關的枚舉值

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

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

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

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

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

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

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

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

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

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 {

 //如果用戶輸入過了用戶名 則將其放在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("跳轉到其他<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>


復制代碼 代碼如下:
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>

jsp技術servlet中session簡介和使用例子,轉載需保留來源!

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

主站蜘蛛池模板: 成人国产在线不卡视频 | 成人伦理影院 | 欧美性动漫3d在线观看完整版 | 亚洲中文字幕无码爆乳APP | AV午夜午夜快憣免费观看 | 亚洲成人在线免费观看 | 99精彩视频在线观看 | 内射人妻骚骚骚 | 男人桶女人j的视频在线观看 | 成人毛片18岁女人毛片免费看 | 99re久久热最新地址一 | 亚洲中文字幕在线第六区 | 午夜影院费试看黄 | 日日碰狠狠躁久久躁77777 | 袖珍人与大黑人性视频 | 快播理伦片 | 试看做受120秒免费午夜剧场 | 一个人在线观看免费高清视频在线观看 | 亚洲欧美综合在线中文 | 国产精品亚洲精品久久国语 | 99久久久久精品国产免费麻豆 | 国产在线一区观看 | 国产成人久久精品激情 | 日本乱子伦一区二区三区 | 美女禁处受辱漫画 | 伦理片飘花免费影院 | 麻豆AV福利AV久久AV | 內射XXX韩国在线观看 | 东北疯狂xxxxbbbb中国 | 欧美午夜精品久久久久久浪潮 | 久久久综合中文字幕久久 | 无人影院在线播放视频 | 久久国产露脸老熟女熟69 | 亚洲日韩在线观看 | 噜噜噜狠狠夜夜躁 | 久久久久国产精品嫩草影院 | av视频在线免播放观看 | 久久免费高清 | 成人中文在线 | 国产午夜久久影院 | 亚洲精品www久久久久久久软件 |