-
1. Re: EJB3 injection in servlet with JBoss AS 7
sfcoy Mar 31, 2013 8:04 PM (in response to evgeniy3054)Hi there, and welcome to the JBoss forums!
Can you show us the code that is not working?
-
2. Re: EJB3 injection in servlet with JBoss AS 7
ybxiang.china Mar 31, 2013 8:49 PM (in response to evgeniy3054)I have same question too.
Now, I try my best to use JSF MBean instead of Servlet (Actually, JSF MBean is Servlet too).
-
3. Re: EJB3 injection in servlet with JBoss AS 7
oortdg Apr 1, 2013 4:36 AM (in response to ybxiang.china)1 of 1 people found this helpfulHi all,
Just tried it out and on a 7.1 jboss server it works. I created a new maven project in eclipse (jboss tools) with jboss-javaee6-ear-blank-archetype (version 7.1.3.CR3.
add my servlet to the ....-web project
add my ejb to the ....-ejb project
I must added a dependency to the ...web project pom :
<dependency> <groupId>javax.ejb</groupId> <artifactId>ejb-api</artifactId> <version>3.0</version> </dependency> and my servlets and ejb works
servlet code :
package org.jboss.servlet2.servlet;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jboss.servlet2.ejb.CalcEJB;
/**
* Servlet implementation class Calc
*/
@WebServlet("/Calc")
public class Calc extends HttpServlet {
private static final long serialVersionUID = 1L;
@EJB
private CalcEJB ejb;
/**
* @see HttpServlet#HttpServlet()
*/
public Calc() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setStatus(HttpServletResponse.SC_ACCEPTED);
try(BufferedWriter wr = new BufferedWriter(response.getWriter())){
wr.append("<p>sum(1+2 )= " + ejb.sum(1,2)+ " <br/></p>");
}catch(IOException ioEx){
ioEx.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
ejb code:
package org.jboss.servlet2.ejb;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
@Stateless
@LocalBean
public class CalcEJB {
public int sum(int...numbers){
if(numbers == null){
return 0;
}
int result = 0;
for(int number:numbers){
result+=number;
}
return result;
}
}
so what is the difference between my code and yours?
-
4. Re: EJB3 injection in servlet with JBoss AS 7
evgeniy3054 Apr 1, 2013 10:07 PM (in response to oortdg)Hi all! Thx very much for your reply. Your code is working) Sorry, but my question was not full. When i try to use inject ejb in class which extends HttpServlet - it is working good, but when i use struts2 actions is not working.I suppose that strats2 action is not related with HttpServlet. If this true, can we use @EJB ot @Inject?
-
5. Re: EJB3 injection in servlet with JBoss AS 7
nickarls Apr 2, 2013 1:53 AM (in response to evgeniy3054)Struts doesn't have injection so it won't work there. I guess you could still access the CDI BeanManager from JNDI and get to a bean from there.