私はデータベースにテキストフィールドのいくつかを入れたいデータベースにデータを持っています。私はdbからデータを取得するメソッドを作成します。私はopen-account.jspページを持っています。そこには、名前、姓、電子メールがすでにデータベースから事前に入力されているので、サーブレットからgetメソッドを使用したフォームがあります。サーブレットからjspページにデータを送信しようとすると、このフィールドはnullになります。サーブレットからJSPにデータを渡すのではないと思いますが、わかりません。ここに私のコードは次のとおりです。サーブレットからjspのテキストフィールドデータを入力してください。
Databaseクラス:
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
{
}
}