JSP内置对象------request

<%@ 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>request1.jsp</title>
</head>
<body>
在该页面下创建一个链接request2.jsp,并传递两个参数<br>

当使用 href="request2.jsp?id=2&user="  这种链接时,后面的地址及参数之间不可以随便加
空格<br>
<a href="request2.jsp?id=2&user=">打开链接</a>
</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>request2.jsp</title>
</head>
<body>
获取传递的参数id、user <br>
<%
String id = request.getParameter("id") ;      //获取id参数的值

String user = request.getParameter("user") ;      //获取user参数的值

String password = request.getParameter("password") ; //获取password参数的值
%>
在使用request的getParameter()方法获取传递参数值时,要注意:<br>
1.如果指定的参数不存在,将返回null<br>

2.如果指定了参数名,但未指定参数值,将返回空字符串 ""      <br>

id参数的值:<%=id %><br>    

user参数的值<%=user %><br>

password参数的值<%=password %><br>

</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>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>

 

posted @ 2016-10-30 09:58  丶疏影横斜  阅读(335)  评论(0编辑  收藏  举报