I am trying to not have an EJB injected in my Servlet, currently I am having it directly injected into my Servlet and it works fine but I want to pass control to a controller class that will inject the EJB, and not have a Controller method or constructor that takes a reference variable of the Bean and working with it. Everything I am doing is in JSP, and I try using the @ManagedBean annotation on my Controller but nothing works. I am using only JSPs and not JSF as I am not familiar with them. Is what I am trying to do achievable and is there an alternative to this besides passing reference of the bean ? Any shed of light is highly appreciated. Thank you. Currently what I have is the following in my Servlet.
package com.ejb.web;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
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 com.ejb.customer.domain.Customer;
import com.ejb.customer.service.CustomerLocal;
import com.ejb.customer.util.DateUtil;
/**
* Servlet implementation class Servlet
*/
@WebServlet("/customer")
public class Dispatcher extends HttpServlet {
private static final long serialVersionUID = 1L;
@EJB
CustomerLocal customerLocal;
/**
* @see HttpServlet#HttpServlet()
*/
public Dispatcher() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String surname = request.getParameter("surname");
String dateOfBirth = request.getParameter("dateOfBirth");
Customer customer = new Customer();
customer.setName(name);
customer.setSurname(surname);
customer.setDateOfBirth(DateUtil.convertToDate(dateOfBirth));
System.out.println("Starting .....Adding");
System.out.println(customerLocal);
customerLocal.addCustomer(customer);
System.out.println("Done .....Adding");
request.setAttribute("customers",customerLocal.getCustomers());
RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");
rd.forward(request, response);
}
}
And what I want to achieve is something along these lines.
package com.ejb.web;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
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 com.ejb.customer.domain.Customer;
import com.ejb.customer.service.CustomerLocal;
import com.ejb.customer.util.DateUtil;
/**
* Servlet implementation class Servlet
*/
@WebServlet("/customer")
public class Dispatcher extends HttpServlet {
private static final long serialVersionUID = 1L;
@Inject
Controller controller;
/**
* @see HttpServlet#HttpServlet()
*/
public Dispatcher() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String surname = request.getParameter("surname");
String dateOfBirth = request.getParameter("dateOfBirth");
Customer customer = new Customer();
customer.setName(name);
customer.setSurname(surname);
customer.setDateOfBirth(DateUtil.convertToDate(dateOfBirth));
System.out.println("Starting .....Adding");
System.out.println(customerLocal);
ccontroller.addCustomer(customer);
System.out.println("Done .....Adding");
request.setAttribute("customers",controller.getCustomers());
RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");
rd.forward(request, response);
}
}
Where the controller class does something like the following.
package com.ejb.customer.util;
import java.util.List;
import javax.annotation.ManagedBean;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import com.ejb.customer.domain.Customer;
import com.ejb.customer.service.CustomerLocal;
@ManagedBean
@SessionScoped
public class Controller {
@EJB
CustomerLocal customerLocal;
public void addCustomer(Customer customer) {
customerLocal.addCustomer(customer);
}
public List getCustomers() {
return customerLocal.getCustomers();
}
}
Sorry about the poor indentation