1. 程式人生 > >session的用法

session的用法

得到 sof ttr pla alt request .org oca ont

技術分享
 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的用法