Reputation: 5972
I'm new to ejb. Actually I've created one ejb and I added reference to a web application which will call the session bean simply. How to call the session bean from the jsp file?
Upvotes: 4
Views: 23168
Reputation: 1
You can mix and match to support multiple application servers in the best way. The code below uses the @EJB injection for WebSphere Liberty and the 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();
}
}
}
%>
Upvotes: 0
Reputation: 1
Simple..Override Jsp's jspInit method and create InitialContext object.. InitialContext object can access all the resources that have JNDI name assigned..
<%!
BeanIntefaceName instanceName;
%>
<%
public void jspInit()
{
instanceName = (BeanIntefaceName)new InitialContext().lookup("java:global/[application_name]/[module_name]/[enterprise_bean_name]/[inteface_name]");
}
instanceName.yourMethodName();
%>
Upvotes: 0
Reputation: 1732
I've tried to do that on Wildfly, but without success using @EJB annotation, probabily JSP don't have CDI. So I've implemented it on another way (not so bright):
Before the :
<%
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 all the rest stays the same!
Upvotes: 2
Reputation: 11926
As you are using EJB at service layer and in MVC , I will never advice to call a session bean from your view or jsp.you can call the session beans method by injection EJB reference using @EJB annotation.
Upvotes: 0
Reputation: 3582
I could also prefer you to use the MVC model for your application. In that case there is no need to call a session bean from the jsp, you can call it from the servlets itself.
Check out this link to call a EJB from a servlet. Click
Upvotes: 4
Reputation: 3582
1) the first way will be 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 normal jsp
<%=object.callmethod()%>
2) the other way will be to use standard actions
<jsp:useBean id="beanId' class="packagename.ClassName" />
<jsp:getStudentInfo name="beanId" property="name"/>
Upvotes: 0