How to display the data in database on a jsp using struts1?

8.3k Views Asked by At

I am new to struts. I want to display all the details from a table in database on a jsp to user. Please help me in doing these.

thanks in advance, Vidya

LoginFormBean.java

public class LoginFormBean extends ValidatorForm 
{

    public String name;
    public String city;
    public String state;
    public String phone;
    public String mailId;
    public String gender;
    public String branch;
    private List studentDetails;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCity() {
        return city;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPhone() {
        return phone;
    }

    public void setMailId(String mailId) {
        this.mailId = mailId;
    }

    public String getMailId() {
        return mailId;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getBranch() {
        return branch;
    }

    public void setBranch(String branch) {
        this.branch = branch;
    }

    public String getGender() {
        return gender;
    }
    public void setState(String state) {
        this.state = state;
    }
    public String getState() {
        return state;
    }

    public List getStudentDetails() {
        return studentDetails;
    }
    public void setStudentDetails(List studentDetails) {
        this.studentDetails = studentDetails;
    }


}

Logindao.java

public class Logindao {
public List viewStudents()
    {

          Connection conn=DBUtil.getDBConnection();
          List list=new ArrayList();
          Statement stmt = null;
          ResultSet rs=null;
          String query="select * from student_info";

               stmt=conn.createStatement();
               rs= stmt.executeQuery(query);
              while(rs.next())
              {

                    list = new ArrayList();  
                    list.add(rs.getString("name"));
                    list.add(rs.getString("phone"));
                    list.add(rs.getString("email"));
                    list.add(rs.getString("city"));
                    list.add(rs.getString("state"));
                    list.add(rs.getString("gender"));
                    list.add(rs.getString("branch"));
              }

          }
              return list;
    }

RetrieveDataActionClass.java

public class RetrieveDataActionClass extends Action{

    public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)throws Exception,ServletException
     {
            List list=new ArrayList();
            String key="success";
            Logindao dao=new Logindao();
            LoginFormBean formbean=(LoginFormBean)form;

    try{

        list=dao.viewStudents();
        formbean.setStudentDetails(dao.viewStudents());
        }
        catch(Exception e)
        {
           e.printStackTrace();
           System.out.println("Error occured");

        }
        return mapping.findForward(key);
        }
        } 

viewstudents.jsp

<body>
   <html:form action="/retrieve" method="post">
    <table border="1">  
    <tr>
    <th>Name</th>
    <th>Phone</th>
    <th>MailId</th>
    <th>City</th>
    <th>State</th>
    <th>Gender</th>
    <th>Branch</th>
    </tr>  

    <logic:iterate id="students" name="LoginFormBean" property="studentDetails">
    <tr>  
    <td><bean:write name="students" property="name"/></td>  
    <td><bean:write name="students" property="phone"/></td>  
    <td><bean:write name="students" property="mailId"/></td>  
    <td><bean:write name="students" property="city"/></td>  
    <td><bean:write name="students" property="state"/></td>  
    <td><bean:write name="students" property="gender"/></td>  
    <td><bean:write name="students" property="branch"/></td>  
    </tr>  
    </logic:iterate> 
    </table>
    </html:form>
  </body>

I have a table 'student_info' in my sqlserver database. columns:name,phone,email,city,state,gender,branch

I want to display the output in the following format in jsp.

name phone email city state gender branch

abc 55555 [email protected] hyd AP Female IT

xyz 55555 [email protected] bang KT Female CSE

Error:

javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot find bean: "LoginFormBean" in any scope

Thanks in Advance, Vidya.

1

There are 1 best solutions below

0
On

You need to instantiate your bean"LoginFormBean" in your JSP before logic:iterate . you can use use jsp:usebean tag for that