continuelt
2011-04-06 23:02
浏览 330
已采纳

请问这个简单的登录界面为什么跳转老错,就是登录账户和密码都对的还跳转不到指定页面?

代码:
/////////////////////////////////////////
LoginServlet.java

package login;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginServlet extends HttpServlet {
public void init() throws ServletException {
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

// response.setContentType("text/html");
doPost(request,response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("text/html");
    //从请求中取出用户名和密码的值
     String username = request.getParameter("username");
     String password = request.getParameter("password");

    //生成一个ArrayList对象,并把用户名和密码的值存入该对象中.
     ArrayList arr = new ArrayList();
     arr.add(username);
     arr.add(password);

     //生成一个session对象
     HttpSession session = request.getSession(true);
     session.removeAttribute("username");
     session.setAttribute("usrename", username);

     //调用模型组件loginHandler,检查该用户是checkLogin否已经注册
     LoginHandler login = new LoginHandler();

    try {
        //判断下
    boolean mark = login.checkLogin(arr);
        if(mark) 
            response.sendRedirect("main.jsp"); 
        else response.sendRedirect("register.jsp");

    } catch (Exception e) {
        e.printStackTrace();
    }


}
public LoginServlet() {
    super();
}
public void destroy() {
    super.destroy(); 
}

}
///////////////////////////////////////////
LoginHandler.java
package login;

import java.sql.*;
import java.util.*;

class LoginHandler {
public LoginHandler(){}

Connection conn;
PreparedStatement ps;
ResultSet rs;

//检查是否已经注册
public boolean checkLogin(ArrayList arr){
    //数据库中访问组件DBPoolZ中取得连接
    conn = DBPool.getConnection();
    String name = (String)arr.get(0);
    String password = (String)arr.get(1);
    try
    {
        //          rs = ps.executeQuery("select * from bbs.t_userinfo where username=? and password=?");
        //          while(rs.next())
        //          {       
        //              if(name.equals(rs.getString("username"))&&password.equals(rs.getString("password")))    {
        //                  DBPool.dbClose(conn,ps,rs);
        //                  return true;
        //              }
        //              else return false;
        //          }

        String sql = "select * from bbs.t_userinfo where username=? and password=?";        
        ps = conn.prepareStatement(sql);
        ps.setString(1,name);
        ps.setString(2,password);
        rs = ps.executeQuery();
        if(rs.next()){      
            DBPool.dbClose(conn,ps,rs);
            return true;
        }
        else {
            DBPool.dbClose(conn,ps,rs);
            return false;
        }       
    }
    catch(SQLException e){return false;}
}

}

////////////////////////////////////////////////////////////////////
DBPool.java

package login;

import java.io.*;
import java.sql.*;
import java.util.Properties;

public class DBPool {
private static DBPool instance = null;

   //取得连接
   public static synchronized Connection getConnection(){
       if(instance == null){
           instance = new DBPool();
       }
       return instance._getConnection();
   }

   private DBPool(){super();}

   private Connection _getConnection(){
       try{
           String sDBDriver = null;
           String sConnection = null;
           String sUser = null;
           String sPassword = null;

           Properties p = new Properties();
           InputStream is = getClass().getResourceAsStream("/db.properties");
           p.load(is);
           sDBDriver = p.getProperty("DBDriver",sDBDriver);
           sConnection = p.getProperty("Connection", sConnection);
           sUser = p.getProperty("User",sUser);
           sPassword = p.getProperty("Password",sPassword);

           Properties pr = new Properties();
           pr.put("user", sUser);
           pr.put("password", sPassword);
           pr.put("characterEncoding", "GB2312");
           pr.put("useUnicode", "TRUE");
           Class.forName(sDBDriver);
           return DriverManager.getConnection(sConnection,pr);
       }
       catch(Exception e){
           System.out.println(e);
           return null;
       }
   }

   //释放资源
   public static void dbClose(Connection conn,PreparedStatement ps,ResultSet rs)
   throws SQLException
   {
     rs.close();
     ps.close();
     conn.close();
   }

}
//////////////////////////////////
db.properties

DBDriver = com.mysql.jdbc.Driver
Connection = jdbc:mysql://localhost:3306/bbs/username=? and password=?
User =
Password =

////////////////////////////
login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


用户名:

密码:




//////////////////////////////////////////////////////
两外两个页面就不写了。

求高手指导指导

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • xing23777 2011-04-12 17:09
    已采纳

    response.sendRedirect("main.jsp");
    response.sendRedirect("register.jsp");
    你的main.jsp,register.jsp是和login.jsp放同一个文件夹里吗?

    不行的话,把路径写死试试:
    response.sendRedirect("/project_name/xxx/xxx.jsp");

    点赞 评论
  • wenchenyang 2011-04-07 08:57

    你这个加上path路径试试,重定向好像是相当于上下文路径而言的

    点赞 评论
  • wenchenyang 2011-04-07 13:19

    response.sendRedirect(" request.getContextPath()\main.jsp");
    同理试试

    点赞 评论
  • wenchenyang 2011-04-08 09:00

    改成这个试试response.sendRedirect(" request.getContextPath()/main.jsp");

    能把你的错误贴出来吗

    点赞 评论

相关推荐 更多相似问题