如何获取请求头、远程地址和其他HttpServletRequest特定信息?
我有一个 JSF 2.0 Web 项目,我的 Web 有一个表单,它必须执行以下操作:
获取表单的参数并将其保存在 Bean 中(完成)
从 servlet 获取此信息:
- 远程地址:
- 远程主机:
- 区域设置:
- 内容类型:
- 边界:
- 内容长度:
- 字符编码:
将 Bean 数据和 Servlet 数据插入数据库的表中(等待步骤 2)
I对 JSF 中的 Servlet 不太了解,我不需要是否必须制作一个。我只有该代码,但在 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 have a JSF 2.0 web project, my web have a form and it have to do:
Get the parametres of the form and save it in a Bean (Done)
Get this information from the servlet:
- Remote Address:
- Remote Host:
- Locale:
- Content Type:
- Boundary:
- Content Length:
- Character Encoding:
Insert the Bean data and Servlet data in a table of a database (waiting step 2)
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";
.....
..
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HttpServletRequest
对象位于 JSF 中,可通过ExternalContext#getRequest()
。ExternalContext
顺便还提供了一些直接的方法来获取所需的信息。检查javadoc。您不需要为此使用另一个 servlet。 JSF 已经将 FacesServlet 作为唯一的请求/响应控制器。
The
HttpServletRequest
object is in JSF available byExternalContext#getRequest()
.The
ExternalContext
by the way also offers some direct methods to get the desired information. Check the methods starting withgetRequestXxx()
such asgetRequestHeaderMap()
,getRequestContentType()
, etc in the javadoc.You don't need another servlet for this. JSF has already the
FacesServlet
as the sole request/response controller.