HttpSession的简单使用
来源:互联网 发布:如何评价周杰伦知乎 编辑:程序博客网 时间:2024/05/05 23:39
HttpSession常用方法
void
setAttribute(String name, Object value)
设置属性值
Object
getAttribute(String name)
获取上面设定的值,注意反回的是Object,需要进行(强制转化)
void
invalidate()
解除session的绑定
代码
<body><p>请输入用户名和密码:</p> <form id="login" method="post" action="./abc/LoginServlet">用户名:<input type="text" name="username" width="100"/><br/>密码:<input type="password" name="password" width="100"/> <br/><input type="submit" name="登录" value="上交表单"></form></body>
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubString encoding;ServletConfig config=this.getServletConfig();encoding=config.getInitParameter("encoding");request.setCharacterEncoding(encoding);response.setContentType("text/html;charset="+encoding);//设置编码PrintWriter out=response.getWriter();//获取输出到网页的对象HttpServletRequest requ=(HttpServletRequest)request;HttpSession session=requ.getSession();//获得sessionString uesrValue=(String)session.getAttribute("user");//获取session存储的属性值String params[]=session.getValueNames();//获得session里所有的属性名out.println("session中参数的个数:"+params.length+"<br/>");if(uesrValue==null){//还没有登录过String username=request.getParameter("username");//从表单中获取用户名String password=request.getParameter("password");//从表单中获取密码out.println("你还未登录<br/>");session.setAttribute("user", username);//设定session中的用户名session.setAttribute("password",password);//设定session中的密码out.println(session.getId());//打印session ID}else{String username=(String)session.getAttribute("user");//从session中获取用户名String password=(String)session.getAttribute("password");//从session获取密码out.println("用户名:"+username+"<br/>");out.println("密 码:"+password+"<br/>");session.invalidate();//使session无效}out.close();//Cookie里不能有中文//Cookie cookie1=new Cookie("usrname","bcd");//Cookie cookie2=new Cookie("password","buzhidao");//response.addCookie(cookie1);//response.addCookie(cookie2);//response.sendRedirect("/JavaEEDemo/first.jsp");}
运行结果
登录界面
第一次登录
第二次登录
由于在第二次登录后,使session无效了,所以第三次与第一次登录session的id不同。
更多方法介绍请参阅http://docs.oracle.com/javaee/7/api/
0 0
- HttpSession的简单使用
- HttpSession的使用
- 1、Spring Session-使用Redis的HttpSession
- 2、Spring Session-使用JDBC的HttpSession
- 3、Spring Session-使用Hazelcast的HttpSession
- JavaWeb_使用Servlet + HttpSession实现简单用户登陆
- JSP HttpSession 基本使用
- Flex Remote Object中直接使用HttpSession的方法
- Flex Remote Object中直接使用HttpSession的方法
- HttpSession 常使用的方法与实现登录
- ServletContextListener监听器,HttpSession监听器,ServletRequestListener监听器的使用
- httpSession的正确理解
- httpsession的api
- httpSession的正确理解
- HttpSession 的正确理解
- HttpSession相关的监听器
- HttpSession的正确理解
- HttpSession相关的监听器
- gvim中Mapping already in use: "<LocalLeader>is", mode "n"错误解决
- python开发工作环境
- 包及多态
- Codeforces Round #235 (Div. 2)
- UVa:1449 Dominating Patterns(AC自动机)
- HttpSession的简单使用
- 对C++容器的增加篇:circular_buffercircular_buffer/any
- C674x笔记(零)——内容介绍
- 处理大并发之二 对epoll的理解,epoll客户端服务端代码
- LeetCode Path Sum II
- 搭建Hadoop 2.2.0 和 HBase 0.94.14 集成
- shell编程之sed
- C674x笔记(一)——C674x框架概述
- 这个问题呀?