servlet中session简介和使用例子
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>
上一篇: Photoshop制作唯美雨景动画
推荐阅读
-
servlet中session简介和使用例子
-
微信小程序授权 获取用户的openid和session_key【后端使用java语言编写】,我写的是get方式,目的是测试能否获取到微信服务器中的数据,后期我会写上post请求方式。
-
flask框架中的cookie和session使用
-
也谈session在JSP和Servlet中不一致问题
-
Servlet中怎么取得session和application
-
使用SQL Mail收发和自动处理邮件中的扩展存储过程简介
-
php中如何同时使用session和cookie来保存用户登录信息
-
iOS开发中Date Picker和UITool Bar控件的使用简介
-
Nodejs中的JWT和Session的使用
-
servlet中session简介和使用例子