客户端的请求信息被封装在Request对象中,经过它才能了解到客户的需求,而后作出响应。它是HttpServletRequest类的实例。html
<%@ page contentType="text/html;charset=gb2312"%> <%request.setCharacterEncoding("gb2312");%> <html> <head> <title>request对象_例1</title> </head> <body bgcolor="#FFFFF0"> <form action="" method="post"> <input type="text" name="qwe"> <input type="submit" value="提交"> </form> 请求方式:<%=request.getMethod()%><br> 请求的资源:<%=request.getRequestURI()%><br> 请求用的协议:<%=request.getProtocol()%><br> 请求的文件名:<%=request.getServletPath()%><br> 请求的服务器的IP:<%=request.getServerName()%><br> 请求服务器的端口:<%=request.getServerPort()%><br> 客户端IP地址:<%=request.getRemoteAddr()%><br> 客户端主机名:<%=request.getRemoteHost()%><br> 表单提交来的值:<%=request.getParameter("qwe")%><br> </body> </html>
<%@ page contentType="text/html;charset=gb2312"%> <%request.setCharacterEncoding("gb2312");%> <%@ page import="java.util.Enumeration"%> <html> <head> <title>request对象_例2</title> </head> <body bgcolor="#FFFFF0"> <form action="" method="post"> 用户名:<input type="text" name="username"> 密 码:<input type="text" name="userpass"> <input type="submit" value="进入" > </form> <% String str=""; if(request.getParameter("username")!=null && request.getParameter("userpass")!=null){ Enumeration enumt = request.getParameterNames(); while(enumt.hasMoreElements()){ str=enumt.nextElement().toString(); out.println(str+":"+request.getParameter(str)+"<br>"); } } %> </body> </html>
<%@ page contentType="text/html;charset=gb2312"%> <%request.setCharacterEncoding("gb2312");%> <html> <head> <title>request对象_例3</title> </head> <body bgcolor="#FFFFF0"> <form action="" method="post"> 擅长:<input type="checkbox" name="cb" value="ON1">VC++ <input type="checkbox" name="cb" value="ON2">JAVA <input type="checkbox" name="cb" value="ON3">DELPHI <input type="checkbox" name="cb" value="ON4">VB <br> <input type="submit" value="进入" name="qwe"> </form> <% if(request.getParameter("qwe")!=null ){ for(int i=0;i<request.getParameterValues("cb").length;i++){ out.println("cb"+i+":"+request.getParameterValues("cb")[i]+"<br>"); } out.println(request.getParameter("qwe")); } %> </body> </html>
Response对象包含了响应客户请求的有关信息,但在JSP中不多直接用到它。它是HttpServletResponse类的实例。java
Session对象指的是客户端与服务器的一次会话,从客户连到服务器的一个WebApplication开始,直到客户端与服务器断开链接为止。它是HttpSession类的实例。sql
<%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.util.*" %> <html> <head><title>session对象_例1</title><head> <body><br> session的建立时间:<%=session.getCreationTime()%> <%=new Date(session.getCreationTime())%><br><br> session的Id号:<%=session.getId()%><br><br> 客户端最近一次请求时间:<%=session.getLastAccessedTime()%> <%=new java.sql. Time(session.getLastAccessedTime())%><br><br> 两次请求间隔多长时间此SESSION被取消(ms):<%=session.getMaxInactiveInterval()%><br><br> 是不是新建立的一个SESSION:<%=session.isNew()?"是":"否"%><br><br> <% session.putValue("name","霖苑编程"); session.putValue("nmber","147369"); %> <% for(int i=0;i<session.getValueNames().length;i++) out.println(session.getValueNames()[i]+"="+session.getValue(session.getValueNames()[i])); %> <!--返回的是从格林威治时间(GMT)1970年01月01日0:00:00起到计算当时的毫秒数--> </body> </html>
Out对象是JspWriter类的实例,是向客户端输出内容经常使用的对象。编程
<%@page contentType="text/html;charset=gb2312"%> <html><head><title>out对象_例1:缓存测试</title></head> <%@page buffer="1kb"%> <body> <% for(int i=0;i<2000;i++) out.println(i+"{"+out.getRemaining()+"}"); %><br> 缓存大小:<%=out.getBufferSize()%><br> 剩余缓存大小:<%=out.getRemaining()%><br> 自动刷新:<%=out.isAutoFlush()%><br> <%--out.clearBuffer();--%> <%--out.clear();--%> <!--缺省状况下:服务端要输出到客户端的内容,不直接写到客户端,而是先写到一个输出缓冲区中.只有在下面三中状况下,才会把该缓冲区的内容输出到客户端上: 1.该JSP网页已完成信息的输出 2.输出缓冲区已满 3.JSP中调用了out.flush()或response.flushbuffer() --> </body> </html>
Page对象就是指向当前JSP页面自己,有点象类中的this指针,它是java.lang.Object类的实例。数组
Application对象实现了用户间数据的共享,可存放全局变量。它开始于服务器的启动,直到服务器的关闭,在此期间,此对象将一直存在;这样在用户的先后链接或不一样用户之间的链接中,能够对此对象的同一属性进行操做;在任何地方对此对象属性的操做,都将影响到其余用户对此的访问。服务器的启动和关闭决定了application对象的生命。它是ServletContext类的实例。缓存
<%@ page contentType="text/html;charset=gb2312"%> <html> <head><title>APPLICATION对象_例1</title><head> <body><br> JSP(SERVLET)引擎名及版本号:<%=application.getServerInfo()%><br><br> 返回/application1.jsp虚拟路径的真实路径:<%=application.getRealPath("/application1.jsp")%><br><br> 服务器支持的Servlet API的大版本号:<%=application.getMajorVersion()%><br><br> 服务器支持的Servlet API的小版本号:<%=application.getMinorVersion()%><br><br> 指定资源(文件及目录)的URL路径:<%=application.getResource("/application1.jsp")%><br><br> <!--能够将application1.jsp换成一个目录--><br><br> <% application.setAttribute("name","霖苑计算机编程技术培训学校"); out.println(application.getAttribute("name")); application.removeAttribute("name"); out.println(application.getAttribute("name")); %> </body> </html>
<%@ page contentType="text/html;charset=gb2312"%> <html> <head><title>APPLICATION对象_例2</title><head> <body><br> <!--因为application一直存在于服务器端,能够利用此特性对网页记数--> <% if(application.getAttribute("count")==null) application.setAttribute("count","1"); else application.setAttribute("count",Integer.toString(Integer.valueOf(application.getAttribute("count").toString()).intValue()+1)); %> 你是第<%=application.getAttribute("count")%>位访问者 </body> <!--因为getAttribute()方法获得的是一个Object类型对象,用getString()方法转化为String类型--> <!--用Integer类的valueOf()方法把获得的String转化成Integer的对象,在用intValue()方法获得int型,再加1,最后把计算的结果用Integer.toString()方法转化成setAttribute()方法所要求的String类型--> </html>
<%@ page contentType="text/html;charset=gb2312"%> <html> <head><title>APPLICATION对象_例3</title><head> <body><br> <!--因为application一直存在于服务器端,能够利用此特性对网页记数--> <% String str=application.getAttribute("count").toString();//getAttribute("count")返回的是Object类型 int i=0; if(str==null) application.setAttribute("count","1"); else i=Integer.parseInt(str); //out.println(i); application.setAttribute("count",++i+""); %> 你是第<%=application.getAttribute("count")%>位访问者 </body> </html>
Exception对象是一个例外对象,当一个页面在运行过程当中发生了例外,就产生这个对象。若是一个JSP页面要应用此对象,就必须把isErrorPage设为true,不然没法编译。他其实是java.lang.Throwable的对象。服务器
PageContext对象提供了对JSP页面内全部的对象及名字空间的访问,也就是说他能够访问到本页所在的Session,也能够取本页面所在的application的某一属性值,他至关于页面中全部功能的集大成者,它的本 类名也叫PageContext。session
<%@page contentType="text/html;charset=gb2312"%> <html><head><title>pageContext对象_例1</title></head> <body><br> <% request.setAttribute("name","霖苑编程"); session.setAttribute("name","霖苑计算机编程技术培训"); //session.putValue("name","计算机编程"); application.setAttribute("name","培训"); %> request设定的值:<%=pageContext.getRequest().getAttribute("name")%><br> session设定的值:<%=pageContext.getSession().getAttribute("name")%><br> application设定的值:<%=pageContext.getServletContext().getAttribute("name")%><br> 范围1内的值:<%=pageContext.getAttribute("name",1)%><br> 范围2内的值:<%=pageContext.getAttribute("name",2)%><br> 范围3内的值:<%=pageContext.getAttribute("name",3)%><br> 范围4内的值:<%=pageContext.getAttribute("name",4)%><br> <!--从最小的范围page开始,而后是reques、session以及application--> <%pageContext.removeAttribute("name",3);%> pageContext修改后的session设定的值:<%=session.getValue("name")%><br> <%pageContext.setAttribute("name","应用技术培训",4);%> pageContext修改后的application设定的值:<%=pageContext.getServletContext().getAttribute("name")%><br> 值的查找:<%=pageContext.findAttribute("name")%><br> 属性name的范围:<%=pageContext.getAttributesScope("name")%><br> </body></html>
Config对象是在一个Servlet初始化时,JSP引擎向它传递信息用的,此信息包括Servlet初始化时所要用到的参数(经过属性名和属性值构成)以及服务器的有关信息(经过传递一个ServletContext对象)。app