Ok Chad !
Follows the code :
WEB.XML (Filter declaration)
<filter>
<filter-name>FiltroLogin</filter-name>
<display-name>FiltroLogin</display-name>
<description></description>
<filter-class>debut.FiltroLogin</filter-class>
</filter>
<filter-mapping>
<filter-name>FiltroLogin</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
STRUTS-CONFIG.XML
<form-bean name="loginForm" type="debut.LoginForm"/>
<action
path="/Login"
type="debut.LoginAction"
name="loginForm"
scope="session"
validate="false"
input="/login.jsp">
<forward name="sucesso" path="/success.jsp"/>
<forward name="falha" path="/error.jsp"/>
</action>
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;
import java.io.IOException;
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");
if(statusLogin.equals("LOGGED")){
chain.doFilter(req, response);
}else{
sessao.setAttribute("URL",req.getRequestURL().toString());
res.sendRedirect("login.jsp");// CALLS THE LOGIN if it�s not already done...
}
}
}
LOGIN.JSP:
Simply posts username and password to Login.do
FORMBEAN
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;
}
}
LOGIN ACTION :
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();
sessao.setAttribute("statusLogin","LOGGED");
target = new String("sucesso");
}
// Forward to the appropriate View
return (mapping.findForward(target));
}
}
Thanks
F�bio
[ June 23, 2003: Message edited by: fabio gama ]
[ June 23, 2003: Message edited by: fabio gama ]