`

Request用法

    博客分类:
  • JSP
阅读更多
转载:http://hi.baidu.com/yuanaishun/blog
获取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头文件中accept-encoding的值:
<%
String header4 = request.getHeader("accept-encoding");
out.println(header4);
%>
1、   Request对象主要方法:

☉ getAttribute(String name)   返回name属性值。

☉ getAttributeNames()   返回request对象所有属性名字。

☉ getCookies()   返回客户端的cookies对象。

☉ getHeader(String name)   获得HTTP协议定义的文件头信息。

☉ getHeaders(String name)   返回指定名字的request Header的所有值。

☉ getMethod()   获得客户端向服务器端传送数据的方法。

☉ getParameter(String name)   获得客户端传送给服务器端的参数值。

☉ getParameterNames()   获得客户端传送给服务器端的所有参数的名字。

☉ getParameterValue(String name)   获得指定参数的所有值。

☉ getProtocol()   获取客户端向服务器端传送数据所依据的协议名称。

☉ getQueryString()   获得查询字符串。

☉ getRequestURI()   获取发出请求字符串的客户端地址。

☉ getRemoteAddr()   获取客户端的IP地址。

☉ getRemoteHost()   获取客户端的名字。

☉ getServerName()   获取服务器的名字。

☉ getServletPath()   获取客户端所请求的脚本文件的文件路径。

☉ getServerPort()   获取服务器的端口号。

☉ setAttribute(String name , java.lang.Object objt)  设置名字为name的request参数的值,该值是由java.lang.Object类型的objt指定。


实例:[Request.jsp]

<html>

<body>

<h1> Request Information</h1>

<hr>

JSP Request Method:<%= request.getMethod() %>

<br>

Request URI:<%= request.getRequestURI() %>

<br>

Request Protocol:<%= request.getProtocol() %>

<br>

Servlet Path:<%= request.getServletPath() %>

<br>

Remote User:<%= request.getRemoteUser() %>

<br>

Remote Address:<%= request.getRemoteAddr() %>

<br>

Remote host:<%= request.getRemoteHost() %>

<br>

Path info:<%= request.getPathInfo() %>

<br>

Path translated:<%= request.getPathTranslated() %>

<br>

Query String:<%= request.getQueryString() %>

<br>

Content Length:<%= request.getContentLength() %>

<br>

Content Type:<%= request.getContentType() %>

<br>

Server Name:<%= request.getServerName() %>

<br>

Server Port:<%= request.getServerPort() %>

<br>

Authorization Scheme:<%= request.getAuthType() %>

<hr>

The Browser you are using is <%= request.getHeader("User-Agent") %>

<hr>

</body>

</html>


2、  Response对象主要方法:

☉ addCookie(Cookie cook)   添加一个cookie对象,用来保存客户端的用户信息。

☉ addHeader(String name , String value)   添加HTTP文件头信息。

☉ containsHeader(String name)   判断指定名字的HTTP文件头是否已经存在。

☉ sendError(int)   向客户端发送错误的信息。

☉ setHeader(String name , String value)   设置指定名字的HTTP文件头的值。


实例:定时刷新[refresh.jsp]

<%@ page contentType="text/html;charset=GB2312" %>

<%@ page import="java.util.Date" %>

<html>

<head>

<title>定时刷新页面</title>

</head>

<body>

<b>本页用来说明response对象</b>

<br>

<b>当前时间为:</b>

<% response.setHeader("refresh","10"); %>

<%

out.println(new Date());

%>

</body>

</html>


3、  Out对象主要方法:

☉ out.print/println (Boolean | char | char[] | double | float | int | long | object | string |)   输出各种类型的数据。

☉ out.newLine()   输出一个换行字符。

☉ out.flush() 输出缓冲区里的数据。

☉ out.close()   关闭输出流。

☉ out.clearBuffer()   清除缓冲区里的是数据,并把数据输出到客户端。

☉ out.clear()   清除缓冲区里的是数据,但不会把数据输出到客户端。

☉ out.getBufferSize()   获得缓冲区的大小。

☉ out.getRemaining()   获取缓冲区中没有被占用的空间的大小。

☉ out.isAutoFlush() 返回布尔值。


4、  Session对象主要方法:

☉ getAttribute(String name)     获取与指定名字name相联系的信息。

☉ getAttributeNames()   返回session对象中存储的每一个属性对象。

☉ getCreationTime()     返回session被创建的时间。

☉ getId()   返回唯一的标识,为每一个session而产生。

☉ getLastAccessedTime()     返回当前session对象最后被客户发送的时间。

☉ getMaxInactiveInterval() 返回总时间(秒)。

☉ removeAttribute(String name) 删除与指定名字name的相联系的信息。

☉ setAttribute(String name , java.lang.object value)    设置指定名字name的属性值value,并存储在session对象中。


5、  PageContext对象主要方法:

☉ getAttribute scope() 检索一个特定的已经命名对象的范围。

☉ findAttribute()   用来按照页面请求、会话以及应用程序范围的顺序实现对某个已经命名属性的搜索。

☉ setAttribute()    用来设置默认页面的范围或特定范围之中的已命名对象。

☉ removeAttribute()     用来删除默认页面范围或特定范围之中已命名的对象。


6、  Application对象主要方法:

☉ getAttribute(String name)     返回由name指定的名字application对象的属性的值。

☉ getAttributeNames()   返回所有application对象的属性的名字。

☉ getInitParameter(String name)     返回由name指定的名字的application对象的某个属性的初始值。

☉ getServletInfo() 返回servlet编译器当前版本信息。

☉ setAttribute(String name , Object object)     设置指定名字name的application对象的属性值object.


实例:在线人数统计[online.jsp]

<%@ page contentType="text/html;charset=gb2312" %>

<body onunload="javascript:cancel();">

<%

Integer i = (Integer)application.getAttribute("counter");

String name = (String)application.getAttribute("onlineName");

//StringBuffer total = new StringBuffer(name);

%>

<font color="blue">

<% out.println((String)session.getAttribute("username")); %>您好:

</font>

<br>

<font color="red">

当前在线人数: <%= i %>

</font>

<br>

<font color="green">

在线人总名册: <% out.println(name); %>

</font>

<script language="javascript">

<!--

function cancel(){

<%

if(session.getAttribute("username")!="" && session.getAttribute("username")!=null){

//cancelname 为注销人名

String cancelname;

cancelname=(String)session.getAttribute("username");

//out.println(cancelname);


//j为cancelname在总名册中的初始位置

//k为cancelname的总长度

//t为cancelname在总名册中的结束位置

int j,k,t;

j=name.indexOf(cancelname);

k=cancelname.length();

t=j+k;

//out.println(j);

//out.println(t);

StringBuffer total=new StringBuffer(name);

total=total.delete(j-4,t);

i=new Integer(i.intValue()-1);

application.setAttribute("counter",i);

name=(String)total.toString();

application.setAttribute("onlineName",name);

//out.println("window.alert("+name+")");

session.setAttribute("username","");

}

%>

}

-->

</script>

</body>

分享到:
评论

相关推荐

    Spring实战之Bean的作用域request用法分析

    主要介绍了Spring实战之Bean的作用域request用法,结合实例形式分析了spring中Bean的request作用域相关使用技巧与操作注意事项,需要的朋友可以参考下

    request的用法总结大全.docx

    request的用法总结大全.docx

    Python request使用方法及问题总结

    主要介绍了Python request使用方法及问题总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    Jsp的request的用法

    Jsp的request的用法Jsp的request的用法Jsp的request的用法Jsp的request的用法Jsp的request的用法Jsp的request的用法Jsp的request的用法Jsp的request的用法Jsp的request的用法Jsp的request的用法Jsp的request的用法...

    browser-request, node.js 请求包兼容的浏览器库.zip

    browser-request, node.js 请求包兼容的浏览器库 浏览器请求:你可以看到的简单的HTTP库浏览器请求是Mikeal无处不在的端口,并向浏览器提供优秀的[request] [req] 包。嫉妒 node.jsPining的智能回调? 请求是为你...

    介绍JSP中request属性的用法

    NULL 博文链接:https://lao1984wang.iteye.com/blog/921876

    http-request例子

    //3.2使用BeanUtils封装 try { BeanUtils.populate(loginUser,map); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } //4....

    了解JSP中request属性的用法

    很全面的了解request的属性和用法,叫你如何是使用request

    Request对象主要方法

    新人备忘Request的好东西。了解方法才能更好的使用!

    浅谈flask中的before_request与after_request

    本文主要是对flask中的before_request与after_request用法做一个简单的分析,具体实例和介绍如下。 使用before_request 和 after_request的方法都非常简单,使用 @app.before_request 或者@app.after_request 修饰...

    request 和response的用法

    在c#Asp.Net简单告诉你request和response的用法;

    IDEA Fast xxx Request

    资源名称暂时避嫌,名称以及操作需要注意。 本资源为IDEA中的插件,插件版本是1.7,如果有使用或者需要使用的,请把插件名称写在评论区供他人了解,谢谢! 插件地址: ...插件详细使用方法自己处理。

    Android Request Response初级用法样例-源代码.rar

    演示Android Request Response的初级用法,附带本样例源代码,在WEB开发中, Request Response也是很重要的两个方法,在Android开发中,同样重要,也很基础,这个例子就是教会android新手如何使用好Request和...

    node使用request请求的方法

    近期使用node做服务端渲染,作为中间层需要请求后端接口,需要封装服务端的请求,接下来来了解下如何使用 request。 基本使用 const request = require('request') 引入这个包就可以开始使用了,最简单的使用方式...

    解读@RequestBody的正确使用方法

    主要介绍了解读@RequestBody的正确使用方法,具有一定借鉴价值

    struts2使用request、response

    struts1中使用request、response都是方法自带的,而struts2中的request、response、session都被隐藏的了,该带代码文档会清晰的让你明白struts2中request、response在自定义方法中如何随意使用

    微信小程序 wx.request(接口调用方式)详解及实例

    主要介绍了微信小程序 wx.request(接口调用方式)详解及实例的相关资料,wx.request请求方式比较简单,但是在使用的时候出现错,这里就记录下,需要的朋友可以参考下

    request的使用.txt

    ## request对象的方法使用 ```javascript String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %&gt; ``` 该...

    微信小程序-控制微信小程序 wx.request 并发请求数量

    如果需要 wx.request 方法支持 Promise,可以使用 wx-promise-request 库哦。 下载 由于小程序不支持 npm,所以直接右键保存 index.js 文件即可。 使用 在 app.js 引入并执行即可 import { queue } from './wx-queue...

    idea 插件 Restful Fast Request - API Buddy

    它是一个功能强大的Restful API工具包插件,可以根据已有的方法快速生成接口调试用例。它有一个漂亮的界面来完成请求、检查服务器响应、存储你的API请求和导出API请求,该插件能帮助你在IDEA内更快更高效地调试API!...

Global site tag (gtag.js) - Google Analytics