========================================================
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>request3.jsp</title> </head> <body> 添加请求属性 request.setAttribute() 语法:<br> request.setAttribute(String name , Object obj) ;<br> 参数说明:<br> name : 表示变量名,String类型,在转发后的页面获取数据时,就是通过 这个变量名来获取数据的<br> obj :用于指定需要在request范围内传递的数据为Object内容<br> <% try //捕获异常 { int a = 100 ; int b = 0 ; request.setAttribute("result", a/b) ; //保存执行结果 } catch(Exception e) { request.setAttribute("result", "抱歉页面发生错误") ; //保存错误提示信息 } %> <% request.setAttribute("name", "张三") ; //传递汉字 %> <jsp:forward page="request4.jsp" /> </body> </html> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>request4.jsp</title> </head> <body> <% //获取传递值,getAttribute() 返回Object类型,要转型, String result = request.getAttribute("result").toString() ; String name = request.getAttribute("name").toString() ; %> <%=result %> <br> <%=name %> </body> </html>
转载于:https://www.cnblogs.com/20gg-com/p/6012562.html
