如何获取请求标头,远程地址和其他HttpServletRequest特定信息? [英] How to obtain request headers, remote address and other HttpServletRequest-specific information?
问题描述
我有一个JSF 2.0 Web项目,我的Web有一个表单,它必须这样做:
I have a JSF 2.0 web project, my web have a form and it have to do:
-
获取表单的参数并将其保存在Bean中(完成)
Get the parametres of the form and save it in a Bean (Done)
从servlet获取此信息:
Get this information from the servlet:
- 远程地址:
- 远程主机:
- 语言环境:
- 内容类型:
- 边界:
- 内容长度:
- 字符编码:
- Remote Address:
- Remote Host:
- Locale:
- Content Type:
- Boundary:
- Content Length:
- Character Encoding:
将Bean数据和Servlet数据插入数据库表中(等待步骤2)
Insert the Bean data and Servlet data in a table of a database (waiting step 2)
我对JSF中的Servlet知之甚少,我是否需要制作一个就不需要.我只有这样的代码,但是在JSP中:
I dont know much about Servlets in JSF, i dont need if i have to make one or not. I only have the code of that but in JSP:
String informe="";
Enumeration a = request.getHeaderNames();
while(a.hasMoreElements() ){
String h = a.nextElement().toString();
informe += h+": "+request.getHeader(h)+"\n";
}
a = request.getAttributeNames();
while(a.hasMoreElements() ){
String h = a.nextElement().toString();
informe += h+": "+request.getHeader(h)+"\n";
}
informe += "Remote Address: "+request.getRemoteAddr()+"\n";
informe += "Remote Host: "+request.getRemoteHost()+"\n";
informe += "Locale: "+request.getLocale()+"\n";
informe += "Content Type: "+request.getContentType()+"\n";
informe += "Content Length: "+request.getContentLength()+"\n";
.....
..
我不知道如何在JSF中获得请求信息以及必须执行的步骤.我读了很多书,但我认为我并不需要他们做的所有事情.
I don't know how I can get the request information in JSF and wich steps I have to do. I readed a lot of pages but I think that I don't need all things that they do.
推荐答案
The HttpServletRequest
object is in JSF available by ExternalContext#getRequest()
.
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
// ...
ExternalContext
方式还提供了一些直接的方法来获取所需的信息.在getRequestHeaderMap()
,getRequestContentType()
等. html"rel =" nofollow> javadoc .
The ExternalContext
by the way also offers some direct methods to get the desired information. Check the methods starting with getRequestXxx()
such as getRequestHeaderMap()
, getRequestContentType()
, etc in the javadoc.
您不需要其他servlet. JSF已经将FacesServlet
作为唯一的请求/响应控制器.
You don't need another servlet for this. JSF has already the FacesServlet
as the sole request/response controller.
这篇关于如何获取请求标头,远程地址和其他HttpServletRequest特定信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!