您的位置:首页 > 编程语言 > Java开发

JSP(Java Server Pages)内置对象

2016-10-23 22:11 585 查看
request对象

(1)访问请求参数

处理HTTP请求中的各项参数。在这些参数中,最常用的就是获取访问请求参数。当通过超链接的形式发送请求时,可以为该请求传递参数,这可以通过在超链接的后面加上问好“?”来实现,例如如下的例子,发送一个请求到delete.jsp页面,并传递一个名称为id的参数:

<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>使用request对象获取请求参数值</title>
</head>
<body>
<a href="deal.jsp?id=1&user=">处理页</a>
</body>
</html>


改页面的作用是在文件中添加一个用于链接到deal.jsp页面的超链接,并传递两个参数。

<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="utf-8"%>
<%
String id=request.getParameter("id");   //获取id参数的值
String user=request.getParameter("user");//获取user参数的值
String pwd=request.getParameter("pwd");//获取pwd参数值
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>处理页</title>
</head>
<body>
id参数的值为:<%=id %><br>
user参数的值为:<%=user %><br>
pwd参数的值为:<%=pwd %>
</body>
</html>


通过request对象的getParameter()方法获取请求参数id,user,pwd并输出,对于不存在的参数返回null。

(2)在作用域中管理属性

在进行请求时,需要把一些数据传递到转发后的页面进行处理。这时就需要使用request对象的setAttribute()方法将数据保存到request范围内的变量中。

request.setAttribute(String name,Object object);


name:变量名,String类型,在转发后的页面取数据时,就是根据这个变量名来获取数据。

object:用于指定需要在request范围内传递的数据,为Object类型

在数据保存到request范围内的变量中后,可以通过request对象的getAttribute()方法获取变量的值:

request.getAttribute(String name);


例如如下例子

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
try{//捕获异常信息
int money=100;
int number=0;
request.setAttribute("result",money/number);//保存执行结果
}catch(Exception e){
request.setAttribute("result","很抱歉,页面产生错误!");//保存错误提示信息
}
%>
<jsp:forward page="deal.jsp"/>
</body>
</html>


<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>结果页</title>
</head>
<body>
<%String message=request.getAttribute("result").toString(); %>
<%=message %>
</body>
</html>




(3)获取cookie

cookie是小段的文本信息,在网络服务器上生成并发送给浏览器。通过使用cookie可以标识用户身份,记录用户名和密码,跟踪重复用户。浏览器将cookie以key/value的形式保存到客户机的指定目录中。

