Enterprise application in Eclipse with Hibernate, ejb, servlet and jsp
eriakort Feb 1, 2012 12:32 PMHi,
I would really love it if I could be given guidance to finish this project of mine. I have to create an application with EJB, servlet, jsp, and Hibernate for my database mapping. I cannot say I am very good with such an application development but I hope to understand any advice given me. Currently I am using eclipse indigo and Jboss 7 as my server and MySQL for my database. I have tried by trying to make a simple login with all the components above and if it works I continue from there. I am using he MVC development procedure. In that I have 3 different components. First the project name is RegistrationEAR and I have RegistrationEJB and RegistrationWAR components under this project. In all I have 3 different folders/directories. I have my jsp pages and my servlet under RegistrationWAR...I have my entity and session beans in my RegistrationEJB. My jsp has no problem communicating with my servlet but the problem is my servlet communicating with my EJB components in RegistrationEJB. Below is the tree structure of my project:
RegistrationEJB
RegistrationWAR
Following are my codes:
ControllerServlet.java
package myejbclasses; import java.io.IOException; import java.util.Properties; //import java.util.Properties; //import javax.ejb.*; import javax.naming.*; //import javax.rmi.*; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ControllerServlet */ @WebServlet("/ControllerServlet") public class ControllerServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ControllerServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ //@EJB //MyRemoteBusInterface remotebusinterface; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub // Step 4: Use JNDI to look up the EJB local home interface. try { Properties properties = new Properties(); properties.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); properties.put("java.naming.factory.url.pkgs","org.jboss.naming rg.jnp.interfaces"); properties.put("java.naming.provider.url", "jnp://localhost:8080"); Context ctx = new InitialContext(properties); System.out.println("Got context"); MyRemoteBusInterface ans=(MyRemoteBusInterface) ctx.lookup("RegistrationEAR/MyStatelessSessBean/remote"); String name = request.getParameter("name"); if (name == null || name.length() == 0) { name = "anonymous"; } response.getWriter().write(ans.sayHello(name)); } catch (NamingException ne) { System.out.println("Could not locate the bean."); } catch (Exception e) { // Unexpected exception; send back to client for now. throw new ServletException(e); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }
MyStatelessSessBean.java
package myejbclasses; import javax.ejb.Stateless; @Stateless public class MyStatelessSessBean implements MyRemoteBusInterface { @Override public String sayHello(String name) { // TODO Auto-generated method stub //return getClass().getName() + " says hello to " + name + "."; return " says hello to you"; } }
MyRemoteBusInterface.java
/** * */ package myejbclasses; import javax.ejb.Remote; /** * @author shengbelen * */ @Remote public interface MyRemoteBusInterface { public String sayHello(String name); }
I guess this is all I need to provide for you analysis...If anything else is needed please let me know. Hope to hear something soon...Thank you in advance