DDR爱好者之家 Design By 杰米

HttpServletRequest有两个重载的getSession()方法,一个接受一个boolean的类型的值,另一个不带任何参数,getSession()方法和getSession(true)方法功能一样,就是如果对应的客户端已经产生过一个session,那么就会返回这个旧的session,否则,这个方法将会产生一个session ID并且和对应的客户端绑定在一起,而如果getSession(false)表示如果对应的客户端已经有对应的session,那么返回这个旧的session,否则不会产生新的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属性,如果不存在不会执行,也不会抛处错误.

public Enumeration getAttributeNames()
返回和会话有关的枚举值

public void invalidate()
使会话失效,同时删除属性对象

public Boolean isNew()
用于检测当前客户是否为新的会话

public long getCreationTime()
返回会话创建时间

public long getLastAccessedTime()
返回在会话时间内web容器接收到客户最后发出的请求的时间

public int getMaxInactiveInterval()
返回在会话期间内客户请求的最长时间为秒

public void setMaxInactiveInterval(int seconds)
允许客户客户请求的最长时间

ServletContext getServletContext()
返回当前会话的上下文环境,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已经创建");
 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 = "";
 //此处不是创建session 而是去取已经创建的session
 HttpSession session = request.getSession();
 //如果已经取到,说明已经登录
 if(session!=null)
 {
 username = (String)session.getAttribute("username");
 out.println("获得创建的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>

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。