将值从Servlet传递到WebLogic中的JSF Action方法 [英] Passing value from Servlet to JSF Action method in WebLogic

查看:187
本文介绍了将值从Servlet传递到WebLogic中的JSF Action方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将ObjectServletdoPost()传递给JSF的Managed bean的操作方法.但是我无法做到这一点.

I am trying to pass an Object from Servlet's doPost() to JSF's Managed bean's action method. But I am unable to do that.

我尝试将Servlet中的值设置为:

I have tried to set the value from Servlet as:

request.getSession().setAttribute(key, "JYM");

并尝试以Managed bean的形式检索它:

And tried to retrieve it form Managed bean as:

FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(key)

它正在返回null.

这也从Managed bean返回null:

((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getSession().getAttribute(key);

也来自Managed bean,这将返回null:

((HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false)).getAttribute(key)

我将密钥传递为:

'${pageContext.request.contextPath}/uploadservlet;jsessionid=${pageContext.session.id}?key=<h:outputText value="#{uploadBean.key}" />'

uploadBeanManaged bean的名称,并且key生成为:

uploadBean is the name of the Managed bean and the key is generated as:

key = UUID.randomUUID().toString();

key在Servlet和托管Bean中均保持不变.我打印的是支票.

The key remains unchanged in both of the Servlet and in managed bean. I have printed is to check.

如何将ObjectServlet传递到Action?任何指针都将非常有帮助.

How can I pass the Object from Servlet to Action? Any pointer would be very helpful.

Managed bean在会话范围内.

通过使用ServletContext我可以传递值:

By using ServletContext I am able to pass the value:

这是我所做的: 在Servlet中:

Here is what I did: In Servlet:

String key = request.getParameter("key");

if (getServletContext().getAttribute(key) == null) {
    List<FileItem> fileFields = new ArrayList<FileItem>();
    fileFields.add(fileField);
    getServletContext().setAttribute(key, fileFields);
} else {
    List<FileItem> fileFields = (List<FileItem>)getServletContext().getAttribute(key);
    fileFields.add(fileField);
}

从会话范围的bean中获取:

And from session scoped bean:

ServletContext servletContext = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext());
List<FileItem> fileFields = (List<FileItem>)servletContext.getAttribute(key);
servletContext.setAttribute(key, null);

现在fileFields不再为null.我了解的是ServletContext的行为类似于Application Scoped变量.

Now the fileFields is not null anymore. What I understand is the ServletContext behave like Application Scoped variable.

HttpSessionListener的实现:

这是我写的课:

public class UploadListener implements HttpSessionListener {
    private HttpSession session = null;

    public void sessionCreated(HttpSessionEvent event) {
        session  = event.getSession();
        session.setMaxInactiveInterval(10);
    }

    public void sessionDestroyed(HttpSessionEvent event) {
        session  = event.getSession();
        Set<String> keys = (Set<String>) session.getAttribute("key");
        Map<String, Object> data = (Map<String, Object>) session.getServletContext().getAttribute("key");
        data.keySet().removeAll(keys);
    }
}

我将ServletContext中的值设置为:

String key = request.getParameter("key");

List<FileItem> fileFields = (List<FileItem>)getServletContext().getAttribute(key);

if (fileFields == null) {
    fileFields = new ArrayList<FileItem>();
    getServletContext().setAttribute(key, fileFields);
}

fileFields.add(fileField);

这就是我调用Servlet的方式:'${pageContext.request.contextPath}/uploadservlet?key=<h:outputText value="#{uploadBean.key}" />'.

And this is the way I am calling the Servlet: '${pageContext.request.contextPath}/uploadservlet?key=<h:outputText value="#{uploadBean.key}" />'.

推荐答案

如果servlet容器不支持通过jsessionid URL片段标识HTTP会话,则此构造将失败.默认情况下支持此功能,但是可以通过servletcontainer特定的配置将其关闭.到目前为止,很遗憾,您的Weblogic服务器已配置为这样.

This construct will fail if the servletcontainer doesn't support identifying the HTTP session by jsessionid URL fragment. This is by default supported, but it's possible to turn off this by servletcontainer specific configuration. So far it unfortunately looks like that your Weblogic server is configured as such.

您最好的选择是在应用程序范围内交换数据. UUID的随机性足够强,不会引起冲突.您只需要确保在销毁会话时清除与会话相关的数据.否则,内存将泄漏掉.为此,您可以使用 HttpSessionListener .假设您将密钥存储在应用程序范围(引用共享数据)和会话范围(引用到目前为止使用的所有密钥的集合)中,则sessionDestroyed()实现可以如下所示:

Your best bet is then to exchange the data in the application scope. The randomness of UUID is strong enough to not cause clashes. You should only need to make sure that the session-associated data is cleaned up when the session is destroyed. Otherwise the memory will leak away. For this, you can use a HttpSessionListener. Provided that you store the key in both the application scope (referencing the shared data) and in the session scope (referencing a set of all keys used so far), then the sessionDestroyed() implementation can look like this:

public void sessionDestroyed(HttpSessionEvent event) {
    Set<String> keys = (Set<String>) event.getSession().getAttribute("keys");
    Map<String, Object> data = (Map<String, Object>) event.getSession().getServletContext().getAttribute("data");
    data.keySet().removeAll(keys);
}


更新,具体取决于您的更新,一种更优雅的方式来获取/设置它们:


Update as per your update, a bit more elegant way to get/set them is:

String key = request.getParameter("key");
List<FileItem> fileFields = (List<FileItem>) getServletContext().getAttribute(key);

if (fileFields == null) {
    fileFields = new ArrayList<FileItem>();
    getServletContext().setAttribute(key, fileFields);
}

fileFields.add(fileField);

List<FileItem> fileFields = (List<FileItem>) FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().remove(key);
// ...

这篇关于将值从Servlet传递到WebLogic中的JSF Action方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