JSP之request对象
2016-02-29 15:13
288 查看
在请求转发时,我们需要把一些数据传递到转发后的页面进行处理。这时就需要使用request对象的setAttribute()方法将数据保存到request范围内的变量中。
示例:创建index.jsp文件,使用Java中的try……catch语句捕获异常,将数据保存到request范围内的变量中:
然后用 <jsp:forward>动作将指令转发到MyJsp.jsp页面:
run:
示例:创建index.jsp文件,使用Java中的try……catch语句捕获异常,将数据保存到request范围内的变量中:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% 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 'index.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> <% try{ int a=1; int b=0; request.setAttribute("result", a/b); }catch(Exception e){ request.setAttribute("result", "很抱歉,页面发生错误!"); } %> <jsp:forward page="MyJsp.jsp"></jsp:forward> </body> </html>
然后用 <jsp:forward>动作将指令转发到MyJsp.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% 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 'MyJsp.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 message=request.getAttribute("result").toString(); %> <%=message %> </body> </html>
run:
很抱歉,页面发生错误!
相关文章推荐
- H5单页面架构:requirejs + angular + angular-route
- UI XML 配置属性
- UIKit框架(11)导航控制器UINavigationController
- iOS开发多线程--(NSOperation/Queue)
- requestLayout, invalidate和postInvalidate的异同
- EasyUi之Tree1
- iOS学习之-----UIBezierPath类
- 使用Hibernate SQLQuery执行原生SQL
- iOS 对UIButton的imageView和titleLabel进行重新布局
- Andorid-XML和JAVA代码混合控制UI
- sql 对应 sequelize
- UIKit框架(10)自定义modal过渡效果
- 如何为UICollectionView添加整体的header view
- 事务Transaction()详解(以及ContentValue的注意点)
- iOS7——UIControlEventTouchDown延迟响应问题
- UIKit框架(9)控制器的modal切换方式(二)
- 简单的计时器动画实现-ValueAnimator
- UINavigationController之NavigationBar
- QT QPixmap:It is not safe to use Pixmaps outside the GUI thread
- UIKit框架(9)控制器的modal切换方式(一)