使用servlet过滤器后相对地址出问题了
为了系统安全性考虑。没登陆用户不能请求除登陆界面和处理登陆的Action之外的界面。
现在功能实现了。但是登陆解面的图片,样式了都不能用了。有人碰到过这种问题吗?
代码用的http://dev.yesky.com/329/2015829.shtml这个页面陈雄华的代码。修改成自己的代码是这样的。
public void doFilter(ServletRequest request, ServletResponse response
8. , FilterChain filterChain)
9. {
10. try
11. {
12. //进行请求和响应的类型转换
13. HttpServletRequest httpRequest = (HttpServletRequest) request;
14. HttpServletResponse httpResponse = (HttpServletResponse) response;
15.
16. boolean isValid = true;
17. String uriStr = httpRequest.getRequestURI().toUpperCase();
18. if (uriStr.indexOf( "INDEX.JSP ") == -1 &&
19. uriStr.indexOf( "INDEX.DO ") == -1 &&
20. httpRequest.getSession().getAttribute( "userid ") == null)
21. {
22. isValid = false;
23. }
24. if (isValid)
25. {
26. filterChain.doFilter(request, response);
27. } else
28. {
29. httpResponse.sendRedirect( "/webModule/index.jsp ");
30. }
31.
32. } catch (
ServletException sx)
33. {
34. filterConfig.getServletContext().log(sx.getMessage());
35. } catch (
IOException iox)
36. {
37. filterConfig.getServletContext().log(iox.getMessage());
38. }
代码清单 过滤器的部署描述配置信息
1. <web-app>
2. <display-name>webModule</display-name>
3. <filter>
4. <filter-name>logincheckfilter</filter-name>
5. <filter-class>bookstore.servlet.LoginCheckFilter</filter-class>
6. </filter>
7. <filter-mapping>
8. <filter-name>logincheckfilter</filter-name>
9. <url-pattern>/*</url-pattern>
10. </filter-mapping>
11. …
12. </web-app>
还添一点问题描述。功能可用。登陆后再回到主页就一切问题都没了。就首次登的时候有问题。
我添了 <html:base/>
或者 <%path=...%> 那一长串代码
<base herf= <%=path%> > 也无效
------解决方案--------------------package com.zlt.filter;
import
java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import
javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)arg0;
Ht