如何获取请求头、远程地址和其他HttpServletRequest特定信息?

发布于 2024-12-22 10:05:45 字数 1193 浏览 1 评论 0原文

我有一个 JSF 2.0 Web 项目,我的 Web 有一个表单,它必须执行以下操作:

  1. 获取表单的参数并将其保存在 Bean 中(完成)

  2. 从 servlet 获取此信息:

    • 远程地址:
    • 远程主机:
    • 区域设置:
    • 内容类型:
    • 边界:
    • 内容长度:
    • 字符编码:

  3. 将 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:

  1. Get the parametres of the form and save it in a Bean (Done)

  2. Get this information from the servlet:

    • Remote Address:
    • Remote Host:
    • Locale:
    • Content Type:
    • Boundary:
    • Content Length:
    • Character Encoding:
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

善良天后 2024-12-29 10:05:45

HttpServletRequest 对象位于 JSF 中,可通过 ExternalContext#getRequest()

ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
// ...

ExternalContext顺便还提供了一些直接的方法来获取所需的信息。检查javadoc

您不需要为此使用另一个 servlet。 JSF 已经将 FacesServlet 作为唯一的请求/响应控制器。

The HttpServletRequest object is in JSF available by ExternalContext#getRequest().

ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
// ...

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.

You don't need another servlet for this. JSF has already the FacesServlet as the sole request/response controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文