File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Using Filters with Struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Using Filters with Struts" Watch "Using Filters with Struts" New topic

Using Filters with Struts

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi there !
I�m having problems on using a simple Filter to check authentication . As I redirect every request to this filter ,apparently the container loses the request content and doesn�t populate the corresponding FormBean after passing through the filter, before going to the Action...
Do I have to configure some special feature ?
Chad McGowan
Ranch Hand

Joined: May 10, 2001
Posts: 265
I've never had this problem. Maybe if you post your code, something may stand out.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Ok Chad !
Follows the code :
WEB.XML (Filter declaration)



<form-bean name="loginForm" type="debut.LoginForm"/>


<forward name="sucesso" path="/success.jsp"/>
<forward name="falha" path="/error.jsp"/>

FILTER code :

package debut;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FiltroLogin implements Filter {

private FilterConfig config = null;

public void init (FilterConfig config) throws ServletException{

this.config = config;


public void destroy(){

config = null;


public void doFilter(ServletRequest request,ServletResponse response,
FilterChain chain) throws
IOException, ServletException {

HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
HttpSession sessao = req.getSession();

String statusLogin = (sessao.getAttribute("statusLogin") == null) ? "" : (String)sessao.getAttribute("statusLogin");


chain.doFilter(req, response);
res.sendRedirect("login.jsp");// CALLS THE LOGIN if it�s not already done...


Simply posts username and password to

public class LoginForm extends ActionForm {
private String user = null;
private String password = null;

/* Set/Get user*/
public void setUser(String user){
this.user = ( user == null ) ? "" : user;
System.out.println("user = "+user);
public String getUser (){
System.out.println("user = "+user);
return user ;

/* Set/Get password*/
public void setPassword(String password){
this.password = ( password == null ) ? "" : password;
System.out.println("password = "+password);
public String getPassword (){
System.out.println("password = "+password);
return password ;
/* Metodo necess�rio para o Struts */
public void reset(ActionMapping mapping,HttpServletRequest request) {

this.user = null;
this.password = null;

public class LoginAction extends Action {

public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

String target = new String("falha");
String user = "";
String password = "";

if ( form != null ) {
// Usa o FormBean para recuperar os parametros do request
LoginForm loginForm = (LoginForm)form;
user = loginForm.getUser();
password = loginForm.getPassword();
System.out.println("user = "+user+" e password = "+password);

if(user.equals("user") && password.equals("password")){

HttpSession sessao = request.getSession();
target = new String("sucesso");

// Forward to the appropriate View
return (mapping.findForward(target));

[ June 23, 2003: Message edited by: fabio gama ]
[ June 23, 2003: Message edited by: fabio gama ]
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I�ve found it myself...
I hadn�t noticed that after some changes, I had left an IF dispatching always to the login.jsp... when not logged... so... always...
Tks anyway !
Best regards,
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
In fact, when it�s already logged on , the request is not lost when passing through the filter, but, if it�s not , when redirected to the login page, the FormBean (all the request object) is lost, once it�s scope has been declared �request� in struts-config.xml... If I declare them always as �session� to avoid loosing the posted data , I�ll have a tremendous memory overhead , so... I�m a little confused on which way to take...
Does anyone have a good way for mixing Filters and Struts for authentication and not having this kind of problem ?

I agree. Here's the link:
subject: Using Filters with Struts
jQuery in Action, 3rd edition