Skip to main content

浅谈JSP表单中的form传值[转]

不同JSP页面之间、不同action之间,以及JSP中form与其对应的action之间,JSP中form与其不对应的action之间如何传值。本文将介绍JSP表单中的form传值。
SP表单中的form传值
页面间链接和数据传递的三种方式
(1)通过JSP表单form将数据提交到下一个页面;
(2)通过JSP表单链接将数据提交到下一个页面;
(3)通过JSP表单Session将数据提交到后续页面,session是一次会话只要浏览器不关闭就不会关闭会话,一般默认保存30分钟可以根据自己的需要更改 。
例:
  1. 01.html  
  2. <html> 
  3. <head> 
  4. <title>利用表单传递数据</title> 
  5. </head> 
  6. <body> 
  7. <h3>利用表单传递数据</h3> 
  8. <form name="form1" method="post" action="02.jsp"> 
  9. <p>你的姓名:  
  10.      <input type="text" name="name"> 
  11. </p> 
  12. <p>你的爱好:  
  13.      <input type="text" name="hobby"> 
  14. </p> 
  15. <p>你所从事的行业:  
  16.      <select name="work"> 
  17.            <option></option>  
  18.         <option value="学生">学生</option> 
  19.         <option value="IT业">IT业</option> 
  20.         <option value="商业">商业</option> 
  21.         <option value="制造业">制造业</option> 
  22.         <option value="服务业">服务业</option> 
  23.      </select> 
  24. </p> 
  25. <p> 
  26.      <input type="Submit" value="提交"> 
  27.      <input type="Reset" value="重置"> 
  28. </p> 
  29. </form> 
  30. </body> 
  31. </html> 
  32. 02.jsp  
  33. <html> 
  34. <head> 
  35. <title>从表单中获取数据</title> 
  36. </head> 
  37. <%@ page contentType="text/html;charset=GB2312"%> 
  38. <body> 
  39. <h3>从表单中获取数据</h3> 
  40. <p> 
  41. <%  
  42.    //从表单中获取数据  
  43.    String name= request.getParameter("name");  
  44.    String hobby=request.getParameter("hobby");  
  45.    String work= request.getParameter("work");  
  46.    //验证"名字(name)"的长度是否符合要求  
  47.    if(name.length()<3||name.length()>8)  
  48.       out.println("你输入的名字长度不符合要求");  
  49.    else  
  50.    {  
  51.       out.println("你的姓名是:"+name+"<br>");  
  52.         out.println("你的爱好是:"+hobby+"<br>");  
  53.       out.println("你所从事的工作是:"+work+"<br>");  
  54.       //将"名字(name)"保存到session对象中,让后续页面(下个页面、下下个页面、……)引用  
  55.       session.setAttribute("name",name);  
  56.    }  
  57. %> 
  58. <br> 
  59.  
  60. <a href="03.jsp?hobby=<%=hobby%>&work=<%=work%>">提交</a> 
  61. </p> 
  62. <hr> 
  63. <p><font size="2">*将"名字(name)"保存到session对象中,让后续页面(下个页面、下下个页面、……)引用</font></p> 
  64. <p><font size="2">*将"爱好(hobby)"和"工作(work)"以链接的形式递给下个页面</font></p> 
  65. </body> 
  66. </html> 
  67. 3.jsp  
  68. <html> 
  69. <head> 
  70. <title>从session对象中获取"名字(name)"的值;从链接中获取"爱好(hobby)"和"工作(work)"</title> 
  71. </head> 
  72. <%@ page contentType="text/html;charset=GB2312"%> 
  73. <SCRIPT language="JavaScript"> 
  74.      function submit1()  
  75.         {  
  76.          document.forms["form1"].action="04.jsp";  
  77.         document.form1.submit();  
  78.         }  
  79.  
  80.         
  81.         function edit1()  
  82.         {  
  83.           document.forms["form1"].action="01.jsp";  
  84.        document.form1.submit();  
  85.         }  
  86. </SCRIPT> 
  87. <%@ page contentType="text/html;charset=GB2312" %> 
  88. <body> 
  89. <h3>从session对象中获取"名字(name)"的值;从链接中获取"爱好(hobby)"和"工作(work)"</h3> 
  90. <%  
  91.    String name=(String)session.getAttribute("name");  
  92.    String hobby=request.getParameter("hobby");  
  93.    String work= request.getParameter("work");  
  94.    out.println("你的姓名是:"+name+"<br>");  
  95.    out.println("你的爱好是:"+hobby+"<br>");  
  96.    out.println("你所从事的工作是:"+work+"<br>");  
  97.    //将"爱好(hobby)"和"工作(work)"保存在session对象中  
  98.    session.setAttribute("hobby",hobby);  
  99.   session.setAttribute("work",work);  
  100. %> 
  101. <form name="form1" method="post"> 
  102. <input type="hidden" name="name" value="<%=name%>"> 
  103. <input type="hidden" name="hobby" value="<%=hobby%>"> 
  104. <input type="hidden" name="work" value="<%=work%>"> 
  105. <p><h3>确认提交这些信息吗?</h3></p> 
  106. <input type="Button" name="Submit" value="确认" onClick="javascript:submit1()"> 
  107. <input type="Button" name="Edit" value="修改" onClick="javascript:edit1()"> 
  108. </form> 
  109. <hr> 
  110. <p><font size="2">*将"爱好(hobby)"和"工作(work)"保存在session对象中,让后续页面(下个页面、下下个页面、……)引用</font></p> 
  111. <p><font size="2">*由于"名字(name)"在上个页面中已经保存在session对象中了,这里没必要再次保存</font></p> 
  112. </body> 
  113. </html> 
  114. 4.jsp  
  115. <html> 
  116. <head> 
  117. <title>从sessoin对象中获取数据</title> 
  118. </head> 
  119. <%@ page contentType="text/html;charset=GB2312" %> 
  120. <body> 
  121. <h3>从sessoin对象中获取数据</h3> 
  122. <%  
  123.    //从sessoin对象中获取数据  
  124.    String name=(String)session.getAttribute("name");  
  125.    String work=(String)session.getAttribute("work");  
  126.    String hobby=(String)session.getAttribute("hobby");  
  127. %> 
  128. <p> 
  129. <font color="#0000FF"><%=name%></font>,你好!你所从事的工作是<font color="#0000FF"><%=work%></font>,在业余时间喜欢<font color="#0000FF"><%=hobby%></font>。  
  130. </p> 
  131. </body> 
  132. </html> 
  133. 1.jsp  
  134. <html> 
  135. <head> 
  136. <title>利用表单传递数据</title> 
  137. </head> 
  138. <%@ page contentType="text/html;charset=GB2312" %> 
  139. <%  
  140.    String name=request.getParameter("name");  
  141.    String hobby=request.getParameter("hobby");  
  142.    String work=new String(request.getParameter("work").getBytes("ISO8859_1"),"GBK");  
  143. %> 
  144. <body> 
  145. <h3>利用表单传递数据</h3> 
  146. <form name="form1" method="post" action="02.jsp"> 
  147. <p>你的姓名:  
  148.      <input type="text" name="name" value="<%=name%>"> 
  149. </p> 
  150. <p>你的爱好:  
  151.      <input type="text" name="hobby" value="<%=hobby%>"> 
  152. </p> 
  153. <p>你所从事的行业:  
  154.      <select name="work"> 
  155.           <%if(work.equals("学生")){%> 
  156.         <option value="学生" selected>学生</option> 
  157.         <option value="IT业">IT业</option> 
  158.         <option value="商业">商业</option> 
  159.         <option value="制造业">制造业</option> 
  160.         <option value="服务业">服务业</option> 
  161.           <%}else if(work.equals("IT业")){%> 
  162.         <option value="学生">学生</option> 
  163.         <option value="IT业" selected>IT业</option> 
  164.         <option value="商业">商业</option> 
  165.         <option value="制造业">制造业</option> 
  166.         <option value="服务业">服务业</option> 
  167.           <%}else if(work.equals("商业")){%> 
  168.         <option value="学生">学生</option> 
  169.         <option value="IT业">IT业</option> 
  170.         <option value="商业" selected>商业</option> 
  171.         <option value="制造业">制造业</option> 
  172.         <option value="服务业">服务业</option> 
  173.           <%}else if(work.equals("制造业")){%> 
  174.         <option value="学生">学生</option> 
  175.         <option value="IT业">IT业</option> 
  176.         <option value="商业">商业</option> 
  177.        <option value="制造业" selected>制造业</option> 
  178.         <option value="服务业">服务业</option> 
  179.               <%}else if(work.equals("服务业")){%> 
  180.         <option value="学生">学生</option> 
  181.        <option value="IT业">IT业</option> 
  182.         <option value="商业">商业</option> 
  183.         <option value="制造业">制造业</option> 
  184.         <option value="服务业" selected>服务业</option> 
  185.               <%}%> 
  186.      </select> 
  187. </p> 
  188. <p> 
  189.      <input type="Submit" value="提交"> 
  190.      <input type="Reset" value="重置"> 
  191. </p> 
  192. </form> 
  193. </body> 
  194. </html> 

