<%@ 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>
<%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.util.*" %> <html> <head><title>session对象_例1</title><head> <body><br> session的创建时间:<%=session.getCreationTime()%> & lt;%=new Date(session.getCreationTime())%><br><br> session的Id号:<%=session.getId()%><br><br> 客户端最近一次请求时间:<%=session.getLastAccessedTime()%> & nbsp;<%=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>
<%@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 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")%& gt;<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>
<%@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>
3 Enumeration getInitParameterNames() 返回Servlet初始化所需所有参数的枚举