从由嵌入在 JSF webapp 中的小程序调用的 servlet 访问 JSF 会话范围的 bean [英] Accessing JSF session scoped bean from servlet which is called by applet embedded in JSF webapp

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

问题描述

我需要从 servlet 访问会话范围的 bean.我已经试过了

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

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

如本帖子中所述.但是我只得到 null 作为结果,虽然 UserBean 的实例已经实例化.这些是我用于 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 页面中有一个文件上传小程序.这个小程序需要一个地址,它可以发送它的 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.

推荐答案

如果返回null,那么只能说明两件事:

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

  1. JSF 尚未事先创建该 bean.
  2. applet-servlet 交互不使用与 web 应用程序相同的 HTTP 会话.

鉴于您描述功能需求的方式,我认为是后者.您需要确保传递 webapp 的会话标识符以及来自小程序的 HTTP 请求.这可以采用 JSESSIONID cookie 或 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.您可以通过将参数传递给 标签来实现此小程序

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}" />

(#{session} 是一个隐式的 JSF EL 变量,引用当前的 HttpSession 又具有 getId() 方法;您不需要要为此创建一个托管 bean,上面的代码行按原样完成)

(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)

可以在小程序中检索如下:

which can be retrieved in the applet as follows:

String sessionId = getParameter("sessionId");

您没有描述如何与 servlet 交互,但假设您使用的是 标准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();
// ...

(请注意,在这两种情况下,案例都很重要)

无论哪种方式,applet-servlet 交互都将在与 JSF 托管 bean 相同的 HTTP 会话中进行.

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

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

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