详解JSP页面传值

开发 后端
本文详细介绍JSP页面传值,以及建立两个JSP页面,tes1.jsp和test2.jsp,包括其JSP页面代码。

这周在调支付宝的接口。期间需要把我方程序处理后的参数(交易金额)按照规定的格式传递给支付宝的接口。因为中途要设计到我方程序对一些数据的处理,所以并不方便直接传值过去。思来想去,决定先把我方的数据提交给webwork的Action进行处理,也就是对数据库进行操作;然后把交易金额以及支付宝接口需要的其他参数一并传递给一个JSP页面,并让这个JSP页面在把action直接指向支付宝的网关接口,注意:中间过程中这个JSP页面时不显示出来的。为此,做了如下测试:建立两个JSP页面传值,tes1.jsp和test2.jsp。代码如下:

  1. <%@pagelanguage="java"import="java.util.*"pageEncoding="GBK"%> 
  2. <%  
  3. Stringpath=request.getContextPath();  
  4. StringbasePath=request.getScheme()+"://"+request.getServerName()
    +":"+request.getServerPort()+path+"/";  
  5. %> 
  6. > 
  7. <html> 
  8. <head> 
  9. <basehrefbasehref="<%=basePath%>"> 
  10.  
  11. <title>MyJSP'test1.jsp'startingpagetitle> 
  12. <metahttp-equivmetahttp-equiv="pragma"content="no-cache"> 
  13. <metahttp-equivmetahttp-equiv="cache-control"content="no-cache"> 
  14. <metahttp-equivmetahttp-equiv="expires"content="0"> 
  15. <metahttp-equivmetahttp-equiv="keywords"content="keyword1,keyword2,keyword3"> 
  16. <metahttp-equivmetahttp-equiv="description"content="Thisismypage"> 
  17. <metahttp_equivmetahttp_equiv="refresh"content="5"> 
  18.  
  19. <scriptlanguagescriptlanguage="javascript"type="text/javascript"> 
  20. functionexecute(){  
  21. varobj=document.getElementById("name");  
  22. document.form1.action="alipay/test2.jsp?param="+obj.value;  
  23. document.form1.submit();  
  24. }  
  25. script> 
  26. head> 
  27. <bodyonloadbodyonload="execute();"> 
  28. <formnameformname="form1"method="post"> 
  29. <table> 
  30. <tr> 
  31. <td> 
  32. 测试JSP页面传值<inputtypeinputtype="text"id="username"value="luodada"> 
  33. td> 
  34. tr> 
  35. table> 
  36. form> 
  37. body> 
  38. html> 

tset2.jsp的代码如下:

  1. <%@pagelanguage="java"import="java.util.*"pageEncoding="GBK"%> 
  2. <%  
  3. Stringpath=request.getContextPath();  
  4. StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %> 
  6. > 
  7. <html> 
  8. <head> 
  9. <basehrefbasehref="<%=basePath%>"> 
  10. <title>MyJSP'test2.jsp'startingpagetitle> 
  11. <metahttp-equivmetahttp-equiv="pragma"content="no-cache"> 
  12. <metahttp-equivmetahttp-equiv="cache-control"content="no-cache"> 
  13. <metahttp-equivmetahttp-equiv="expires"content="0"> 
  14. <metahttp-equivmetahttp-equiv="keywords"content="keyword1,keyword2,keyword3"> 
  15. <metahttp-equivmetahttp-equiv="description"content="Thisismypage"> 
  16.  
  17. head> 
  18. <body> 
  19. <%  
  20. Stringvalue=request.getParameter("param");  
  21. out.print("从test1.jsp传递过来的值是"+value);  
  22. %> 
  23. body> 
  24. html> 

具体思路如下:

在JSP页面传值test1.jsp中,通过JavaScript把文本框中的值获取出来,,使test1.jsp在加载进来的时候马上执行页面跳转;

在JSP页面传值test2.jsp中通过request.getParameter("参数名称");来获取test1.jsp传递过来的值即可。

【编辑推荐】

  1. 详解JSP中调用JavaBean
  2. JSP开发环境的搭建
  3. 解决JSP开发Web程序中文显示三种方法
  4. 开发JSP HTTP服务器
  5. JSP、ASP和PHP安全编程
责任编辑:佚名 来源: JavaEye
相关推荐

2012-06-29 13:31:56

ServletJSPJava

2009-07-03 13:24:56

JSP表单

2009-02-17 10:40:26

页面跳转JSP教程

2012-05-14 21:14:07

Android页面传值

2009-07-24 14:36:10

ASP.NET跨页面传

2013-07-05 15:05:42

Windows PhoWP页面间传值方法

2009-09-07 03:44:50

C#窗体间传值

2009-07-03 18:12:49

JSP页面

2009-07-06 09:34:19

JSP页面

2009-07-01 18:50:29

Dreamweaver

2013-06-03 10:22:41

iOS开发移动开发页面间传值

2009-03-16 15:07:20

JSP分页window.openJSP表单

2009-07-06 17:08:19

测试JSP容器

2009-09-10 09:48:15

JSP使用FCKedi

2010-05-11 16:55:12

Windows Pho

2009-07-06 18:23:56

Struts和JSPJSP页面

2009-07-03 17:48:34

JSP页面翻译

2009-07-02 10:13:47

ASP和JSPJSP页面

2009-02-25 10:55:29

FCKeditor控件JSP

2009-07-07 13:42:57

Servlet JSP
点赞
收藏

51CTO技术栈公众号