Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6771483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:26:15+00:00 2026-05-26T15:26:15+00:00

To enable Servlet, JSP, JSF etc to use a remote EJB , performing resource

  • 0

To enable Servlet, JSP, JSF etc to use a remote EJB, performing resource injection is essential. JSP however takes a different way to inject an EJB into it than that taken by the rest. let’s see all of them by example.


In JSF, a remote EJB can be injected in a very simple way as can be seen in the following ManagedBean. An EJB may a stateful, a stateless or a singleton.

Suppose the interface which is implemented by the session bean is namely CustomerBeanRemote

@ManagedBean
@RequestScoped

public class AddSubscription
{
    @EJB
    private CustomerBeanRemote obj=null;

    //Getters and setters.
}

Here the remoter EJB can be injected only with one annotation @EJB just before the interface declaration. Since EJB is injected here, the interface is able to call the methods in the session bean even if it is explicitly assigned null. null here makes no sense at all.


Servlet takes the same view as shown below.

public class Add extends HttpServlet {
{
    @EJB 
    private CustomerBeanRemote obj=null;

    //Rest of the elegant Servlet code goes here.
}

JSP (and JSTL/EL) however takes a different view as mentioned below.

try
{
    obj=(CustomerBeanRemote )new   
    InitialContext().lookup(CustomerBeanRemote.class.getName());
}
catch(NamingException e)
{
    out.println(e.getMessage());
}

obj.someEJBMethod();

or


try
{
    obj=(CustomerBeanRemote )new   
    InitialContext().lookup("Specific JNDI");
}
catch(NamingException e)
{
    out.println(e.getMessage());
}

obj.someEJBMethod();

Why JSP (or JSTL/EL) requires a JNDI (Java Naming and Directory Interface) to perform a resource injection while in Servlet and JSF it can simply be performed with only one annotation which is @EJB?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:26:16+00:00Added an answer on May 26, 2026 at 3:26 pm

    A JSP is a servlet, so you should certainly be able to use the annotation in the JSP:

    <%! @EJB private CustomerBeanRemote obj=null; %>
    

    (untested though).

    But even if it doesn’t work, that’s not a problem because JSPs should not contain Java code anyway. JSPs should be used to generate markup from beans prepared by a controller (Servlet or action of your preferred framework) written in Java.

    See How to avoid Java code in JSP files?

    Also, your snippets of code are wrong: they use the obj variable although a naming exception occurred when initializing them. This will obviously lead to a NullPointerException.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way to enable my webapplication to use JSF files stored
I'm developing a simple servlet/JSP, data-driven web site on Google App Engine. I've started
I've read: Expression Language in JSP not working but it's slightly different. Using EL
I am developing a java webapp, using jsp/jquery/ejb/jboss. I have a web-form that enables
i want to use EL 2.2 for JSF 2 application and tomcat 7 is
How can I enable EL expression in JSP version 2.0? Every time I'm getting
I have this servlet which needs to send mail using Java Mail API, however
To enable single sign-on for a web application and a web application in a
I want to enable an user to be able to communicate with other users
I am trying to enable Full-text indexing in SQL Server 2005 Express. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.