Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Pro JSP
|
Pro JSP Advanced JSP coding questions. Beginning questions will be redirected to the Beginning JSP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro JSP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 30th, 2006, 08:31 AM
Authorized User
 
Join Date: Jul 2003
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to set a session in Java Filter?

I make a filter,in this filter,I check a request parameter,if this parameter equals a assigned name,I put a flag into a session,like follows:
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException,ServletException
 String name=request.getParameter("name");
 .....
 if(name.compareTo("John")==0){
  session.setAttribute("isLogin","true");
 }
 ...
}

But when I run above program,I will get a error,because session is not defined. I know HttpSession is get by HttpServletRequest.getSession(),
but the public void doFilter is only provide ServletRequest,not HttpServletRequest! I want to know whether there is a method which I can get HttpSession in Filter?

Any idea will be appreciated!

 
Old January 31st, 2006, 05:34 AM
Authorized User
 
Join Date: Jan 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to longjava
Default

What you need to do is casting it to HttpServletRequest as following:

String name = request.getParameter( "name" );
if ( name.compareTo( "John" ) )
{
   if ( request instanceof HttpServletRequest )
   {
      httpRequest = (HttpServletRequest) request; // doing casting here
      httpRequest.setAttribute( "isLogin", "true" );
   }
}







Similar Threads
Thread Thread Starter Forum Replies Last Post
Set Session to FormView Label sterreyl ASP.NET 2.0 Basics 0 March 29th, 2007 11:16 AM
how to set session.save_path on WS 2003 ? cristi Pro PHP 2 April 2nd, 2005 04:45 PM
Re: need a correct method to set filter flyfish Access 2 January 28th, 2005 06:32 AM
set maximum session time through program vinodkalpaka Beginning PHP 1 December 17th, 2004 10:44 AM
How to set up Java AppServer partho_choudhury J2EE 7 June 14th, 2004 02:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.