我在数据库中有数据,我希望某些文本字段来自数据库。我创建了一个从db获取数据的方法。我有一个open-account.jsp页面,其中有一个表单,其中已经从数据库预先填充了名字,姓氏和电子邮件,因此我使用了servlet中的get方法。当我尝试将数据从servlet发送到jsp页面时,该字段为空。我认为它不是将数据从servlet传递给jsp,我不确定。这是我的代码:
数据库类:
public static ArrayList getUsers()
{
ArrayList<Users> userList = new ArrayList<>();
try
{
DBConnection.connectToDB();
String query = "SELECT * FROM userlogin";
stmt = DBConnection.conn.prepareStatement(query);
ResultSet rs = stmt.executeQuery();
while(rs.next())
{
Users user = new Users();
user.setFirstName(rs.getString("firstname"));
user.setLastName(rs.getString("lastname"));
user.setEmail(rs.getString("email"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
userList.add(user);
}
}
catch(Exception e)
{
System.out.println(e);
}
return userList;
}
开account.jsp
<!DOCTYPE html>
<html>
<head>
<title>Open Account</title>
</head>
<body>
<h3>Please fill in the details</h3>
<form name="openAccount" action="OpenAccount" method="GET">
<!-- in openaccount servlet, we will get the users info and fill some
of the forms below for them -->
First Name: <input type="text" name="firstname" value= <%= request.getAttribute("Users.getFirstName()") %> > <br/><br/>
Last Name: <input type="text" name="lastname"> <br/><br/>
Email: <input type="text" name="email"> <br/><br/>
</form>
<form name="chooseAccount" action="OpenAccount" method="POST">
Select the type of account:
<select name="accounttype">
<option>Checking</option>
<option>Saving</option>
<option>Money Market</option>
<option>Credit Card</option>
</select> <br/><br/>
Please check the box if everything above is complete:
Agree <input type="radio" name="agree" value="Agree">
Disagree <input type="radio" name="agree" value="Disagree">
<br/><br/>
<input type="submit" value="submit" name="Submit">
</form>
</body>
</html>
OpenAccount serlvet
@WebServlet("/OpenAccount")
public class OpenAccount extends HttpServlet
{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
ArrayList<Users> userList = DBConnection.getUsers();
request.setAttribute("Users", userList);
RequestDispatcher dispatcher = request.getRequestDispatcher("open-account.jsp");
dispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
}
}
答案 0 :(得分:0)
request
获取列表,您需要在jsp中进行投射User
个实例,而您的代码会传递ArrayList
的整个User
。代码段只是为了理解您需要提取所需信息的内容。
// make sure to import all required class
Object users = request.getAttribute('Users');
if(null != users && users instanceof ArrayList){
ArrayList<User> userList = (ArrayList<User>)request.getAttribute('Users');
for(User user: userList){
out.println(user.getFirstName());
}
} else {
out.println("No user records found.");
}
P.S。使用jsp scriplets是不好的做法,你可以改用JSTL。
答案 1 :(得分:-1)
让我们来看看你的jsp页面。似乎您只想显示用户配置文件而不是用户配置文件列表。所以你应该修改你的代码,如下所示:
数据库类:
public static Users getUsers(String username)
{
Users user = new Users();
try
{
DBConnection.connectToDB();
String query = "SELECT * FROM userlogin where username=?";
stmt = DBConnection.conn.prepareStatement(query);
stmt.setString(1,username);
ResultSet rs = stmt.executeQuery();
while(rs.next())
{
user.setFirstName(rs.getString("firstname"));
user.setLastName(rs.getString("lastname"));
user.setEmail(rs.getString("email"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
}
}
catch(Exception e)
{
System.out.println(e);
}
return user;
}
开account.jsp:
<!DOCTYPE html>
<html>
<head>
<title>Open Account</title>
</head>
<body>
<h3>Please fill in the details</h3>
<form name="chooseAccount" action="OpenAccount" method="POST">
<!-- in openaccount servlet, we will get the users info and fill some
of the forms below for them -->
First Name: <input type="text" name="firstname" value= <%= request.getAttribute("firstname") %> > <br/><br/>
Last Name: <input type="text" name="lastname" value= <%= request.getAttribute("lastname") %>> <br/><br/>
Email: <input type="text" name="email" value= <%= request.getAttribute("email") %>> <br/><br/>
Select the type of account:
<select name="accounttype">
<option>Checking</option>
<option>Saving</option>
<option>Money Market</option>
<option>Credit Card</option>
</select> <br/><br/>
Please check the box if everything above is complete:
Agree <input type="radio" name="agree" value="Agree">
Disagree <input type="radio" name="agree" value="Disagree">
<br/><br/>
<input type="submit" value="submit" name="Submit">
</form>
</body>
</html>
OpenAccount serlvet:
package com.javabycode;
import java.io.IOException;
import java.util.ArrayList;
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;
@WebServlet("/OpenAccount")
public class OpenAccount extends HttpServlet
{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
Users users = DBConnection.getUsers();
request.setAttribute("firstname", users.getFirstName());
request.setAttribute("lastname", users.getLastName());
request.setAttribute("email", users.getEmail());
RequestDispatcher dispatcher = request.getRequestDispatcher("open-account.jsp");
dispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
// do something
//System.out.println(request.getAttribute("firstname"));
}
}
我修改了你的例子并通过引用post
在servlet容器上运行它如果您只是初学者,我强烈建议您使用Servlet 3 tutorial
系列希望这有帮助!