【问题标题】:How to initialize session and use them properly in JSP?如何在 JSP 中初始化会话并正确使用它们?
【发布时间】:2016-04-16 08:08:31
【问题描述】:

我的 Spring MVC 应用程序中有以下文件:login.jspHomeController.javaDatabaseService.java。当有人点击 login.jsp 中的登录按钮时,它会将数据(用户名和密码)传输到 HomeController.java,然后 HomeController.java 调用DatabaseService.java 带有一些条件并从中获取响应,最后将响应返回给 login.jsp。到目前为止,无效的用户名和密码字段是可以的。 login.jsp 中的用户名和密码与存储的用户名和密码匹配时会出现问题。 问题是,没有数据存储到会话变量/对象中。我尝试了几种方法:

public String dblogin(String username, String password) throws SQLException {
    String response = "";
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs;
    String pass = "", fname = "", lname = "";
    int userid = 0;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bloodspring", "root", "");
        stmt = conn.createStatement();
        try {
            String query = "SELECT user_id, user_fname, user_lname, pword FROM bld_user WHERE uname='"+ username +"'";
            rs = stmt.executeQuery(query);
            int res = 0;
            while( rs.next() ) {
                res++;
                pass = rs.getString("pword");
                fname = rs.getString("user_fname");
                lname = rs.getString("user_lname");
                userid = rs.getInt("user_id");
            }
            if( res == 0 ) {
                response = "Invalid Username";
            } else {
                //String new_pass = crypt.encryption(password);
                if( crypt.encryption(password).compareTo(pass) != 0 ) {
                    response = "Invalid username and/or password";
                    //response = "Original :" + pass + " :::: new : " + crypt.encryption(password);
                } else {
                    HttpServletRequest request;
                    HttpSession session = request.getSession();
                    session.setAttribute("fname", fname);
                    session.setAttribute("lname", lname);
                    session.setAttribute("username", username);
                    session.setAttribute("userid", userid);
                    response = "Login Successful";
                }
            }
        } catch( SQLException Ex) {
            Ex.printStackTrace();
            response = "Sorry something goes wrong with your MySQL query or server";
        }
    } catch( ClassNotFoundException E) {
        E.printStackTrace();
        response = "Sorry no MySQL class found";
    }
    return response;
}

但它显示了一些与空指针异常相关的错误。显示我更改代码如下:

public String dblogin(String username, String password) throws SQLException {
    String response = "";
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs;
    String pass = "", fname = "", lname = "";
    int userid = 0;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bloodspring", "root", "");
        stmt = conn.createStatement();
        try {
            String query = "SELECT user_id, user_fname, user_lname, pword FROM bld_user WHERE uname='"+ username +"'";
            rs = stmt.executeQuery(query);
            int res = 0;
            while( rs.next() ) {
                res++;
                pass = rs.getString("pword");
                fname = rs.getString("user_fname");
                lname = rs.getString("user_lname");
                userid = rs.getInt("user_id");
            }
            if( res == 0 ) {
                response = "Invalid Username";
            } else {
                //String new_pass = crypt.encryption(password);
                if( crypt.encryption(password).compareTo(pass) != 0 ) {
                    response = "Invalid username and/or password";
                    //response = "Original :" + pass + " :::: new : " + crypt.encryption(password);
                } else {
                    HttpServletRequest request = null;
                    try {
                        HttpSession session = request.getSession();
                        session.setAttribute("fname", fname);
                        session.setAttribute("lname", lname);
                        session.setAttribute("username", username);
                        session.setAttribute("userid", userid);
                        response = "Login Successful";
                        //response = fname + " " + lname;
                    } catch( java.lang.NullPointerException lex ) {
                        lex.printStackTrace();
                        response = "Something Wrong";
                    }
                }
            }
        } catch( SQLException Ex) {
            Ex.printStackTrace();
            response = "Sorry something goes wrong with your MySQL query or server";
        }
    } catch( ClassNotFoundException E) {
        E.printStackTrace();
        response = "Sorry no MySQL class found";
    }
    return response;
}

但它返回响应为“错误”[catch block of java.lang.NullPointerException]。如果我将代码更改如下:

public String dblogin(String username, String password) throws SQLException {
    String response = "";
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs;
    String pass = "", fname = "", lname = "";
    int userid = 0;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bloodspring", "root", "");
        stmt = conn.createStatement();
        try {
            String query = "SELECT user_id, user_fname, user_lname, pword FROM bld_user WHERE uname='"+ username +"'";
            rs = stmt.executeQuery(query);
            int res = 0;
            while( rs.next() ) {
                res++;
                pass = rs.getString("pword");
                fname = rs.getString("user_fname");
                lname = rs.getString("user_lname");
                userid = rs.getInt("user_id");
            }
            if( res == 0 ) {
                response = "Invalid Username";
            } else {
                //String new_pass = crypt.encryption(password);
                if( crypt.encryption(password).compareTo(pass) != 0 ) {
                    response = "Invalid username and/or password";
                    //response = "Original :" + pass + " :::: new : " + crypt.encryption(password);
                } else {
                    HttpServletRequest request = null;
                    try {
                        response = fname + " " + lname;
                    } catch( java.lang.NullPointerException lex ) {
                        lex.printStackTrace();
                        response = "Something Wrong";
                    }
                }
            }
        } catch( SQLException Ex) {
            Ex.printStackTrace();
            response = "Sorry something goes wrong with your MySQL query or server";
        }
    } catch( ClassNotFoundException E) {
        E.printStackTrace();
        response = "Sorry no MySQL class found";
    }
    return response;
}

它可以正常工作并从数据库中返回名字姓氏。所以我认为我的代码不适合会话。
谁能告诉我如何将数据库中的数据存储到会话中。

【问题讨论】:

  • 是否需要自己构建身份验证?如果没有,请尝试使用 spring security。关于npe:它扔在哪里?显示堆栈跟踪。
  • 你在一个空请求对象上调用 getSession()。( HttpServletRequest request = null; try { HttpSession session = request.getSession();
  • dsp_user: HttpSession session = request.getSession(); 是我写这些代码的第一次使用。但它没有用。
  • @Shimul 你试过答案了吗?它应该工作

标签: jsp spring-mvc


【解决方案1】:

您可以使用 spring-security 进行身份验证。除此之外,您可以遵循以下代码 sn-p。请注意,您实际上并不需要在会话中单独存储所有字段。只需将用户对象存储到会话中即可。

@RequestMapping(value = "login.html", method = RequestMethod.POST)
            public ModelAndView post(@ModelAttribute("login") LoginEntity login, HttpServletRequest req) {


    /*    CHECK IN DATABASE if the user name and password matches or not */
    UserObject userObject=dblogin(login.userName,login.password);


    if(userObject!=null){
        HttpSession session = req.getSession(true);
        session.setAttribute("user",userObject);
    }
    else
       return new ModelAndView("login");
    }

【讨论】:

    猜你喜欢
    • 2020-05-16
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多