Embed Servlet object in CDI pojo on glass fish

I'm sure there is nothing new in the Java EE6 world (however, JEE5, I know quite well).

My environment is Glassfish 3.1.2

I am trying to embed servlet objects in a CDI bean (pojo annotated with @Named and @ * Scoped) which is called for jsp with $ {nameofBean.nameofMethode (par1 ...)}

I try this:

@Inject private ServletContext context; (or HTTPServletRequest or HttpServletResponse (for calling sendRedirect)) 

Which gives me the following deployment exception: WELD-001408 Unsatisfactory dependencies for type [ServletContext] with qualifiers [@Default] at the injection point

Apparently this might work on JBoss: http://docs.jboss.org/seam/3/servlet/latest/reference/en-US/html/injectablerefs.html

One of my ideas was to use @Produces, but so far I have not figured out how to do this.

Perhaps the word why I want to do this:
I want to do some CRUD things, but I don't know how to handle exceptions. So my idea is to create a CDI Bean that is invoked by the JSP and which invokes some ejb in the backend. If EJB throws an exception, I want to catch this in a CDI bean and something with a request object (redirect somewhere, set message ...). With the introduction of the servlet context, I would also gain access to the POST parameters. I want to avoid using servlets to support jsps and getting in touch with ejbs (I think it's too ld-fashioned).

thanks for the help

Willow

+4
source share
1 answer

The reference to the ServletContext is entered using Weld using @Resource :

 @Resource private ServletContext servletContext; 

However, you cannot enter HttpServletRequest or HttpServletResponse . To do this, you need a servlet filter that stores the request and response object. Solder for seams provides this opportunity.

I am not an expert in interface development using JSF or JSP, but I am convinced that there is a better way to handle exceptions than using ServletContext or response objects ...

-1
source

Source: https://habr.com/ru/post/1402640/


All Articles