论坛首页 入门技术论坛

关于如何在fitler中使用转发到.do的问题

浏览 3713 次
该帖已经被评为新手帖
作者 正文
   发表时间:2007-11-21  
现在需要使用filter实现一个网站登陆时”记住用户名“的功能。步骤如下:
1.action中保存用户第一次登陆时正确的登陆名与密码到cookie.
2.filter实现功能。如果filter能从请求中取到对应的cookie,则请求/login/loginLoginAction.do,否则到登陆页,接着登陆。

在web.xml中配置针对/index.jsp进行filter:
<filter>
<filter-name>CheckCookieFilter</filter-name>
<filter-class>
com.tongcard.merchant.web.filter.CheckCookieFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>CheckCookieFilter</filter-name>
<url-pattern>/index.jsp</url-pattern>
</filter-mapping>

filter如下:
package com.tongcard.merchant.web.filter;

import java.io.IOException;
import javax.servlet.FilterConfig;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class CheckCookieFilter implements Filter {
FilterConfig filterConfig;
public void destroy() {
// TODO Auto-generated method stub
}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse res = ((HttpServletResponse) response);
HttpServletRequest req = ((HttpServletRequest) request);
Cookie[] cookies = req.getCookies();
String path=req.getRequestURI();
if (cookies != null) {
String loginName = "";
String loginPasswd = "";
for (Cookie cookie : cookies) {
if (cookie.getName().equalsIgnoreCase("loginName")) {
loginName = cookie.getValue();
}
if (cookie.getName().equalsIgnoreCase("loginPasswd")) {
loginPasswd = cookie.getValue();
}
}
if (loginName != "" && loginPasswd != "") {
req.setAttribute("loginUser.loginName", loginName);
req.setAttribute("loginUser.loginPasswd", loginPasswd);
//ServletContext context = filterConfig.getServletContext();
req.getRequestDispatcher("/login/loginLogin.do").forward(req,res);
//context.getRequestDispatcher("/login/loginLogin.do").forward(req,res);
} else {
chain.doFilter(req, res);
}
} else {
chain.doFilter(req, res);
}
}

public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}

}
url中login代表方法,Login代表action.
为什么当我从cookie中得到上一次保存到用户本地的登陆名与密码后,然后想执行/login/loginLogin.do时总是提示HTTP Status 404 - /mmp/login/loginLogin.do
forward难道只能到jsp页面?为了这个问题试了一下午。郁闷。
   发表时间:2007-11-22  
404  一般都是路径不对,你可以看看路径.
0 请登录后投票
   发表时间:2007-11-22  
你可以试一试。我将地址http://localhost/mmp/login/loginLogin.docopy到地址栏里可以执行,说明路径是没有问题的。
0 请登录后投票
   发表时间:2007-11-22  
自己顶一下,哪一位朋友遇到过这样的问题。forward到jsp是没有问题的。就是到.do就提示找不到。怪怪怪
0 请登录后投票
   发表时间:2008-04-04  
这个问题我遇到过,返回的时候在req.getRequestDispatcher("list").forward(req,res);
然后在Struts的配置文件里面将
<forward name="list"  path="/login/loginLogin.do" redirect="true"/>
就行了!!!
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics