访问JSF会话范围从servlet的豆这是由所谓的小程序嵌入在Web应用程序的JSF [英] Accessing JSF session scoped bean from servlet which is called by applet embedded in JSF webapp

查看:303
本文介绍了访问JSF会话范围从servlet的豆这是由所谓的小程序嵌入在Web应用程序的JSF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要访问会话从一个servlet作用域bean。我已经尝试过

I need to access a session scoped bean from a servlet. I already tried

UserBean userBean = (UserBean) request.getSession().getAttribute("userBean");

在本帖子描述。但我只得到null作为结果,altough的UserBean的实例alreay instatiated。这些都是注释/进口我用的UserBean:

as described in this post. But I only get null as result, altough an instance of UserBean is alreay instatiated. Those are the annotations/imports I use for userBean :

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class UserBean implements Serializable{
 ... }

一些背景,为什么我无法摆脱的servlet:我有我的JSF页面文件上传小程序。此applet期待一个ADRESS它可以把它的POST请求。 (我不能编辑这个职位要求添加更多的字段或东西)。我的servlet的POST方法,然后存储文件。这项工作不能由托管bean来完成,因为servlet有与@MultiPartConfig被注解,我不能这个注释添加到JSF托管bean。

Some Background why I can't get rid of the servlet : I have a file upload applet in my jsf page. This applet expects an adress where it can send it's POST request. (I can't edit this post request to add more fields or something). The post method of my servlet then stores the file. This job can't be done by a managed bean because the servlet has to be annotated with @MultiPartConfig and I can't add this annotation to the jsf managed bean.

推荐答案

如果返回,那么它只能意味着两件事情:

If it returns null, then it can only mean 2 things:


  1. JS​​F还没有创建bean事先呢。

  2. 小程序,servlet的互动不使用相同的HTTP会话的Web应用程序。

给你如何描述的功能需求的方式,我认为是后者。你需要确保你通过web应用程序的会话标识符与小程序以及HTTP请求一起。这可以在 JSESSIONID 饼干或 JSESSIONID 的形式URL路径属性。

Given the way how you described the functional requirement, I think it's the latter. You need to ensure that you pass the session identifier of the webapp along with the HTTP request from the applet as well. This can be in the form of the JSESSIONID cookie or the jsessionid URL path attribute.

首先,你需要告诉web应用程序正在使用的会话ID的小程序。小程序> 或<对象> 标记拿着小程序&LT做p>

First, you need to tell the applet about the session ID the webapp is using. You can do it by passing a parameter to <applet> or <object> tag holding the applet

<param name="sessionId" value="#{session.id}" />

(即#{会话} 是一个隐含的JSF EL变量引用当前的 的HttpSession 这反过来有一个的getId()方法;您不必为创造一个托管bean左右,code以上线齐全原样)

(the #{session} is an implicit JSF EL variable referring the current HttpSession which in turn has a getId() method; you don't need to create a managed bean for that or so, the above line of code is complete as-is)

String sessionId = getParameter("sessionId");

您没有说明你是如何与servlet的交互,但假定你正在使用standard Java SE的的URLConnection 为此,指向 @WebServlet(/ servleturl)的servlet,然后您可以使用调用setRequestProperty()来设置请求头:

You didn't describe how you're interacting with the servlet, but assuming that you're using the standard Java SE URLConnection for this, pointing to the @WebServlet("/servleturl") servlet, then you can use setRequestProperty() to set a request header:

URL servlet = new URL(getCodeBase(), "servleturl");
URLConnection connection = servlet.openConnection();
connection.setRequestProperty("Cookie", "JSESSIONID=" + sessionId);
// ...

另外,你也可以把它当作一个URL路径属性:

Alternatively, you can also pass it as a URL path attribute:

URL servlet = new URL(getCodeBase(), "servleturl;jsessionid=" + sessionId);
URLConnection connection = servlet.openConnection();
// ...

(请注意,该情况下,在这两种情况下事项)

无论哪种方式,这种方式的小程序,servlet的互动将发生在同一HTTP会话JSF管理的bean。

Either way, this way the applet-servlet interaction will take place in the same HTTP session as JSF managed beans.

这篇关于访问JSF会话范围从servlet的豆这是由所谓的小程序嵌入在Web应用程序的JSF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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