添加了一个Filter 用于验证是否登陆,和自动登陆(还没写完),,
加了Filter 后可以正常过滤,可以在跳转到登陆页面时,验证码不能显示出来了,,
直接点登陆却可以正常显示..有经验的GGJJ们帮忙看看呀,,,Filter 代码:public class LoginFilter implements Filter {
       public void destroy() {       }       private FilterConfig filterConfig;
       private String loginPage = "/login.jsp";
       
       public void doFilter(ServletRequest req, ServletResponse resp,
                     FilterChain chain) throws IOException, ServletException {              HttpServletRequest request = (HttpServletRequest)req;
              HttpServletResponse response = (HttpServletResponse)resp;
              HttpSession session = request.getSession(true);
              User user = (User)session.getAttribute("user");              //如果封装的user不为空,说明已经登陆,则继续执行用户的请求.下面的就不处理了
              if(user!=null){
                     chain.doFilter(request,response);
                     return;
              }else{//想在这里实现,没有登陆时跳转到登陆页面
               ServletContext ctx = filterConfig.getServletContext();
               ctx.getRequestDispatcher(loginPage).forward(request, response);
              }       }
        public void init(FilterConfig config) throws ServletException {
        filterConfig = config;
        if(filterConfig.getInitParameter("loginPage")!=null){
        loginPage = filterConfig.getInitParameter("loginPage");
        }
       }}
Web.xml 部分代码 <!-- 验证码 -->
  <servlet>
    <servlet-name>Img</servlet-name>
    <servlet-class>servlet.Img</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Img</servlet-name>
    <url-pattern>/servlet/Img</url-pattern>
  </servlet-mapping><!-- 过滤器 -->
  <filter>
   <filter-name>AutoLogonFilter</filter-name>
   <filter-class>filter.LoginFilter</filter-class>
   <init-param>
   <param-name>loginPage</param-name>
   <param-value>/login.jsp</param-value>
   </init-param>
  </filter>
  <filter-mapping>
   <filter-name>AutoLogonFilter</filter-name>
   <url-pattern>/user/*</url-pattern>
  </filter-mapping>登陆页面  代码
<%@ page language="java" pageEncoding="UTF-8"%>
 
<html> 
<head>
<title>JSP for DynaValidatorActionForm form</title>
<script type="text/javascript">
function refreshCode(){
document.getElementById("code").src = "servlet/Img";
}
</script>
</head>
<body>
<form action="login.do" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" id="username" name="username" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" id="password" name="password"/></td>
</tr>
<tr>
<td>验证码:</td>
<td><input type="text" id="checkcode" name="checkcode"/></td>
</tr>
<tr>
<td></td>
<td><img name="code" src="servlet/Img" width="160" height="30" alt="验证码"></td>
</tr>
<tr>
<td></td>
<td><a href="javascript:refreshCode();">重新获取</a></td>
</tr>
<tr>
<td></td>
<td><input name="rember" type="checkbox">
两周内不用再登陆</td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Sumit"/></td>
</tr>
</table>
</form>
</body>
</html>验证码  Servlet 部分代码
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("image/jpeg");

response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);

OutputStream out = response.getOutputStream();
int width = 80;
int height = 20;
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman",Font.ITALIC,18));
String sRand = "";
for (int i = 0; i < 4; i++) {
String  rand = String.valueOf(random.nextInt(10));
sRand+=rand;
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
g.drawString(rand, 20*i+6, 16);
}
g.dispose();
ImageIO.write(image, "jpeg", out);
HttpSession session = request.getSession();
session.setAttribute("code", sRand);
}

解决方案 »

  1.   

    问题已解决
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <base href="<%=basePath%>">
    加上以上代码可以正常运行了,,,
    问一下,这三句起的是什么作用呀,,
    在什么时候发挥的作用呀,,<base href="<%=basePath%>">这句是在设置根目录吗????能不能给讲解一下呀,,,
      

  2.   

    上面代码是加在login.jsp 页面的
      

  3.   

    恩struts1.2 <html:base/>
    作用跟着个差不多
    还有你用window.showModelDialog();
    在模态的对话框你请求,怎么返回到对话框里面,
    也是要用这个标签. 
      

  4.   

    String path = request.getContextPath();
    獲得工程根目錄!
      

  5.   

    问题又回到权限上了,,,如果放问一个页面b.jsp,因为没有登陆,这时过滤器会起作用。跳到login.jsp页面.
    (这里rul为:  http://localhost:8080/Second/user/b.jsp)
    过滤器里跳转用的是  
     
    ServletContext ctx = filterConfig.getServletContext();
     setForwardURI(request);     //把b.jsp的跳径放到request里面.以便登陆后自动跳转(问题也在这)
     ctx.getRequestDispatcher(loginPage).forward(request, response);因为用的是getRequestDispatcher().forward   地址栏不会发生变话
    到了登陆页面,登陆后,,可以到达b.jsp  可这里的地址栏上的url为: http://localhost:8080/Second/login.do
    我现在想在登陆后跳转到b.jsp页面时  url  也会变(如变成http://localhost:8080/Second/user/b.jsp)该怎么办呀,,,有没有谁有好的例子呀,,
    能达到像在  CSND 上,没有登陆下载东西时,同样的效果