weixin_45202083
17little
2020-02-27 17:04
采纳率: 37.5%
浏览 477
已采纳

Servlet登录验证写法,为啥不成功呀?

一直跳转到error页面,想各位看看是不是我代码有问题..

DBConn.java

package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBConn {
public static Connection conn = null;
/**
* 得到数据库连接
/
public static Connection getConnection() throws ClassNotFoundException,
SQLException, InstantiationException, IllegalAccessException {

try {
// 指定驱动程序
Class.forName("com.mysql.jdbc.Driver");
// 建立数据库连结
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/system","root", "root");
return conn;
} catch (Exception e) {
// 如果连接过程出现异常,抛出异常信息
throw new SQLException("驱动错误或连接失败!");
}
}
/

* 释放连接
*/
public static void closeConnection() throws Exception {
if (conn != null && !conn.isClosed())
System.out.println("数据库关闭");
conn.close();
}
}

UserDao.java

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import dao.User;
import dao.DBConn;

public class UserDao {
private Connection conn = null;
private PreparedStatement ps;

public User login(String username, String password) throws Exception{
    //List<User> users = new ArrayList<User>();
    String sql = "select * from user where username = ? and password = ?";
    //List<User> users=new ArrayList<User>();
    conn=DBConn.getConnection(); 
    ps=conn.prepareStatement(sql);
    ps.setString(1, username);
    ps.setString(2, password);
    ResultSet rs=ps.executeQuery();
    while(rs.next()) {
        User user=new User();
        user.setUsername(rs.getString("username"));//得到数据库中列 传入账号到User对象
        user.setPassword(rs.getString("password"));
        //users.add(user);
    }

    return null;    

}
}

LoginServlet.java

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.StringUtils;

import dao.DBConn;
import dao.User;
import dao.UserDao;

/**

  • Servlet implementation class LoginServlet
    /
    @WebServlet("/LoginServlet")
    public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    DBConn db = new DBConn();
    UserDao userDao = new UserDao();
    User user = new User();
    /
    *

    • @see HttpServlet#HttpServlet() */ public LoginServlet() { super(); // TODO Auto-generated constructor stub }

    /**

    • @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, response); }

    /**

    • @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
      */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      response.setContentType("text/html;charset=utf-8");
      request.setCharacterEncoding("utf-8");
      PrintWriter out= response.getWriter();

      String username=request.getParameter("username");
      String password=request.getParameter("password");

      UserDao userDao = new UserDao();

      User user = null;
      try {
          user = userDao.login(username,password);
      } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      // 判断user是否为空
      if(user != null){
          // 转发到LoginSuccess.jsp页面
          //  getRequestDispatcher()是请求转发
                  request.getRequestDispatcher("register.jsp").forward(request, response);
          }else{
          // 登录失败
              request.getRequestDispatcher("error.jsp").forward(request, response);
                  }
          }
      

      }

login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


登录页面

用户名: 密码:


register.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


$Title$


成功!


error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


$Title$


失败!


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

2条回答 默认 最新

  • weixin_43975639
    shavenField 2020-02-27 17:47
    已采纳

    login()的最后一句,return null

    点赞 评论
  • dashicaineng
    点赞 评论

相关推荐