首页 > 代码库 > JSP详细篇
JSP详细篇
JSP内置对象
JSP使用Java作为脚本语言,所以JSP具有强大的对象处理功能,并可以动态创建web页面内容。但Java语言在使用时,需要先实例化一个对象。而JSP为了简化开发,提供了内置对象,用来实现很多JSP应用。
在JSP中提供9大内置对象。分别为:request、response、session、application、out、pageContext、config、page、exception。
request对象
request对象封装了客户端生成的HTTP请求的所有细节。主要包括:HTTP头信息,系统信息,请求方式,请求参数等。
1.访问请求参数
Request对象处理HTTP请求中的各项参数,在这些参数中,最常见的就是获取访问请求参数。当通过超链接的形式发送请求时,可以为该请求传递参数,可以通过在超链接后面加“?”的形式来实现。这个问号为英文半角。
范例:
新建index.jsp,链接到deal.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>使用request对象请求参数</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>
<a href="./RequestDetail/deal.jsp?id=1&user=">处理页面</a>
</body>
</html>
新建deal.jsp。用request对象的getParameter( )方法获取id、user、pwd参数的值
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
String id = request.getParameter("id");//取得id参数值
String user = request.getParameter("user");//取得user参数值
String pwd = request.getParameter("pwd");//取得pwd参数值
%>
<!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>
id参数值为:<%=id %><br/>
user参数值为:<%=user %><br/>
pwd参数值为:<%=pwd %><br/>
</body>
</html>
输出结果:
id参数值为:1
user参数值为:
pwd参数值为:null
PS:
在使用request的getParameter()方法时,如果指定的参数不存在,则返回null。如果指定了参数名,但是没有指定参数值,则返回空字符串””
2.在作用域中管理属性
在进行请求转发的时候,需要将一些数据传递到转发后的页面进行处理。此时就需要request对象的setAttribute()方法将数据保存到request范围内的变量中。
request对象的setAttribute( )方法的语法格式如下:
request.setAttribute(String name,Object object);
参数说明:
A.name:表示变量名,为String类型。在转发后的页面取数据的时候,就是通过这个变量名来获取数据的
B.object:表示传递的变量值。为Object类型
将数据保存到request对象后,可以通过request对象的getAttribute()方法获取该变量的值。
request()对象的getAttribute()方法的语法格式如下:
request().getAttribute(String name);
参数说明:
name:表示变量名。该变量名在request范围内有效
范例:
创建index.jsp页面,将页面转发到deal.jsp页面
Index.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>使用request对象管理作用中的属性</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 x = 100;
int y = 0;
request.setAttribute("result", x/y);//保存执行结果
}
catch(Exception e)
{
request.setAttribute("result","页面产生错误");
}
%>
<jsp:forward page = "deal.jsp"/> //页面跳转
</body>
</html>
Deal页面如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
String id = request.getParameter("id");//取得id参数值
String user = request.getParameter("user");//取得user参数值
String pwd = request.getParameter("pwd");//取得pwd参数值
%>
<!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>
<%String message = request.getAttribute("result").toString();%>
<%out.print(message);%>
</body>
</html>
3.获取cookie
request对象的getCookies()方法可以获取cookie中的内容
request对象的getCookis( )方法的语法格式如下:
request.getCookie();
范例:
通过cookie保存并读取用户信息
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.net.URLDecoder" %>
<%
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>使用request对象管理作用中的属性</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>
<%
Cookie[] cookies = request.getCookies();
String user = "";
String date = null;
if(cookies != null)
{
for(int i = 0 ; i < cookies.length ; i++)
{
if(cookies[i].getName().equals("mrCookie"));
user = URLDecoder.decode(cookies[i].getValue().split("#")[0]);//获取用户名
date = cookies[i].getValue().split("#")[1];
}
}
if("".equals(user)&&"".equals(date))
{
%>
游客您好,欢迎初次光临
<form action = "deal.jsp" method = "post">
<center>
请输入姓名:<input name="user" type="text" value ="">
<input type="submit" value="确定">
</center>
</form>
<%
}else{
%>
欢迎[<b><%=user%></b>]再次光临<br/>
您注册的时间是:<%=date %>
<%
}
%>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.net.URLEncoder"" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
String id = request.getParameter("id");//取得id参数值
String user = request.getParameter("user");//取得user参数值
String pwd = request.getParameter("pwd");//取得pwd参数值
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>写入cookie</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>
<%
request.setCharacterEncoding("GB18030");
String use = URLEncoder.encode(request.getParameter("user"),"UTF-8");
Cookie cookie = new Cookie("mrCookie",use+"#"+new java.util.Date().toLocaleString());
cookie.setMaxAge(60*60*24*30);
response.addCookie(cookie);
%>
<script type="text/javascript">
window.location.href=http://www.mamicode.com/"index.jsp";
</script>
</body>
</html>
JSP详细篇