code for servlet
String id=request.getParameter("id");
UserProfileDAO dao = new UserProfileDAO();
List<UserProfilePojo> list = dao.UserProfile(id);
request.setAttribute("Profile", list);
RequestDispatcher view= request.getRequestDispatcher("Profile.jsp");
view.forward(request, response);
在数据访问对象类中编写的代码
public List<UserProfilePojo>UserProfile(String id)
{
String query ="select fname from registration where Id="+id;
List<UserProfilePojo> list = new ArrayList<UserProfilePojo>();
UserProfilePojo User = null;
try {
connection = getConnection();
stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
{
User = new UserProfilePojo();
User.setFname(rs.getString("fname"));
list.add(Profile);
} }
在 Pojo 类中编写的代码
public class UserProfilePojo
{
private String fname;
public String getFname();
{
return fname;
}
public void setFname(String fname)
{
this.fname = fname;
}
}
通过 JSP 访问
<td>${Profile.fname}</td>
异常堆栈跟踪
message java.lang.NumberFormatException: For input string: "fname"
description The server encountered an internal error that prevented
it from fulfilling this request.
exception
org.apache.jasper.JasperException: java.lang.NumberFormatException:
For input string: "fname"
root cause
java.lang.NumberFormatException: For input string: "fname"
java.lang.NumberFormatException.forInputString(Unknown Source)
问题是,当使用 ${Profile.fname} 从 jsp 调用 setter 方法时,它会显示数字格式异常。在研究了这个特定的异常之后,我知道只有当一种数据类型尝试转换为另一种数据类型时,才会发生这种情况。我无法理解在这种特定情况下它在哪里发生。
请帮助我解决问题。谢谢问候。
这是因为您尝试访问列表中的元素而不引用它所在的索引。
request.setAttribute("Profile", list); //Profile is a List object
请改用这个:
<td>${Profile[0].fname}</td>