How to call a bean session from jsp

I am new to ejb. Actually, I created one ejb and I added a link to a web application that would just call a bean session. How to invoke bean session from jsp file?

+4
source share
6 answers

I might also prefer using the MVC model for your application. In this case, there is no need to invoke a bean session from jsp, you can invoke it from the servlets themselves.

Check out this link to invoke EJB from the servlet. Click

+4
source

I tried to do this on Wildfly, but without success using the @EJB annotation, the JSP probably doesn't have a CDI. Therefore, I implemented it differently (not so brightly):

Before:

<% LoginAction loginAction; try { Properties properties = new Properties(); properties.put("jboss.naming.client.ejb.context", true); properties.put(Context.URL_PKG_PREFIXES,"org.jboss.ejb.client.naming"); Context ctx=new InitialContext(properties); loginAction = (LoginAction) ctx.lookup("java:module/LoginAction"); session.setAttribute("loginAction", loginAction); //this is to be able to use loginAction on EL Expressions! } catch (Exception e) { e.printStackTrace(); } %> 

And everything else remains unchanged!

+2
source

1) the first way is to create a direct object

use import tag to import ur class

 < % @ page import = packageName.Classname %> <% @EJB Classname object = new Classname(); %> 

and then access methods using regular jsp

 <%=object.callmethod()%> 

2) another way would be to use standard actions

 <jsp:useBean id="beanId' class="packagename.ClassName" /> <jsp:getStudentInfo name="beanId" property="name"/> 
0
source

Since you use EJB at the service level and in MVC, I will never advise calling a bean session from your view, or jsp.you can call the beans session method by referencing the EJB insert using the @EJB annotation.

0
source

A simple method. Install Jsp jspInit and create the InitialContext object. An InitialContext object can access all resources that are named JNDI.

 <%! BeanIntefaceName instanceName; %> <% public void jspInit() { instanceName = (BeanIntefaceName)new InitialContext().lookup("java:global/[application_name]/[module_name]/[enterprise_bean_name]/[inteface_name]"); } instanceName.yourMethodName(); %> 
0
source

You can combine and match to support multiple application servers in the best way. The code below uses @EJB injection for WebSphere Liberty and InitialContext for JBoss Wildfly

 <%! @EJB GitlabHelper gitAPI; public void jspInit() { if (gitAPI == null) { try { gitAPI = (GitlabHelper) new InitialContext().lookup("java:module/GitlabHelper"); System.out.println("<!-- initContext has been used -->"); } catch (Exception e) { e.printStackTrace(); } } } %> 
0
source

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


All Articles