Comments

Popular posts from this blog

Regression Testing

From  http://www.softwaretestinghelp.com/regression-testing-tools-and-methods/ The selective retesting of a  software  system that has been modified to ensure that any  bugs  have been fixed and that no other previously working functions have failed as a result of the reparations and that newly added features have not created problems with previous versions of the  software . Also referred to as  verification testing , regression testing is initiated after a  programmer  has attempted to fix a recognized problem or has added  source code  to a program that may have inadvertently introduced errors. It is a  quality  control measure to ensure that the newly modified code still complies with its specified requirements and that unmodified code has not been affected by the maintenance activity. What is Regression Software Testing? Regression means retesting the unchanged parts of the application. Test cases are re-execu...

ThreadPoolExecutor使用

From 洞玄的博客 http://dongxuan.iteye.com/blog/901689 jdk官方文档(javadoc)是学习的最好,最权威的参考。 文章分上中下。上篇中主要介绍ThreadPoolExecutor接受任务相关的两方面入参的意义和区别,池大小参数 corePoolSize和 maximumPoolSize,BlockingQueue选型( SynchronousQueue, LinkedBlockingQueue, ArrayBlockingQueue );中篇中主要聊聊与 keepAliveTime这个参数相关的话题;下片中介绍一下一些比较少用的该类的API,及他的近亲: ScheduledThreadPoolExecutor 。 如果理解错误,请直接指出。 查看JDK帮助文档,可以发现该类比较简单,继承自AbstractExecutorService,而AbstractExecutorService实现了ExecutorService接口。 ThreadPoolExecutor的完整构造方法的签名是: ThreadPoolExecutor (int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit  unit, BlockingQueue < Runnable > workQueue, ThreadFactory  threadFactory, RejectedExecutionHandler  handler)   先记着,后面慢慢解释。 ===============================神奇分割线================================== 其实对于ThreadPoolExecutor的构造函数网上有N多的解释的,大多讲得都很好,不过我想先换个方式, 从Executors这个类入手 。因为他的几个构造工厂构造方法名字取得令人很容易了解有什么特点。但是其实Executors类的底层实现便是ThreadPoolExecutor! ThreadPoolEx...

Spring MVC 3.2 Preview: Introducing Servlet 3, Async Support

Continuing the Spring 3.0 "simplification series" started by Keith and Chris , I would like to provide a quick overview of simplifications in scheduling and task execution enabled by Spring 3.0. I will be walking through a basic sample application that you can checkout from the spring-samples Subversion repository. It has been designed to be as simple as possible while showcasing both annotation-driven and XML-based approaches to scheduling tasks in Spring 3.0. Let's begin with the annotation-driven approach. You can run it directly via the main() method in AnnotationDemo. If you take a look, you'll see that it's nothing more than a bootstrap for a Spring ApplicationContext: public static void main(String[] args) {      new ClassPathXmlApplicationContext( "config.xml" , AnnotationDemo. class ); } The reason nothing else is necessary is that the ApplicationContext contains an "active" component, which we will...