You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Günter Kukies <gu...@heuft.com> on 2003/02/27 13:05:57 UTC

Filter - ServletContext

Hello,

I want to read some context-param from web.xml within a filter. But the getAttribute is always null. 
There is no problem to get the same context-param within a Servlet.


Thanks

Günter 

Here is the code snippet:


import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
import javax.servlet.*;
import javax.servlet.http.*;

import java.sql.*;
import javax.sql.*;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import javax.naming.*;

public class MyFilter implements Filter {
    
    // The filter configuration object we are associated with.  If
    // this value is null, this filter instance is not currently
    // configured.
    private FilterConfig filterConfig = null;
    String category_server_ip = "";
    int category_server_port = 8080;
    String category_root = "";
    String category_base = "";
    /**
     * Init method for this filter
     *
     */
    public void init(FilterConfig filterConfig) {
        
        this.filterConfig = filterConfig;
        ServletContext sctx = filterConfig.getServletContext();
        sctx.getServletContextName();
        category_server_ip = (String)sctx.getAttribute("category_server_ip");
        category_server_port = stringToInt((String)sctx.getAttribute("category_server_port"));
        category_root = (String)sctx.getAttribute("category_root");
        category_base = (String)sctx.getAttribute("category_base");
        if (filterConfig != null) {
            if (debug) {
                log("MyFilter :Initializing filter");
            }
        }
    }
    
    public MyFilter () {
        
    }
    .....

Re: Filter - ServletContext

Posted by Erik Price <ep...@ptc.com>.

Günter Kukies wrote:
> Oh, sorry getInitParameter() was the solution. Thanks for your hint.
> But why is the ServletContext not the same in Filter and HTTPServlet?

I am confused.  There is one ServletContext in a webapp (as far as I 
know, which isn't very far).  You can access it from a Filter using 
getFilterConfig().getServletContext(), and you can access it from a 
HttpServlet using getServletContext().  It is the same.



Erik


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: Filter - ServletContext

Posted by Günter Kukies <gu...@heuft.com>.
Oh, sorry getInitParameter() was the solution. Thanks for your hint.
But why is the ServletContext not the same in Filter and HTTPServlet?

Günter

----- Original Message -----
From: "Erik Price" <ep...@ptc.com>
To: "Tomcat Users List" <to...@jakarta.apache.org>
Sent: Thursday, February 27, 2003 4:54 PM
Subject: Re: Filter - ServletContext


>
>
> Günter Kukies wrote:
> > Hello,
> >
> > I want to read some context-param from web.xml within a filter. But the
getAttribute is always null.
> > There is no problem to get the same context-param within a Servlet.
>
>
> Don't you want getInitParameter() ?
>
>
>
>
> Erik
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


HTTP Status 500 Error

Posted by "Ravindra K. Bhat" <bh...@BPA.Arizona.EDU>.
Hello

how do I replace HTTP Status 500 error page with my own page?

Thanks
Ravi





---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: Filter - ServletContext

Posted by Erik Price <ep...@ptc.com>.

Günter Kukies wrote:
> Hello,
> 
> I want to read some context-param from web.xml within a filter. But the getAttribute is always null. 
> There is no problem to get the same context-param within a Servlet.


Don't you want getInitParameter() ?




Erik


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: Filter - ServletContext

Posted by Jon Wingfield <jo...@mkodo.com>.
Try:

sctx.getInitParameter(...) instead of sctx.getAttribute(...)


Günter Kukies wrote:

>Hello,
>
>I want to read some context-param from web.xml within a filter. But the getAttribute is always null. 
>There is no problem to get the same context-param within a Servlet.
>
>
>Thanks
>
>Günter 
>
>Here is the code snippet:
>
>
>import java.io.*;
>import java.net.*;
>import java.util.*;
>import java.text.*;
>import javax.servlet.*;
>import javax.servlet.http.*;
>
>import java.sql.*;
>import javax.sql.*;
>
>import javax.servlet.Filter;
>import javax.servlet.FilterChain;
>import javax.servlet.FilterConfig;
>import javax.servlet.ServletContext;
>import javax.servlet.ServletException;
>import javax.servlet.ServletRequest;
>import javax.servlet.ServletResponse;
>
>import javax.naming.*;
>
>public class MyFilter implements Filter {
>    
>    // The filter configuration object we are associated with.  If
>    // this value is null, this filter instance is not currently
>    // configured.
>    private FilterConfig filterConfig = null;
>    String category_server_ip = "";
>    int category_server_port = 8080;
>    String category_root = "";
>    String category_base = "";
>    /**
>     * Init method for this filter
>     *
>     */
>    public void init(FilterConfig filterConfig) {
>        
>        this.filterConfig = filterConfig;
>        ServletContext sctx = filterConfig.getServletContext();
>        sctx.getServletContextName();
>        category_server_ip = (String)sctx.getAttribute("category_server_ip");
>        category_server_port = stringToInt((String)sctx.getAttribute("category_server_port"));
>        category_root = (String)sctx.getAttribute("category_root");
>        category_base = (String)sctx.getAttribute("category_base");
>        if (filterConfig != null) {
>            if (debug) {
>                log("MyFilter :Initializing filter");
>            }
>        }
>    }
>    
>    public MyFilter () {
>        
>    }
>    .....
>
>  
>




---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org