O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required