使用Session在JSP頁面間傳遞表單內容

來源:互聯網
上載者:User

用 Session儲存表單提交的內容,基本原理是用Session下的setAttribute()  與 getAttribute() 方法來儲存與擷取一個參數.

1.在MY ECLIPSE下建立一個WEB PROJECT,在預設路徑下建立兩個Session的JSP檔案,其中Session.JSP用來提交表單內容, Session1.JSP用來擷取由Session儲存的參數內容.

2. 建立一個servlet檔案,用以處理由Session.JSP提交的表單內容,並將其放入request.setAttribute()方法中.

註:在servlet中不能直接使用Session下的setAttribute()  與 getAttribute() 方法,在標頭檔中必須包含javax.servlet.Http.*;   建立Session對象時要先聲明這個對象,使用文法為:

Http Session session=request.getSession();

具體代碼如下:

Session.JSP

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  

  </head>
 
  <body>
  <form action="Form">&nbsp;&nbsp;
   A :<input type="text" name="a" size=5><p>&nbsp;&nbsp;
   B :<input type="text" name="b" size=5><p>&nbsp;&nbsp;
   C :<input type="text" name="c" size=5><p>&nbsp;&nbsp;
   <input type=submit value="Submit">
  </form>
  </body>
</html>

Session1.JSP

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 
 
  <body>
   <%
   List<String> array=new ArrayList();    // List 用以儲存表單中的每一個值
   array=(List<String>)request.getAttribute("array");
   out.print("<h2>Session save A,B,C :</h2><p>");  //JDK
   for(String name:array)   //JDK1.4以後的版本支援的 foreach 文法
   {
   out.println(name);
   out.print("<p>");
   }
    %>
  </body>
</html>

Form.java   // servlet檔案

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.*;
import java.util.*;
public class Form extends HttpServlet {

 /**
  * Constructor of the object.
  */
 public Form() {
  super();
 }

 /**
  * Destruction of the servlet. <br>
  */
 public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }

 /**
  * The doGet method of the servlet. <br>
  *
  * 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");
  PrintWriter out = response.getWriter();
  String a=(String)request.getParameter("a");
  String b=(String)request.getParameter("b");
  String c=(String)request.getParameter("c");
  List<String> array=new ArrayList();
  array.add(a);array.add(b);array.add(c);
  HttpSession session=request.getSession(true);
  session.setAttribute("array",array);
  request.setAttribute("array",session.getAttribute("array"));
  getServletConfig().getServletContext().getRequestDispatcher(
    "/session1.jsp").forward(request, response);
 }

 /**
  * The doPost method of the servlet. <br>
  *
  * 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. <br>
  *
  * @throws ServletException if an error occurs
  */
 public void init() throws ServletException {
  // Put your code here
 }

}

 為我在my eclipse 下建立的Web project 結構圖:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.