`
liuxi1024
  • 浏览: 384616 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

基础巩固-jsp内置对象

阅读更多

1、request对象

request对象封装了客户端的请求信息,服务端通过request对象可以了解到客户端的需求,然后做出响应。request对象是 HttpServletRequest(接口)的实例。HttpServletRequest接口继承自ServletRequest接口,只是增加了一 些HTTP相关的方法。所谓的request(在JSP中使用的)其实只是规范中的一个名称而已,它是一个对象,但并不是由SUN提供,而是由各个不同的 Servlet提供商编写的,SUN只是规定这个类要实现HttpServletRequest接口,并且规定了各个方法的用途,但具体的实现则由各个提 供商自己决定。

(1)setAttribute(String name, Object value) 和 getAttribute(String name)

(2)getParameter(String)

获得客户端传送给服务端的参数值,该参数由name指定。

(3)void setCharacterEncoding(String charEncoding)和String getCharacterEncoding()

前者用来设置请求的字符编码格式,后者返回请求的当前字符编码格式。

(4)getContextPath()和getRealPath()

前者获取相对路径,后者回去绝对路径。

(5)以下方法用来获取客户端请求中的相关信息:

int getContentLength() 返回请求体的长度(以字节数)。

String getContentType() 得到请求体的MIME类型。

String getProtocol() 返回请求用的协议类型及版本号。

String getScheme() 返回请求用的协议名,如:http.https及ftp等。

String getServerName() 返回接受请求的服务器主机名

int getServerPort() 返回服务器接受此请求所用的端口号

String getRemoteAddr() 返回发送此请求的客户端IP地址

String getRemoteHost() 返回发送此请求的客户端主机名

String getRealPath(String path) 返回一虚拟路径的真实路径

 

例子

<%@ page language="java" contentType="text/html; charset=GBK"
	pageEncoding="GBK"%>
<%@ page import="java.util.Enumeration" %>
<!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=GBK">
<title>Insert title here</title>
</head>
<body>
<Font size=5> <BR>
客户使用的协议是: 
<%  String protocol=request.getProtocol();  
	out.println(protocol); 
%> <BR>
获取接受客户提交信息的页面:
<%  String path=request.getServletPath(); 
	out.println(path);
%> <BR>
接受客户提交信息的长度:
<%  int length=request.getContentLength(); 
	out.println(length); 
%> <BR>
客户提交信息的方式:
<%  String method=request.getMethod(); 
	out.println(method);
%> <BR>
获取HTTP头文件中User-Agent的值:
<%  String header1=request.getHeader("User-Agent"); 
	out.println(header1);
%> <BR>
获取HTTP头文件中accept的值:
<%  String header2=request.getHeader("accept"); 
	out.println(header2); 
%> <BR>
获取HTTP头文件中Host的值:
<%  String header3=request.getHeader("Host"); 
	out.println(header3); 
%> <BR>
获取HTTP头文件中accept-encoding的值:
<%  String header4=request.getHeader("accept-encoding"); 
	out.println(header4);
%> <BR>
获取客户的IP地址:
<%  String IP=request.getRemoteAddr(); 
	out.println(IP); 
%> <BR>
获取客户机的名称:
<%  String clientName=request.getRemoteHost(); 
	out.println(clientName); 
%> <BR>
获取服务器的名称:
<%  String serverName=request.getServerName(); 
	out.println(serverName); 
%> <BR>
获取服务器的端口号:
<%  int serverPort=request.getServerPort(); 
	out.println(serverPort); 
%> <BR>
获取客户端提交的所有参数的名字:
<% Enumeration enume=request.getParameterNames(); 
while(enume.hasMoreElements())
{String s=(String)enume.nextElement();
out.println(s);
}
%> <BR>
获取头名字的一个枚举: 
<% Enumeration enum_headed=request.getHeaderNames();
while(enum_headed.hasMoreElements())
{String s=(String)enum_headed.nextElement();
out.println(s);
}
%> <BR>
获取头文件中指定头名字的全部值的一个枚举:
<% Enumeration enum_headedValues=request.getHeaders("cookie");
while(enum_headedValues.hasMoreElements())
{String s=(String)enum_headedValues.nextElement();
out.println(s);
}
%> <BR>

</Font>
</body>
</html>
 

HttpServletRequest的API文档:
http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServletRequest.html

 

2、response对象

response对象包含了响应客户端请求的有关信息,它是HttpServletResponse的实例。

(1)String getCharacterEncoding()
返回响应用的是何种字符编码,该输出同page编译指令的pageEncoding是相关的。

(2)ServletOutputStream getOutputStream()
返回响应的一个二进制输出流

(3)PrintWriter getWriter()
返回可以向客户端输出字符的一个对象

(4)void setContentLength(int len)
设置响应头长度

(5)void setContentType(String type)
设置响应的MIME类型

(6) sendRedirect(java.lang.String location)
重新定向客户端的请求
 

3、out对象

out对象是JspWriter类的实例,是向客户端输出内容常用的对象.

 

<%
out.println("<html>");
out.println("<head>");
out.println("<title>");
out.println("<test");
out.println("</title>");
out.println("</head>");
out.println("<body>");
out.println("<hl>");
out.println("out对象示例");
out.println("</hl>");
out.println("</body>");
out.println("</html>");
%>

 4、application/session对象

application对象实现了用户间数据的共享,可存放全局变量。它开始于服务器的启动,直到服务器的关闭,在此期间,此对象将一直存在;这样在用户 的前后连接或不同用户之间的连接中,可以对此对象的同一属性进行操作;在任何地方对此对象属性的操作,都将影响到其他用户对此的访问。服务器的启动和关闭 决定了application对象的生命。它是ServletContext类的实例。

session对象指的是客户端与服务器的一次会话,从客户连到服务器的一个WebApplication开始,直到客户端与服务器断开连接为止。它是HttpSession类的实例。

 

您是第
<%
String count=(String)application.getAttribute("count");
if(count==null){
    count="1";
}else{
    count=Integer.parseInt(count)+1+"";
}
application.setAttribute("count",count);

%>

<% out.println(count); %>
访问者

 

session.isNew()方法------判断一个会话是否是新的会话!

 

5、page对象

page对象就是指向当前JSP 页面本身,有点象类中的this指针,它是java.lang.Object类的实例

 

6、exception对象

exception对象是一个例外对象,当一个页面在运行过程中发生了例外,就产生这个对象。如果一个JSP 页面要应用此对象,就必须把isErrorPage设为true,否则无法编译。他实际上是java.lang.Throwable的对象

 

page1.jsp

<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK" errorPage="error.jsp" %>
<!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=GBK">
<title>Insert title here</title>
</head>
<body>
<%
 int i= 10/0;
%>
</body>
</html>

 error.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
	pageEncoding="GBK" isErrorPage="true" %>
<!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=GBK">
<title>Insert title here</title>
</head>
<body>
Error Message:getMessage() Method
<br>
<%
out.println(exception.getMessage());
%>
<br>
Error String:toString() Method
<br>
<%
out.println(exception.toString());
%>
</body>
</html>

 

7、pageContext对象

pageContext对象提供了对JSP 页面内所有的对象及名字空间的访问,也就是说他可以访问到本页所在的SESSION,也可以取本页面所在的application的某一属性值,他相当于页面中所有功能的集成者,它的本类名也叫pageContext。

<%
         JspWriter myout=pageContext.getOut();
         myout.println("myout write: hello world"+"<br>");
         out.println("out write: hello world"+"<br>");

         String username1="george";
         String password1="george";
         HttpSession mySession= pageContext. getSession();
         mySession.putValue("username",username1);
		 mySession.putValue("password", password1);

         out.println("Session bind username:"+session.getValue("username")+"<br>");
		 out.println("Session bind password:"+session.getValue("password")+"<br><br>");
           
		 Object mypage=pageContext.getPage();
		 out.println("Compile Class Came:"+mypage.toString()+"<br><br>");

         ServletResponse myresponse=pageContext.getResponse();
         out.println("Character Encoding:"+ myresponse.getCharacterEncoding()+"<br><br>");

         ServletRequest myrequest=pageContext.getRequest();
		 out.println(myrequest.getRemoteHost());

          String username2="robin";
          String password2="robin";
          myrequest.setAttribute("username",username2);
          myrequest.setAttribute("password", password2);
          out.println("Request bind username:"+request.getAttribute("username")+"<br>");
          out.println("Request bind password:"+request.getAttribute("password") +"<br><br>");
        
          ServletContext myapplication=pageContext.getServletContext();
          out.println(myapplication.getServerInfo());
          String username3="java";
          String password3="java";
          myapplication.setAttribute("username",username3);
          myapplication.setAttribute("password", password3);
          out.println("Application bind username:"+application.getAttribute("username")+"<br>");
          out.println("Application bind password:" +application.getAttribute("password")+"<br><br>");
       %>
 

8、config对象

config对象是在一个Servlet初始化时,JSP 引擎向它传递信息用的,此信息包括Servlet初始化时所要用到的参数(通过属性名和属性值构成)以及服务器的有关信息(通过传递一个ServletContext对象)。

Server Info:
<br>
<%
out.println(config.getServletContext().getServerInfo());
%>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics