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

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

代码:
/////////////////////////////////////////
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");

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 无源定位系统的时差估计误差标准差
  • ¥15 请问这个代码哪里有问题啊
  • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?
  • ¥15 通过GaussianView进行结构微调消除虚频
  • ¥15 调用transformers库
  • ¥15 由于导出的数据名字中带有/,导致Matlab打不开,怎么办?
  • ¥15 新硬盘安装的程序总是崩溃,提示遇到错误
  • ¥15 openpcdet自制数据集评估bev精度和3d精度相同
  • ¥15 excel 上下按钮 显示行
  • ¥20 云卓h12pro 数传问题