jsp的request与forward实例
下面是一个request内置对象与forward指令的实例,只要是为了说明request在请求中获取请求参数,操作request的范围属性,以及当使用forward转发用户请求的时候,客户端的请求参数和请求属性不会丢失。。
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <% 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> <base href="<%=basePath%>"> <title>取钱的表单页</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> This is my JSP page. <br> <!-- 取钱的表单 --> <form method= "post" action="first.jsp"> 取钱: <input type="text" name="balance"> <input type="submit" value="提交"> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <%@ page import="java.util.*" %> <% 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> <base href="<%=basePath%>"> <title>My JSP ‘first.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> // <% String str = request.getParameter("balance"); double qian = Double.parseDouble(str); if(qian<500) { out.println("账户余额减少:"+qian); }else { List<String> info = new ArrayList<String>(); info.add("AAAAAAAAA"); info.add("BBBBBBBBB"); info.add("CCCCCCCCC"); request.setAttribute("info", info); %> <!-- 实现转发 --> <jsp:forward page="second.jsp"></jsp:forward> <% } %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <%@ page import="java.util.*" %> <% 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> <base href="<%=basePath%>"> <title>My JSP ‘second.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% String str = request.getParameter("balance"); double qian = Double.parseDouble(str); List<String> info =(List<String>)request.getAttribute("info"); for(String trm:info) { out.println(trm+"<br>"); } out.println("取钱"+qian+"快"); %> </body> </html>
表明,使用forward用户请求时,请求参数与request范围的属性都不会丢失,即forward的动作还是原来的请求,并未向服务器再次发请求。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。