日期:2014-05-19  浏览次数:20726 次

关于Servlet页面跳转的问题,急求解决!!!
通过res.sendRedirect可以传递参数却不能实现页面跳转,请帮忙看看怎么回事哈???
那就那个经典的登陆系统的例子出问题啦。
登陆界面:
Java code

import javax.servlet.http.*;
import java.io.*;

public class Login extends HttpServlet{

   //处理get请求
   public void doGet(HttpServletRequest req,HttpServletResponse res)
   {
            try{
            
                res.setContentType("text/html;charset=utf-8");
             PrintWriter pw=res.getWriter();
            //res.setContentType("text/html;charset=UTF-8");
            //res.setContentType("text/html;charset=gbk");
            pw.println("<html>");
            pw.println("<body>");
            pw.println("<hl>Login</hl><br>");
            pw.println("<form action=loginCK method=post>");
            pw.println("User:<input type=text name=username> <br>");
            pw.println("Passwd:<input type=password name=password> <br>");
            pw.println("<input type=submit value=Login> <br>");
            pw.println("</form>");
            pw.println("</body>");
            pw.println("</html>");
            
        }catch(Exception ex){
            ex.printStackTrace();
        }
   }
   public void doPost(HttpServletRequest req,HttpServletResponse res)
   {
       this.doGet(req,res);
   }
    
}

登陆验证:
Java code

import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class LoginCK extends HttpServlet{

   //处理get请求
   public void doGet(HttpServletRequest req,HttpServletResponse res)
   {
           Connection ct=null;
           PreparedStatement ps=null;
           ResultSet rs=null;
        
            try{
            
                //res.setContentType("text/html;charset=gbk");
             //PrintWriter pw=res.getWriter();
             //接收用户名和密码
             String u=req.getParameter("username");
            String p=req.getParameter("password");
            //验证
            System.out.println(u);  
            System.out.println(p);

            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            ct=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Students","sa","lissdy033!");
 ps=ct.prepareStatement("select top 1 * from [Students].[dbo].[User] where name='"+u+"'and passwd='"+p+"'");  
        rs=ps.executeQuery();  
           if(rs.next())  
            {  System.out.println("Wel");
               res.sendRedirect("Wel?user="+u+"&pass="+p);  
            }else  
             {  System.out.println("login");
               res.sendRedirect("login");//写需要跳转的servlet的那个url   
              }  

             if(rs!=null)
             {
                     rs.close();
             }
             if(ps!=null)
             {
                 ps.close();
             }
             if(ct!=null)
             {
                 ct.close();
             }
                
        
        }catch(Exception ex){
            ex.printStackTrace();
        }
    
        }
        
   public void doPost(HttpServletRequest req,HttpServletResponse res)
   {
       this.doGet(req,res);
   }
    
}


欢迎界面:
Java code

import javax.servlet.http.*;
import java.io.*;

public class Wel extends HttpServlet{

   //处理get请求
   public void doGet(Http