使用Session在JSP页面间传递表单内容
2011-04-08 00:25
429 查看
用 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
Session1.JSP
Form.java // servlet文件
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"> A :<input type="text" name="a" size=5><p> B :<input type="text" name="b" size=5><p> C :<input type="text" name="c" size=5><p> <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 } }
相关文章推荐
- 使用Session在JSP页面间传递表单内容
- 使用Session在JSP页面间传递表单内容
- 使用session在jsp页面之间传递多维数组
- 百度UEditor的简单使用,JSP页面的表单中的内容后台获取和前端配置-结合项目使用
- 在jsp页面中使用list列表来传递信息到action
- SpringMvc的使用注意事项(接收jsp页面传递过来的参数)
- 使用session在页面间传递参数
- JSP页面中文传递参数使用escape编码
- jsp页面清空表单内容
- jsp页面重置表单查询内容
- jsp中使用Session防止表单重复提交
- jsp传递中文参数、空格以及表单内容换行问题解决小结
- 使用DiskFileItemFactory在Action中获得JSP表单内容(用在有图片上传功能的表单中)
- JSP页面中文传递参数使用escape编码
- 在JSP中使用el标签传递值(两个jsp页面之间传值)
- 在Action中查询出SQL SERVER表的数据,返回一个list。怎么在JSP页面中使用Struts标签,显示出表中的内容?(转)
- JSP页面中的Form表单自动校验(使用validate插件)
- 使用JSP相关元素实现页面内容输出
- JSP页面使用富文本控件ckeditor提交表单数据
- JSP中页面跳转后获取表单信息的方法述用到(hidden)(session)