Sie sind auf Seite 1von 6

EJB in Netbeans Following is the procedure to create a simple EJB bean in netbeans Open the netbeans IDE Select

file->new project->under categories select javaEE->select Enterprise applications Specify the name of the project as Test and click next Select both create ejb module and ejb web application module click finish Right click the ejb module select new->session bean Specify the ejb name and package name Eg ejb name TestEJB

Select the session type and create interface For now select stateless as session type and remote as the interface. cick Finish. You get two files such as TestEJB.java and TestEJBRemote.java

Open the file TestEJB.java and right click inside the class TestEJB in the popup menu select insert code and select Add business method

Specify the business method in the name field Eg Method name getMessage Return type String No parameters need Specify the return type if you need

If you want to add parameters to the method specify the parameter name and select the data type. Click ok Right click the web application module Open index.jsp under web-pages in web-inf Add the following code <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h2>Hello World!</h2> <a href="TestServlet">Click here to call the EJB component</a> </body> </html>

Right click source packages of the web application module Select new -> servlet Speicfy the class name as TestServlet and package name as servlets Open the file TestServlet.java Add the following the code package servlets; import java.io.IOException; import java.io.PrintWriter; 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 stateless.TestEJBRemote; /** * * @author RAM */ @WebServlet(name="TestServlet", urlPatterns={"/TestServlet"}) public class TestServlet extends HttpServlet { @EJB private TestEJBRemote testEJB; /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { /* TODO output your page here out.println("<html>"); out.println("<head>"); out.println("<title>Servlet TestServlet</title>");

out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet TestServlet at " + request.getContextPath () + "</h1>"); out.println("</body>"); out.println("</html>"); */ } finally { out.close(); } } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET</code> method. * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet TestServlet</title>"); out.println("</head>"); out.println("<body>"); out.println(testEJB.getString()); out.println("</body>"); out.println("</html>"); processRequest(request, response); } /** * Handles the HTTP <code>POST</code> method. * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold> } Note the line private TestEJBRemote testEJB; which creates the instance of the remote interface of the TestEJB Now save the project and right click the Test enterprise application (blue color triangle symbol) select Run Enjoy the Output

Das könnte Ihnen auch gefallen