例如,通过cookie保存并读取用户登录信息:

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.net.URLDecoder" %>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>通过cookie保存并读取用户登录信息</title>
</head>
<body>
<%
Cookie[ ]   cookies = request.getCookies();//从request中获得Cookie对象的集合
String user = "";   //登录用户
String date = "";   //注册的时间
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {  //遍历cookie对象的集合
if (cookies[i].getName().equals("mrCookie")) {//如果cookie对象的名称为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">
请输入姓名:<input name="user" type="text" value="">
<input type="submit" value="确定">
</form>
<%
} else {//已经注册
%>
欢迎[<b><%=user %></b>]再次光临<br>
您注册的时间是:<%=date %>
<%
}
%>
</body>
</html>


<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.net.URLEncoder" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>写入cookie</title>
<script type="text/javascript">window.location.href="index.jsp"</script>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");

String user=URLEncoder.encode(request.getParameter("user"),"utf-8");    //获取用户名
Cookie cookie = new Cookie("mrCookie", user+"#"+new java.util.Date().toLocaleString());
cookie.setMaxAge(60*60*24*30);      //设置cookie有效期30天
response.addCookie(cookie); //保存cookie
%>

</body>
</html>


(4)解决中文乱码问题

有时请求参数的文字编码方式与页面中的不一致会导致乱码问题,因为所有的request请求都是ISO-8859-1的,而有时页面采用的是UTF-8的编码方式。要解决此问题,只要将获取到的数据通过String的构造方法使用指定的编码类型重新构造一个String对象,即可正确的显示出中文信息。例如:

<body>
<a href="show.jsp?name=张三&sex=男">解决中文乱码</a>
</body>


<body>
name参数的值为:<%=new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8")%>
sex参数的值为:<%=request.getParameter("sex")%>
</body>


response对象

response对象用于响应客户的请求,向客户端输出信息。它封装了JSP产生的响应,并发送到客户端以响应客户端的请求。请求的数据可以是各种数据类型,甚至是文件。response对象在JSP页面内有效。

(1)重定向网页

使用response对象提供的sendRedirect()方法可以将网页重定向到另一个页面。重定向操作支持将地址重定向到不同的主机上,这点不同于转发(< jsp:forward>)。在客户端上将会得到跳转的地址,并重新发送请求连接。进行重定向操作后,request中的属性全部失效,并开始一个新的request对象。

<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>首页</title>
</head>
<body>
<%response.sendRedirect("login.jsp"); %>
</body>
</html>


<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>用户登录页面</title>
</head>
<body>
<form name="form1" method="post" action="">
用户名: <input name="name" type="text" id="name" style="width: 120px"><br>
密  码: <input name="pwd" type="password" id="pwd" style="width: 120px"> <br>
<br>
<input type="submit" name="Submit" value="提交">
</form>
</body>
</html>


默认执行的是index.jsp界面,在index.jsp界面中调用response对象的setRedirect()方法重定向页面到用户登录界面login.jsp。

(2)处理HTTP文件头

禁用缓存

<%
response.setHeader("Cache-Control","no-store");
response.setHeader("Expires",0);
%>


设置页面自动刷新

<%
response.setHeader("refresh","10");
%>


使网页每隔10秒刷新一次

定时网页跳转

<%
response.setHeader("refresh","5","URL=login.jsp");
%>


session对象

session在网络中称为会话。由于HTTP请求是一种无状态请求,当一个客户向服务器发出请求,服务器接收请求,并返回响应,该链接就结束了,而服务器并不保存相关信息。为了弥补这一缺点,HTTP协议提供了session。通过session可以在应用程序的Web页面间进行跳转,保存用户的状态,使整个用户会话一直存在下去,知道关闭浏览器。但是,在一个会话中,客户端穿长时间不向服务器发出请求,session对象就会自动消失。Request 是在一次请求范围,Session 是在一次会话中,也就是说,你从一开始进入页面到你离开页面时,都属于一次会话当中,也就是说它的作用范围比Request要广,你在任何页面都可以用session.getAttribute(“values”);得到values的值

通过session对象可以存储或读取客户相关的信息。可以通过setAttribute()方法和getAttribute()方法实现。例如:

在index.jsp界面提供输入用户名文本框

<%@ 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>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="css/style.css">

</head>

<body>
<form id="form1" name="form1" method="post" action="session.jsp">
<div align="center">
<table width="23%" border="0">
<tr>
<td width="36%"><div align="center">您的名字是:</div></td>
<td width="64%">
<label>
<div align="center">
<input type="text" name="name" />
</div>
</label>
</td>
</tr>
<tr>
<td colspan="2">
<label>
<div align="center">
<input type="submit" name="Submit" value="提交" />
</div>
</label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>


在session.jsp界面中,将用户在index.jsp页面中输入的用户名保存在session对象中,并为用户提供用于添加最想去的地址的文本框

<%@ 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>My JSP 'session.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="css/style.css">

</head>

<body>
<%
String name = request.getParameter("name");     //获取用户填写的用户名

session.setAttribute("name",name);              //将用户名保存在session对象中
%>
<div align="center">
<form id="form1" name="form1" method="post" action="result.jsp">
<table width="28%" border="0">
<tr>
<td>您的名字是:</td>
<td><%=name%></td>
</tr>
<tr>
<td>您最喜欢去的地方是:</td>
<td><label>
<input type="text" name="address" />
</label></td>
</tr>
<tr>
<td colspan="2"><label>
<div align="center">
<input type="submit" name="Submit" value="提交" />
</div>
</label></td>
</tr>
</table>
</form>
<p> </p>
</div>
</body>
</html>


在result.jsp页面中,实现显示用户输入的用户名与最喜欢去的地方

<%@ 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>My JSP 'result.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="css/style.css">

</head>

<body>
<div align="center">
<%

String name = (String)session.getAttribute("name");     //获取保存在session范围内的对象

String solution = request.getParameter("address");      //获取用户输入的最想去的地方
%>
<form id="form1" name="form1" method="post" action="">
<table width="28%" border="0">
<tr>
<td colspan="2"><div align="center"><strong>显示答案</strong></div>          </td>
</tr>
<tr>
<td width="49%"><div align="left">您的名字是:</div></td>
<td width="51%"><label>
<div align="left"><%=name%></div>       <!-- 将用户输入的用户名在页面中显示 -->
</label></td>
</tr>
<tr>
<td><label>
<div align="left">您最喜欢去的地方是:</div>
</label></td>
<td><div align="left"><%=solution%></div></td> <!-- 将用户输入的最想去的地方在页面中显示 -->
</tr>
</table>
</form>
<p> </p>
</div>
</body>
</html>


application对象

application对象用于保存所用应用程序中的公有数据。它在服务器启动时自动创建,在服务器停止时销毁。当application对象没有被销毁时,所有的用户都可以共享application对象。与session对象相比,application对象的生命周期更长,类似于系统的“全局变量”。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jsp