Servlet 将响应作为 null 返回给 jsp jdbc 请求



我正在尝试根据用户名和密码获取用户的详细信息。UserControllerServlet 接收来自 UserUtil 类(helper 类(的响应,并打印有关重定向.jsp的详细信息。

问题是当我从登录页面输入用户名和密码时,Servlet 不会重定向到重定向.jsp页面。我得到的只是空白。

我尝试在 Servlet 中打印来自 UserUtil 的响应,但对象只返回 null。

文件:登录.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
</head>
<body>
    <form action="/Login_Test/UserControllerServlet" method="GET">
        <input type="hidden" name="command" value="login">
        <table>
                <tbody>
                    <tr>
                        <td><label>User name:</label></td>
                        <td><input type="text" name="username"/></td>
                    </tr>
                    <tr>
                        <td><label>Password:</label></td>
                        <td><input type="text" name="password"/></td>
                    </tr>
                    <tr>
                        <td><label></label></td>
                        <td><input type="submit" value="Submit" class="save"/></td>
                    </tr>
                </tbody>
            </table>
    </form>
</body>
</html>

文件:用户.java

package com.logintest.example;
public class User {
    private String username;
    private String password;
    private String usertype;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getUsertype() {
        return usertype;
    }
    public void setUsertype(String usertype) {
        this.usertype = usertype;
    }

    public User(String username, String password, String usertype) {
        super();
        this.username = username;
        this.password = password;
        this.usertype = usertype;
    }
    public User() {
        // TODO Auto-generated constructor stub
    }
    @Override
    public String toString() {
        return "User [username=" + username + ", password=" + password + ", usertype=" + usertype + "]";
    }
}

文件:用户实用.java

package com.logintest.example;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
public class UserUtil {
     DataSource dataSource;
    public UserUtil(DataSource theDataSource){
        dataSource = theDataSource;
    }
    public List<User> getUsers() throws Exception{
        List<User> user = new ArrayList<>();
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try{
            conn=dataSource.getConnection();
            String sql="select * from user_login";
            stmt=conn.createStatement();
            rs=stmt.executeQuery(sql);
            //process resultset
            while(rs.next())
            {
                String username = rs.getString(1);
                String password = rs.getString(2);
                String usertype = rs.getString(3);
                //create user object
                User user1 = new User(username,password,usertype);
                //add to list
                user.add(user1);
            }
        }catch(Exception e)
        {
        }
        finally{
            close(conn,stmt,rs);
        }
        return user;
    }
    private void close(Connection conn, Statement stmt, ResultSet rs) {
        try
        {
            if(rs!=null)
            {
                rs.close();
            }
            if(stmt!=null)
            {
                stmt.close();
            }
            if(rs!=null)
            {
                rs.close();
            }
        }
        catch(Exception e)
        {
        }
    }
    public User getUser(String username, String password) throws Exception{
        User user1 = null;
        Connection conn=null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        String username1=null;
        String password1=null;
        String usertype1=null;
        try
        {
            conn=dataSource.getConnection();
            String sql = "select * from user_type where username=? and password=?";
            stmt=conn.prepareStatement(sql);
            stmt.setString(1, username);
            stmt.setString(2, password);
            rs=stmt.executeQuery();
            if(rs.next())
            {
                 username1 = rs.getString(1);
                 password1 = rs.getString(2);
                 usertype1 = rs.getString(3);
                 user1 = new User(username1,password1,usertype1);
            }
            else
            {
                throw new Exception("User not found: ");
            }
        }catch(Exception e)
        {}
        finally{
            close(conn,stmt,rs);
        }
        return user1;
    }
}

文件:UserControllerServlet.java

package com.logintest.example;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.RequestDispatcher;
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 javax.sql.DataSource;
@WebServlet("/UserControllerServlet")
public class UserControllerServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private UserUtil userUtil;
    @Resource(name="jdbc/sample")
    private DataSource dataSource;

    @Override
    public void init() throws ServletException {
        // TODO Auto-generated method stub
        super.init();
        try{
            userUtil = new UserUtil(dataSource);
        }catch(Exception e){
            throw new ServletException(e);
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try
        {
            String command = request.getParameter("command");
            switch(command)
            {
            case "login":
                login(request,response);
                break;
            }
        }
        catch(Exception e)
        {
        }
    }
    private void login(HttpServletRequest request, HttpServletResponse response)
    throws Exception{

        String username=request.getParameter("username");
        String password=request.getParameter("password");
        User user1 = userUtil.getUser(username, password);
        request.setAttribute("USER", user1);

        RequestDispatcher dispatcher = request.getRequestDispatcher("/redirect.jsp");
        dispatcher.forward(request, response);
    }
}

表- user_login

+-------------+-----------------+----------+
| username    | password        | usertype |
+-------------+-----------------+----------+
| sample_user | sample_password | employee |
+-------------+-----------------+----------+

登录.jsp屏幕

这是我得到的回应

我尝试在 Servlet 中打印来自 UserUtil 的响应,但对象只返回 null。

在您的代码中,数据源无法获取连接,这就是它显示为 null 的原因。

在您的 UserUtil.java 文件中,捕获异常。您可以清楚地确定特定异常的原因。

catch(Exception e)
    {e.printStackTrace();
}
    finally{
        close(conn,stmt,rs);
    }
    return user1;

向我发送反馈:)

最新更新