JSP详细篇
来源:互联网 发布:电动车仿真软件 编辑:程序博客网 时间:2024/06/03 13:42
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="index.jsp";
</script>
</body>
</html>
4.获取客户端信息
request对象可以获取客户端的相关信息。如:HTTP报文头信息、客户端提交方式、客户端主机IP地址、端口号等。
request获取客户端信息的常用方法:
getHeader(String name):获取HTTP协议定义的文件头信息
getHeaders(String name):返回指定名称的request Header的所有值,返回枚举类型实例
getHeaderNames():返回所有request Header名字,返回枚举类型实例
getMethod():获取客户端服务器传送数据的方法,如geet,post,header,trace等
getPrototol():获取客户端传递参数所语句的协议
getRequestURI():获取发出请求字符串的客户端地址,不包括参数
getRequestURL():获取发出请求字符串的客户端地址
getRealPath():返回当前请求文件的绝对路径
getRemoteAddress():获取客户端的IP地址
getRemoteHost():获取客户端的主机名
getServerName():获取服务器的名字
getServerName():获取服务器的名字
getServerPath():获取客户端所请求的脚本文件的文件路径
getServerPort():获取服务器的端口号
范例:
<%@ 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>
<b>客户端提交信息的方式:<%=request.getMethod() %></b><br/>
<b>使用的协议:<%=request.getProtocol() %></b><br/>
<b>获取发出请求字符串的客户端地址:<%=request.getRequestURI() %></b><br/>
<b>获取打出请求支付穿的客户端地址:<%=request.getRequestURL() %></b><br/>
<b>获取提交数据的客户端的IP地址:<%=request.getRemoteAddr() %></b><br/>
<b>获取服务器的端口号:<%=request.getServerPort() %></b><br/>
<b>获取服务器的名称:<%=request.getServerName() %></b><br/>
<b>获取客户端的主机名:<%=request.getRemoteHost() %></b><br/>
<b>获取客户端所请求的脚本文件的文件路径:<%=request.getServletPath() %></b><br/>
<b>获取HTTP协议定义的文件头信息的Host的值:<%=request.getHeader("host") %></b><br/>
<b>获取HTTP协议定义的文件头信息User-Agent的值:<%=request.getHeader("user-agent") %></b><br/>
<b>获取HTTP协议定义的文件头信息accept-language值:<%=request.getHeader("accept-language") %></b><br/>
<b>获取请求文件的绝对路径:<%=request.getRealPath("index.jsp") %></b><br/>
</body>
</html>
- JSP详细篇
- JSP详细篇——其他一些JSP内置对象
- JSP详细篇——response对象
- JSP详细篇——session
- JSP详细篇——application
- JSP详细篇——out
- JSP详细篇——JavaBean技术
- JSP详细篇——Servlet(一)
- JSP详细篇——Servlet(二)
- JSP详细篇——Servlet监听器
- JSP详细篇——异步处理
- JSP详细篇——JSTL
- JSP详细篇——Ajax
- JSP详细篇——EL表达式
- jsp include详细解释
- JSP详细知识点
- JSP入门详细讲解
- 详细介绍JSP表达式
- Android实现文件上传下载【xUtils工具实现】
- 《Java编程思想》学习笔记9——集合容器高级
- 国产操作系统小论
- AngularJS简介
- Girl_iOS100天学iOS的第七天(Swift)
- JSP详细篇
- JAVA线程池(ThreadPoolExecutor)源码分析
- U3D同时播放2个声音
- 中序表达式->逆波兰式->计算结果
- hdu 3635 Dragon Balls 并查集(带权)
- 最小化类和成员的访问能力
- Splay POJ3468(老题新做)
- java集合框架类源代码阅读体会
- 验证用户名、密码和日期