The use of Jndi in the Java EE environment is very simple, because all of the Java-EE containers to implement Jndi service, so, in the Java EE environment using Jndi, and the use of Hashtable is not much different. There is only one limit, that is, when the object is bound, The class that the object belongs to must implement the Java.io.Serializable interface, which is not difficult at all, almost all Java classes that are used implement this interface, and for the custom class, add the interface to the interface implementation list.
Below, I will demonstrate how to use Jndi in the Java environment, in order to ensure the versatility of the code, I do not use a framework such as struts, but the direct use of standard JSP and servlet implementation. I set the name of the project as Jndi_test
To use Jndi, you need to go to the Sun's website to download Jndi.jar.
2.1 JSP
This project includes 5 JSP, the function description is as follows:
Index.jsp: Home
BIND.JSP: Used to bind objects in Jndi
BIND_RESULT.JSP: The return page after the object is bound
LOOKUP.JSP: Used to retrieve objects in Jndi
LOOKUP_RESULT.JSP: Used to display retrieved objects
The JSP code used in this section is as follows, and the code is simple enough to explain it.
2.1.1 index.jsp
<%...@ page language= "java" contenttype= "text/html; charset=gb18030 "
pageencoding= "GB18030"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb18030 ">
<title>jndi test</title>
<body>
<a href= "bind.jsp" target= "_blank" >bind.jsp</a>
<br/>
<a href= "lookup.jsp" target= "_blank" >lookup.jsp</a>
</body>
2.1.2 Bind.jsp
<%...@ page language= "java" contenttype= "text/html; charset=gb18030 "
pageencoding= "GB18030"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb18030 ">
<title>jndi test-bind</title>
<body>
<a href= "Bind.do" >bind an object</a>
</body>
2.1.3 bind_result.jsp
<%...@ page language= "java" contenttype= "text/html; charset=gb18030 "
pageencoding= "GB18030"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb18030 ">
<title>jndi Test-bind result</title>
<body>
<p>binded successfully!</p>
</body>
2.1.4 lookup.jsp
<%...@ page language= "java" contenttype= "text/html; charset=gb18030 "
pageencoding= "GB18030"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb18030 ">
<title>jndi test-lookup</title>
<body>
<a href= "Lookup.do" >lookup the binded object</a>
</body>
2.1.5 lookup_result.jsp
<%...@ page language= "java" contenttype= "text/html; charset=gb18030 "
pageencoding= "GB18030"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb18030 ">
<title>jndi Test-lookup result</title>
<body>
<% ...
Object o = Request.getattribute ("Found_jndi_obj");
Out.println (o);
%>
</body>
2.2 Servlet
This example includes two servlet features that are described below:
Bindservlet: Used to bind an object in the Jndi service
Lookupservlet: Used to take an object out of the Jndi service
2.2.1 Bindservlet.java
Package Lld.test.jndi;
Import java.io.IOException;
Import Java.util.Date;
Import Javax.naming.Context;
Import Javax.naming.InitialContext;
Import Javax.servlet.RequestDispatcher;
Import Javax.servlet.ServletContext;
Import javax.servlet.ServletException;
Import javax.servlet.http.*;
public class Bindservlet extends HttpServlet
... {
Private static final long serialversionuid = 5219969790998794367L;
@Override
protected void Doget (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException
... {
This.dopost (req, resp);
}
@Override
protected void DoPost (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException
... {
Try
... {
Context Jndi_ctx = new InitialContext ();
String key = "Jndi_object";
Jndi_ctx.rebind (Key, New Date ());
}catch (Exception ex)
... {
Ex.printstacktrace ();
}
ServletContext context = This.getservletcontext ();
RequestDispatcher dispatcher = Context.getrequestdispatcher ("/bind_result.jsp");
Dispatcher.forward (req, resp);
}
}
Binding an object with rebind instead of bind because, when you use BIND, an exception is thrown if an object already is bound to that key value.
Because it's just the sample code, I just tied a simple date object.
2.2.2 Lookupservlet.java
Package Lld.test.jndi;
Import java.io.IOException;
Import Javax.naming.Context;
Import Javax.naming.InitialContext;
Import Javax.servlet.RequestDispatcher;
Import Javax.servlet.ServletContext;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class Lookupservlet extends HttpServlet
... {
Private static final long serialversionuid = 6677219828267184673L;
@Override
protected void Doget (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException
... {
This.dopost (req, resp);
}
@Override
protected void DoPost (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException
... {
Try
... {
Context Jndi_ctx = new InitialContext ();
String key = "Jndi_object";
Object o = jndi_ctx.lookup (key);
Req.setattribute ("Found_jndi_obj", O);
}catch (Exception ex)
... {
Ex.printstacktrace ();
}
ServletContext context = This.getservletcontext ();
RequestDispatcher dispatcher = Context.getrequestdispatcher ("/lookup_result.jsp");
Dispatcher.forward (req, resp);
}
}
2.3 Web.xml
In Web.xml, a servlet map was added
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app id= "webapp_id" version= "2.4" xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE" xmlns:xsi= /xmlschema-instance "xsi:schemalocation=" Http://java.sun.com/xml/ns/j2ee Http://java.sun.com/xml/ns/j2ee/web-app _2_4.xsd ">
<display-name>jndi_test</display-name>
<servlet>
<servlet-name>BindServlet</servlet-name>
<servlet-class>lld.test.jndi.BindServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BindServlet</servlet-name>
<url-pattern>/bind.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LookupServlet</servlet-name>
<servlet-class>lld.test.jndi.LookupServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LookupServlet</servlet-name>
<url-pattern>/lookup.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
OK, all the code is here, deploy to Tomcat and run.