Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

servlet filter

Thread view: 
odelya - 29 Aug 2007 10:10 GMT
Hello,
I wrote the followint doFilter method:
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
        if (filterConfig == null)
            return;
        HttpServletRequest request = (HttpServletRequest) arg0;
        HttpServletResponse response = (HttpServletResponse) arg1;
        String contentType = (String)
request.getAttribute("JPServletContentType");
        if (contentType != null){
            //response.setContentType(contentType);

        }
        String conType =
(String)filterConfig.getServletContext().getAttribute("JPServletContentType");
        if (conType!=null) response.setContentType(conType);
        String expires = (String) request.getAttribute("JPServletExpires");
        if (expires != null)
            response.setHeader("Expires", expires);
        String cacheControl = (String)
request.getAttribute("JPServletCacheControl");
        if (cacheControl != null)
            response.setHeader("Cache-Control", cacheControl);
        response.setContentType("text/plain");
        long startTime = System.currentTimeMillis();
        arg2.doFilter(request, response);
       long stopTime = System.currentTimeMillis();
       System.out.println("Time to execute request: " + (stopTime -
startTime) +
           " milliseconds");

    {
when i started the server, I saw a debug message :
2007-08-29 11:56:25,759 DEBUG [tomcat.localhost./servlet.Context]
Starting filter 'jpServletFilter'

But it doesn't seem to run the code, since I wrote the
System.out.print and id doesn't print to the log!

b
Manish Pandit - 30 Aug 2007 02:41 GMT
> Hello,
> I wrote the followint doFilter method:
[quoted text clipped - 7 lines]
>
> b

Did you put the 'Starting filter..' message in the init() method ? Can
you paste the init() body ? You are supposed to keep the filterConfig
that you get in the init() method. Apparently the line where you check
filterConfig == null is true, and the rest of the method does not
execute as the filter returns. Put a log.info or system.out.println
there to be sure.

-cheers,
Manish
odelya - 30 Aug 2007 08:06 GMT
> > Hello,
> > I wrote the followint doFilter method:
[quoted text clipped - 17 lines]
> -cheers,
> Manish
Hi,
I still have a problem:
I wrote in the jsp file:
<input name='JPServletContentType' type='hidden' value='text/plain'>
and in the filter:
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) {
....some code...
HttpServletRequest request = (HttpServletRequest) arg0;
        HttpServletResponse response = (HttpServletResponse) arg1;
        String contentType = (String)
request.getAttribute("JPServletContentType");
        if (contentType != null) {
            response.setContentType(contentType);
            System.out.println(contentType);
        } else
            System.out.println("noContentType");
but the variable contentType returns null.
so i tried:

String conType = (String)
filterConfig.getServletContext().getAttribute("JPServletContentType");
        if (conType != null)
        {
            response.setContentType(conType);
            System.out.println(conType);

        } else
            System.out.println("noContentType2");

and still the conType returned null.
What's the way to get an attribut and send it?

Thank you!
Manish Pandit - 30 Aug 2007 18:09 GMT
> <input name='JPServletContentType' type='hidden' value='text/plain'>
> and in the filter:
> request.getAttribute("JPServletContentType");

JPServletContentType will come in as a request Parameter, not request
Attribute. That is why you're seeing a null. Why are you looking for
it in ServletContext ? If you feel you are unclear on the scopes
(request/context/session/page), attribute vs. parameters, read the
Sun's JSP/Servlet tutorial which can give you a nice overview.

Try request.getParameter("JPServletContentType") instead of
request.getAttribute(..) and you should see 'text/plain' as you set in
the JSP.

-cheers,
Manish
odelya - 02 Sep 2007 07:24 GMT
> > <input name='JPServletContentType' type='hidden' value='text/plain'>
> > and in the filter:
[quoted text clipped - 12 lines]
> -cheers,
> Manish

Hi,
I did it as you mentioned.
But it doesn't help, it still get the null.
Is there a way to get the whole html text that is sent to the server
and search in it for values?

Thank you!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.