2015-04-14 29 views
0

代码的servlet获取数字格式异常而从jsp的访问值

写入数据访问对象类

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); 
     }                     }                     

代码

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); 

代码写入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) 

问题是,当装定方法从JSP通过使用$称为{Profile.fname}它显示的数字格式的例外。在研究了这个特殊的异常之后,我知道只有当一种数据类型试图转换为另一种数据类型时才会出现这种情况。我无法理解这种情况下发生的情况。

请帮我解决问题。 谢谢 问候。

回答

0

这是因为您正在尝试访问列表中的某个元素,而没有引用它所在的索引。

request.setAttribute("Profile", list); //Profile is a List object 

使用这个代替:

<td>${Profile[0].fname}</td>