Using @Resource to access JNDI in WebSphere AS 8.5 Liberty Profile

Z Jacek Laskowski - Wiki Amatora Programowania
Skocz do: nawigacji, wyszukiwania

In Using JNDI binding for constants from the server configuration files you can read about defining JNDI bindings for constants in WebSphere Application Server 8.5 Liberty Profile (aka WebSphere Liberty Profile, Liberty Profile or simply Liberty). I was surprised to have read about the feature without a code with the @Resource annotation. Here is a sample to complement the document.

It's a document to get you started with the Liberty Profile and JNDI with @Resource annotation.

Download the server from Download: WebSphere Application Server Liberty Profile. Choose the method that suits your needs best - with Eclipse or a standalone version. People who use other IDEs may want to follow the second route.

Create a web application project with the following servlet.

package liberty;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ResourceServlet extends HttpServlet {
     private static final long serialVersionUID = 1L;
     @Resource(lookup = "schoolOfAthens/defaultAdminUserName")
     String defaultAdminUserName;
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                    "<body><h2>defaultAdminUserName resource's value is %s</h2></body>",

Note the @Resource annotation to get at the schoolOfAthens/defaultAdminUserName jndiEntry in server.xml - the configuration file for Liberty Profile. Speaking of server.xml, it looks as follows:

<server description="new server">
  <jndiEntry jndiName="schoolOfAthens/defaultAdminUserName" value="plato"/>

Run the servlet and notice that the value of the jndiEntry is displayed in the browser.


It works! Questions? Suggestions? Ideas? You're welcome to send me an email to Thanks!

Przestrzenie nazw