[ Team LiB ] Previous Section Next Section

Recipe 25.7 Accessing the WebLogic JNDI Resource from a JSP

Problem

You want to use a WebLogic JNDI object in a JSP.

Solution

Create a filter that accesses the JNDI object and sets the object as a session attribute.

Discussion

Any sense of dejá vu comes from a few recipes ago, when you used a filter to pass a JNDI object to a JSP on Tomcat. The only difference in this recipe is that the application server used is WebLogic and the JNDI object is a JavaMail Session, not a JavaBean.

The filter accesses the object using the JNDI API on WebLogic. Then the filter sets the object as a session attribute, so that the JSP can access the javax.mail.Session. Example 25-10 shows the code for the filter that recipe uses on the WebLogic server.

Example 25-10. A filter stores a WebLogic JNDI object in a session attribute
package com.jspservletcookbook;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import javax.servlet.*;
import javax.servlet.http.*;

public class JndiFilter implements Filter {
    
    private FilterConfig config;
    private Context env;
    
  public JndiFilter( ) {}
    
  public void  init(FilterConfig filterConfig)  throws ServletException {
     this.config = filterConfig;
     try {
         env = (Context) new InitialContext( );
      } catch (NamingException ne) { 
          throw new ServletException(ne);
     }
  }//init
    
  public void  doFilter(ServletRequest request, ServletResponse response, 
    FilterChain chain) throws IOException, ServletException {
      
      javax.mail.Session mailSession = null;
       try {
           mailSession = (javax.mail.Session) env.lookup("MyEmail");
       } catch (NamingException ne) { }
        
       HttpServletRequest hRequest = null;
       if (request instanceof HttpServletRequest){
          hRequest = (HttpServletRequest) request;
          HttpSession hSession = hRequest.getSession( );
          if (hSession != null)
              hSession.setAttribute("MyEmail",mailSession);

       }//if
       chain.doFilter(request,response);

  }// doFilter
    
  public void destroy( ){
        /*called before the Filter instance is removed 
        from service by the web container*/
  }
}

Example 25-11 shows the filter configuration inside the deployment descriptor. This deployment descriptor must accompany a web application that you or another deployer has installed on WebLogic server.

Example 25-11. A filter that accesses a JNDI object on Weblogic
<!-- start of web.xml -->

<filter>

    <filter-name>JndiFilter</filter-name>
    <filter-class>com.jspservletcookbook.JndiFilter</filter-class>

</filter>

<filter-mapping>

    <filter-name>JndiFilter</filter-name>
    <url-pattern>/jndiJsp.jsp</url-pattern>

</filter-mapping>

<!-- rest of web.xml -->

Example 25-12 shows a JSP that accesses the JNDI object. This code displays the class name of the object, a javax.mail.Session type that Recipe 25.4 bound as a JNDI object on WebLogic. The filter in Example 25-11 then set the object as a session attribute (not to be confused with the Session type of the object). This attribute is available to all web components that participate in the same session. Therefore, the c:set tag in this JSP uses the following EL code to get access to the attribute.

${MyEmail}

Then the c:out tag displays the class name of the session attribute, in order to verify that the object is a javax.mail.Session. Recipe 25.6 gives the complete JavaMail code for sending an email.

Example 25-12. The JSP accesses the JavaMail object as a session attribute
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<html>
<head><title>Jndi Email</title></head>
<body>
<h2>Getting a javax.mail.Session object via JNDI...</h2>

<c:set var="mSession" value="${MyEmail}" />

<c:out value="${mSession.class.name}" />

</body>
</html>

Figure 25-7 shows a web browser window after a user has requested the JSP.

Figure 25-7. A JSP accesses a JNDI object via a servlet filter
figs/jsjc_2507.gif

See Also

Chapter 19 on filters; Chapter 23 on the JSTL; Recipe 25.4 on configuring a JNDI object with WebLogic; Recipe 25.6 on accessing a JNDI object with a servlet on WebLogic; Chapter 2 on deploying web components with WebLogic.

    [ Team LiB ] Previous Section Next Section