亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

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

系統 2154 0

用?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
?}

}

<v:shapetype o:spt="75" coordsize="21600,21600" filled="f" stroked="f" id="_x0000_t75" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t">?下圖為我在my eclipse 下創建的Web project 結構圖:</v:shapetype>

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

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


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久久久久久久久福利 | 国产在线激情 | 国产精品免费大片 | 精品亚洲欧美中文字幕在线看 | 精品视频免费 | 奇米影视777中文久久爱 | 中文字幕国产专区 | 久久精品一区二区国产 | 欧美日韩乱码毛片免费观看 | www.狠狠| 久久婷婷综合在线视频观看6 | 国产香蕉98碰碰久久人人 | 国产精品视频一区牛牛视频 | 俄罗斯一级毛片aaaa | 午夜看毛片| 国产成人精品日本亚洲语言 | 免费的成人a视频在线观看 免费的黄色网 | 国产96福利视频在线观看 | 99久久精品免费 | 日韩三级 | 久久国产精品国语对白 | 久久婷婷色一区二区三区 | 日本久久久久中文字幕 | 99精品网站 | 人色在线视频 | 女人18一级特级毛片免费看 | 欧美激情在线免费 | 成人ab片 | 亚洲国产欧美国产综合一区 | 一级肉体毛片视频免费看看 | 国产一区二区三区免费 | 亚洲小视频| 国产一级在线免费观看 | 男人天堂成人 | 美女美女高清毛片视频 | 国产乱码亚洲精品一区二区 | 久久国产乱子伦精品免费看 | 在线亚洲黄色 | 成人网欧美亚洲影视图片 | 欧美jizzhd欧美巨大 | 日韩在线看片中文字幕不卡 |