我正在尝试根据用户名和密码获取用户的详细信息。 UserControllerServlet从UserUtil类(辅助类)接收响应,并在redirect.jsp上打印详细信息。
问题是当我从登录页面输入用户名和密码时,Servlet不会重定向到redirect.jsp页面。我得到的只是空白。
我尝试在Servlet中从UserUtil打印响应,对象只返回null。
文件:login.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>
文件:User.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 + "]";
}
}
文件:UserUtil.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 |
+-------------+-----------------+----------+
答案 0 :(得分:0)
我尝试在Servlet中从UserUtil打印响应,对象只返回null。
在您的代码中,数据源无法获得连接,这就是为什么它显示为null。
在UserUtil.java文件中,捕获异常。您可以清楚地确定特殊例外的原因。
catch(Exception e)
{e.printStackTrace();
}
finally{
close(conn,stmt,rs);
}
return user1;
发给我反馈:)