[Starter] EJB Session Bean based Web Server and Servlet client

EJB Session Bean based Web Server and Servlet client in Netbeans

1. Create a Class Library project which will hold your Remote EJB interfaces


2. Create new interface called WsHello

package com.vvirlan.ejb;
import javax.ejb.Remote;
public interface WsHello {
    public String sayHello(String name);

3. Create a new Project of type EJB Module
4. Create a new class in the EJB Module this class will implement your interface defined in the Class Library you defined earlier

package com.vvirlan.ejb;

import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;
public class WsHelloImpl implements WsHello {
    private String msg = "Hello! ";
    public String sayHello(String name) {
        return msg + name;

5. Create a Web Application and a new Servlet in it. Here you add the annotation @EJB to autowire your interface.

package com.vvirlan.servlet;

import com.vvirlan.ejb.HelloServiceBean;
import com.vvirlan.ejb.WsHello;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WsServlet extends HttpServlet {

    private WsHello hello;
     * 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 {
        String result = hello.sayHello("Vlad");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<title>Servlet WsServlet</title>");            
            out.println("<h1>Servlet WsServlet at " + request.getContextPath() + "</h1>");
            out.println("<h1>Servlet WsServlet at " + result + "</h1>");

6. Add the Class Library as a dependency in your web application project. Now all errors should solve by themselves.