java获取request
‘壹’ java中Request对象的主要方法有哪些
答:setAttribute(Stringname,Object):设置名字为name的request的参数值
getAttribute(Stringname):返回由name指定的属性值
getAttributeNames():返回request对象所有属性的名字集合,结果是一个枚举的实例
getCookies():返回客户端的所有Cookie对象,结果是一个Cookie数组
getCharacterEncoding():返回请求中的字符编码方式
getContentLength():返回请求的Body的长度
getHeader(Stringname):获得HTTP协议定义的文件头信息
getHeaders(Stringname):返回指定名字的requestHeader的所有值,结果是一个枚举的实例
getHeaderNames():返回所以requestHeader的名字,结果是一个枚举的实例
getInputStream():返回请求的输入流,用于获得请求中的数据
getMethod():获得客户端向服务器端传送数据的方法
getParameter(Stringname):获得客户端传送给服务器端的有name指定的参数值
getParameterNames():获得客户端传送给服务器端的所有参数的名字,结果是一个枚举的实例
getParameterValues(Stringname):获得有name指定的参数的所有值
getProtocol():获取客户端向服务器端传送数据所依据的协议名称
getQueryString():获得查询字符串
getRequestURI():获取发出请求字符串的客户端地址
getRemoteAddr():获取客户端的IP地址
getRemoteHost():获取客户端的名字
getSession([Booleancreate]):返回和请求相关Session
getServerName():获取服务器的名字
getServletPath():获取客户端所请求的脚本文件的路径
getServerPort():获取服务器的端口号
removeAttribute(Stringname):删除请求中的一个属性
‘贰’ Java 怎么在一个普通类中获取到Request对象
首先,向你确认你所谓的普通类就是POJO吗,如果是是无法获取Request对象的。
不过,可以使用参数传递的方式。
Request对象,即ServletRequest,隶属于javax包下的,需要实现其HttpServletRequest接口才能获取到Request对象
故在普通java类中是无法获取到request对象的
望参考
‘叁’ java怎么获取request
java webservice中通过注入WebServiceContext ,获取request、session等
代码如下:
@Resource
private WebServiceContext webServiceContext;
public String getLoginUser() {
HttpServletRequest request =
(HttpServletRequest) webServiceContext.getMessageContext().get(MessageContext.SERVLET_REQUEST);
HttpSession session = request.getSession();
return session.getAttribute("loginUser").toString();