java left logo
java middle logo
java right logo
 

Home arrow Java EE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4100
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Accessing EJB from a servlet within the same container E-mail
User Rating: / 27
PoorBest 

Since servlets represent a front-end technology you may want to contact an EJB from a servlet. With the recent push to use web applications for enterprise solutions, servlets have started to perform important roles in business applications. While servlets control the flow and validation of page presentation, they also are the main access point to the back-end business logic contained in EJBs.

In our sample we are to create a login servlet with uses EJB behind to perform authentification. When contacting an EJB in the same container (the same virtual machine), you need only make use of a default instance of the InitialContext class. Our servlet contacts a Login EJB in order to process a user login.

...
public class LoginServlet extends HttpServlet {
  // home interface for the EJB Login:
  private LoginHome _loginHome = null;
  
  public void init(ServletConfig confthrows ServletException {
    super.init(conf);
    
    try {
      // create initial context:
      Context ctx = new InitialContext();
      // find Login home interface:
      _loginHome = (LoginHomectx.lookup("test.loginHome");
      
    catch (NamingException e) {
      e.printStackTrace();
    }
  }

  public void doGet(HttpServletRequest request,
    HttpServletResponse response) {

    try {
      // get login parameters:
      String user = request.getParameter("user");
      String password = request.getParameter("password");

      // create EJB instance for the user requested:
      Login login = _loginHome.create(name);
      
      // perform login operation:
      Boolean valid = login.login(password);

      //perform further processing...
      ...
      
    catch (Exception e) {
      //handle exception
    }
  }
}


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.