session的用法

Posted 初夏的一棵歪脖子树

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了session的用法相关的知识,希望对你有一定的参考价值。

 

技术分享
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <script type="text/javascript" src="../js/jquery-1.7.2.js"></script>
10 <script type="text/javascript">
11 $(function(){
12     $(":button").click(function(){
13         document.getElementsByTagName("form")[0].submit();
14     });
15     
16 })
17 </script>
18 <body>
19 <form action="../loginSrevlet" method="post">
20 用户名:<input type="text" name="userName"/><br/>
21 密码:<input type="password" name="password"/><br/>
22 <button type="button">登录</button>
23 <button type="reset">重置</button>
24 </form>
25 </body>
26 </html>
View Code

 

技术分享
 1 package com.zdsofe.servlet1;
 2 
 3 import java.io.IOException;
 4 import java.util.ArrayList;
 5 import java.util.Enumeration;
 6 import java.util.List;
 7 
 8 import javax.servlet.ServletException;
 9 import javax.servlet.annotation.WebServlet;
10 import javax.servlet.http.HttpServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 import javax.servlet.http.HttpSession;
14 
15 /**
16  * Servlet implementation class loginSrevlet
17  */
18 @WebServlet("/loginSrevlet")
19 public class loginSrevlet extends HttpServlet {
20     private static final long serialVersionUID = 1L;
21        
22     /**
23      * @see HttpServlet#HttpServlet()
24      */
25     public loginSrevlet() {
26         super();
27         // TODO Auto-generated constructor stub
28     }
29 
30     /**
31      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
32      */
33     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
34         // TODO Auto-generated method stub
35     }
36 
37     /**
38      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
39      */
40     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
41         //编码格式
42         request.setCharacterEncoding("utf-8");
43         response.setCharacterEncoding("utf-8");
44         response.setContentType("text/heml charset=utf-8");
45         
46         //得到表单的数据
47        Enumeration<String > name=request.getParameterNames();
48        List<String> list=new ArrayList<String>();
49        while(name.hasMoreElements())
50        {
51            list.add(request.getParameter(name.nextElement()));
52        }
53        
54        //创建Session对象
55        HttpSession ss= request.getSession();
56        ss.setAttribute("uName", list.get(0));
57        response.sendRedirect("/webProject1/jsp/loginWelcome.jsp");
58        
59     }
60 
61 }
View Code

 

技术分享
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <!--  实现3秒后跳转功能-->
 8 <!-- <meta content="3;url=loginBottom.jsp" http-equiv="refresh"> -->
 9 <title>Insert title here</title>
10 </head>
11 <body>
12 <%=session.getAttribute("uName") %>
13 <button onclick="location.href=‘loginBottom.jsp‘">跳转</button>
14 三秒后跳转....
15 </body>
16 </html>
View Code

 

技术分享
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 Welcome!
11 </body>
12 </html>
View Code

 

以上是关于session的用法的主要内容,如果未能解决你的问题,请参考以下文章

SQL Select 语句的用法

session与cookie的用法

php.ini 的 session.save_path 的正确用法是啥?

requests中session的用法

jquery中的$的特殊用法

Xcode中的变量模板(variable template)的用法