`

JSP九大内置对象总结(二)

阅读更多

上一篇文章列举了JSP内置对象的作用范围和当中的五个内置对象,本篇文章将进一步列举request和response内置对象的使用方法。

 

一、request对象

1、作用范围:请求有效。在一起完整的请求响应过程中,即request scope。

2、说明:代表由用户提交请求而出发的request对象。

3、request对象可以获取的信息:

(1)建立HTML表单: <form  action=“action” method=“method” name=“name”>。。。</form>

(2)用REQUEST对象处理:JSP页面将数据存放在request对象里,并将该请求传递到下一个页面,下一个页面访问request对象,处理上一个JSP页面传递过来的数据。

(3)通过超链接来传递:<a   href="aaa.jsp?aa=aa&bb=bb&cc=cc">aaa</a> 

(4)通过jsp动作标签param来进行传递

4、下面对四种可以通过request传递信息的方式进行一一说明。

(1)建立HTML表单: 

// login.jsp
<%@ page language="java" import="java.util.*" pageEncoding=“GB2312"%>
<html>
  <head><title>DengLu</title></head>
  <body>
    <form action="treat.jsp" method="post" >
     用户名<input type="text" name="name" />
     密  码<input type="password" name="password" />
     <input type="submit" value="登陆“ />
    </form>
  </table>
  </body>
</html>
 获取表单提交信息并处理: 
// treat.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<html>
  <head><title>treat.jsp</title></head>
  <body>
   <%
   String name=request.getParameter("name");
   String passw=request.getParameter("password");
   %>
   您好!<%=name%><br />
   您的密码是<%=passw%>
  </body>
</html>
 (2)用REQUEST对象处理: 
// index.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
  <title>请求有效</title>
</head>
<body>
请求有效 - 使用请求request.setAttribute()
<p/>
<%
request.setAttribute("name", "yyw");  
request.setAttribute("password", "123");
%>
<jsp:forward page="requestScopeGet.jsp"/>
</body>
</html>
 由于index.jsp中采用的是forwadr跳转,所以会在同一次请求当中,所以requestScopeGet.jsp中可以获取index.jsp中保存的数据。 
// requestScopeGet.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head><title>请求有效</title></head>
<body>
请求有效 - 使用请求request.getAttribute()
<br />
<%
   String name = (String) request.getAttribute("name");  
   String password = (String) request.getAttribute("password");
   out.println("姓名 = " + name);
   out.println("密码 = " + password);
%>
</body>
</html>
 (3)通过超链接来传递: 
// chaolianjie.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
  <title>请求有效</title>
</head>
<body>
<h1>连接有效 - 使用超链接a</h1>
<a href="requestScopeGet.jsp?name=yyw&password=123">连接</a>
</body>
</html>
 处理提交请求的页面与(2)中的requestScopeGet.jsp一模一样。 

(4)通过jsp动作标签param来进行传递:

仔细观察,可以发现jsp:forward的结束放在了jsp:param的后面。

// jspparam.jsp
<%@ page contentType="text/html;charset=GB2312"%>
<html>
<head>
<title></title>
</head>
<body>
   	<jsp:forward page="header.jsp">
   	<jsp:param name="selected" value="welcome"/>
   	</jsp:forward>
    <h1>北京</h1>
</body>
</html>

 处理页面:

// Header.jsp
<html> …..
<h1><font color="#ff0000">beijin</font></h1>
<%
String name=request.getParameter("selected");
%>
<%=name%>

 (5)如何接受多个属性值的属性

比方说我们在表单中使用到了复选框checkbox,那么我们如何在表单提交页面获取用户选择的多个值呢,我们可以来模拟实现以下。 

// info.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
  <title>请求有效</title>
</head>
<body>
<h1>用户信息</h1>
<form action="requestScopeGet2.jsp" method = "post">
姓 名:<input type="text" name="name" /><br />
密 码:<input type="password" name="password" /><br />
爱 好:<input type="checkbox" name="habit" value="read" checked />看书
	   <input type="checkbox" name="habit" value="sport" />运动
	   <input type="checkbox" name="habit" value="code" />编程
	   <br />
<input type="submit" />
</form>
</body>
</html>

 requestScopeGet2.jsp中的处理内容如下: 

// requestScopeGet2.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
  <title>请求有效</title>
</head>
<body>
请求有效 - 使用请求request.getAttribute()
<br />
<%
   String name = request.getParameter("name");  
   String password = request.getParameter("password");
   String[] userHabits=request.getParameterValues("habit");
   out.println("姓名 = " + name);
   out.println("密码 = " + password);
   for(int i=0;i<userHabits.length;i++){
%>
	第<%=i%>项<%=userHabits[i]%><br>
<%}%>
<br />
</body>
</html>

(6)通过request对象获取服务器和浏览器相关信息

 主要代码:

 <%
//服务器
String localName=request.getLocalName();
String serverName = request.getServerName();
String localAddr=request.getLocalAddr();
int localPort=request.getLocalPort();
int serverPort = request.getServerPort();%>
<p>
<b>服务器</b>:<%= localName %><br/>
<b>服务器端IP</b>:<%= localAddr %><br/>
<b>服务器端口</b>:<%= localPort %><br />
</p>
<%//客户端信息
String remoteHost=request.getRemoteHost();
String remoteAddr=request.getRemoteAddr();
int remotePort=request.getRemotePort();%>
<p>
<b>浏览器端</b>:<%= remoteHost %><br/>
<b>浏览器端IP是</b>:<%= remoteAddr %><br/>
<b>浏览器端口</b>:<%= remotePort %><br/>
</p>
<%//协议相关
String pro=request.getProtocol();
String pro1=request.getScheme();
int len=request.getContentLength();
String type=request.getContentType();
String charEncode=request.getCharacterEncoding();
%>
<b>协议版本</b>:<%= pro %><br/>
<b>协议</b>:<%= pro1 %><br/>
<b>数据内容长度</b>:<%= len %><br/>
<b>数据类型</b>:<%= type %><br/>
<b>字符编码方式</b>:<%= charEncode %><br/>

 效果:


 

二、response对象

1、作用范围:page scope

2、说明:负责将服务器端的数据发送回浏览器的客户端。主要用于向客户端发送数据,如Cookie、HTTP文件头等信息。

3、用法实例:

(1)设置返回给浏览器的页面格式,如下例将页面设置为word类型

 

<%@ page contentType="text/html;charset=GB2312" %>
<html>
	<body>
	<h2>response对象 - setContentType方法</h2>
	将当前页面转换为word文档
	<% response.setContentType("application/msword;charset=GB2312");%> 
	</body>
</html>
 运行效果: 


 

(2)设置header。如下实现了页面自动刷新 

<%@ page contentType="text/html;charset=GBK" %>
<html>
<head>
  <title>response对象 - 处理HTML Header</title>
</head>
<body>
<h2>response - no-cache</h2>
<% 	
	if (request.getProtocol().compareTo("HTTP/1.0") == 0) 		
		 response.setHeader("Pragma", "no-cache"); 	
	else if (request.getProtocol().compareTo("HTTP/1.1") == 0) 		
		response.setHeader("Cache-Control", "no-cache"); 	
	response.setDateHeader("Expires", -1); 
%>
<h2>response - 自动刷新</h2>
当前时间:
<% 
   response.setHeader("Refresh","3");
   out.println(""+new java.util.Date());
 %>
</body>
</html>
 设置setHeader的第一参数为“refresh”的时候,第二个参数可以在指定时间内跳转到其他页面,如下:response.setHeader("Refresh","3");设为 

response.setHeader("Refresh","3;url=http://localhost:8080/try/response1.jsp");

(3)设置重定向页面

<%@page import="java.util.*"%>
<html>
<head><title>response应用</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%
Date nowdate=new Date();
int hour;
hour=nowdate.getHours();
if (hour>=8 & hour<=17) {
       response.sendRedirect(“business.jsp?time=”+hour);//跳转
}else{
       response.sendRedirect(“privacy.jsp?time=”+hour);//跳转
}
%>
</body>
</html>

 business.jsp代码:

// business.jsp
<%@page import="java.util.*"%>
<html>
<head>
<title>response应用</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#FF11FF">
现在是办公时间
<%
    String nowdate = request.getParameter("time");
%>
<%= nowdate%>
</body>
</html>

 private.jsp代码:

// private.jsp
<%@page import="java.util.*"%>
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>response应用</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#FF11FF" text="#000000">
现在是私人时间
<%
   String nowdate = request.getParameter("time");
%>
<%= nowdate%>
</body>
</html>

 (4)添加Cookie

// Addcookies.jsp
<body>
<%
//以获取到的请求参数为值,创建一个Cookie对象
Cookie c = new Cookie("username" , "yyw");
//设置Cookie对象的生存期限,24小时
c.setMaxAge(24 * 3600);
//向客户端增加Cookie对象
response.addCookie(c);
%>
</body>

 readCookies.jsp代码:

// readCookies.jsp
<body>
<%
//获取本站在客户端上保留的所有Cookie
Cookie[] cookies = request.getCookies();
//遍历客户端上的每个Cookie
for (Cookie c : cookies){
	//如果Cookie的名为username,表明该Cookie是我们需要访问的Cookie
	if(c.getName().equals("username")){
		out.println(c.getValue());
		return;
	}
}
out.println("没有这个用户");
%>
</body>

 

关于request和response的用法先介绍到这了,下一篇将会详细介绍session和pageContext对象的使用和探究内置对象为何不用声明便可直接使用。

 

 谢谢您的关注和阅读,文章不当之处还请您不吝赐教~~~微笑微笑微笑

 

  • 大小: 10 KB
  • 大小: 12.2 KB
分享到:
评论

相关推荐

    JSP九大内置对象

    jsp九大内置对象是学习jsp必须了解的内容,所以现在总结一下,以便以后学习中可以使用。

    JSP内置对象实验报告.doc

    JSP内置对象实验报告

    Jsp内置对象session总结

    Jsp内置对象session总结:详细介绍,Session机制,原理,生命周期h和Session的主要方法等。

    JSP内置对象归纳与总结

    JSP内置对象的归纳与总结,内置对象概述 out对象 request对象 response对象 session对象 application对象 pageContext对象 exception对象

    JSP 9大内置对象学习总结

    JSP9大内置对象学习总结 JSP内置9大对象 request常用方法 response session与客户端取得会话 application实现网页计数器

    jsp内置对象 jsp的几个内置的对象图解

    jsp内置对象 有关jsp的一些内置的对象的总结

    jsp内置对象ppt总结

    jsp基础的学习资料,适合初学者,ppt形式,可以很容易学会jsp jsp基础的学习资料,适合初学者

    jsp的9大内置和4大作用域对象精心总结

    jsp的9大内置和4大作用域对象精心总结,如果里面有错误和不足之处希望各位多多指正!!!

    jsp内置对象及常用函数总结

    jsp中9个内置对象的总结,以及每个对象的常用函数的作用和用法。

    实验三 内置对象

    jsp 实验三 内置对象

    JSP 简单总结

    JSP简单总结,内置对象的简单使用,基础概念概括的还算不错

    JSP与Servlet 技术总结

    6. JSP内置对象 5 7. JSP内置对象及其作用 5 8. 获取页面参数 5 9. 重定向(forward与sendRedirect)区别 6 10. JSP和Servlet的区别 7 11. Cookie学习 7 12. Session学习 9 Servlet技术总结 9 1. Servlet生命...

    jsp 9个内置对象

    今天笔试了JSP很多题 回来总结,顺便和大家分享

    jsp内置对象1_详细书介绍

    有关jsp的内之对象的详细介绍和总结,这是我总结的ppt。

    JSP脚本小总结

    JSP知识小总结一.指令元素二.脚本元素三.标准动作元素四.内置对象五.JavaBeans的使用六.JSP中的文件操作 七.JSP运行原理剖析

    JSP_WEB实验报告

    JSP实验环境组建、简单JSP应用、JSP内置对象的应用、Servlet的应用、JSP访问数据库的应用

    JSP基础 课程知识点总结论文

    JSP基础课程知识点总结,多采用表格对比/归纳总结。...包含知识点内容:JSP简介、JSP语法、Tag文件与Tag标记、JSP内置对象、JSP与Java bean、Java Servlet基础、MVC模式、JSP中使用数据库、JSP中的文件操作。

    JSP的四种作用范围总结

    总结Jsp中四种作用范围,应用方法总结

    java面试题自己总结的ssh较多

    3.Servlet中没有内置对象,jsp中的内置对象都是必须通过HttpServletRequest对象。HttpServletretSponse对象以及HttpServlet对象得到。 4.Jsp是servlet的一种简化,使用jsp只需要完成程序员需要输出到客户端的内容,...

    吴天雄--JavaWeb完整笔记.doc

    模块二:使用eclipse和idea快速开发jsp(idea和tomcat的相关配置、eclipse创建web项目、jsp页面元素、jsp九大内置对象、四大作用域对象、解决get/post请求乱码问题、cookie和session详解、请求重定向);模块三:...

Global site tag (gtag.js) - Google Analytics