0

I wanted to send JSP form values to database through a servlet and receive the results in another JSP. However, I am able to display the complete results that I receive through the database query, but I couldn't split them and display only the value that I want.

DB class snippet:

public  ArrayList<String>  getTablenodomain()
{
    ArrayList<String> ob1 = new ArrayList<String>();
    try
    {
    s = con.prepareStatement("select AU_ID,DOMAIN_ID,SAFE,SAFE from auditstats");
    rs = ps.executeQuery();
        while(rs.next())
        {
        ob1.add(rs.getString(1));
        ob1.add(rs.getString(2));
        ob1.add(rs.getString(3));
        ob1.add(rs.getString(4));
        }
    }
    catch(Exception ee)
    {
        System.out.println("WHERE ARE YOU DOMAIN");
    }
    return ob1;
}

My servlet snippet :

DBCoding ob3 = new DBCoding();
if (safe.equals("ALL"))
{   ArrayList<String> a3 = new ArrayList<String>();
    a3 = ob3.getTablenodomainsd();
    request.setAttribute("safe", a3);
}
else
{
    ArrayList<String> al3 = new ArrayList<String>();
    al3 = ob3.getTable3(safe, domain);
    request.setAttribute("safe", al3);
}
RequestDispatcher rd = request.getRequestDispatcher("page2.jsp");
rd.forward(request, response);

Page2.jsp:

<%
  ArrayList<String> ob3 = new ArrayList<String>(); %>
  <%if(request.getAttribute("safe")!=null)
  {
      ob3 = (ArrayList<String>)request.getAttribute("safe");
      %>

      <%for(int j=0;j<ob3.size();j++)

      {
      %>
<table>   
          <tr>STEP-DURATION</tr>
          <tr><%=ob3.get(j)%></tr>
</table>             
     <%} 

     }%>  

By this way, I receive the complete data(all columns/rows) and they get displayed in page2.jsp. But how canI get the data individually?

N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
Sanjivi
  • 75
  • 1
  • 9

1 Answers1

0

What you should do is create a POJO for containing all the data of one row. Like...

public class Safe{
    private String auId;
    private String domainId;
    private String safe;
    //other fields...

    //constructor, getters, setters

}

Now your getTablenodomian() method should look like...

public  ArrayList<Safe>  getTablenodomain()
    { 
        ArrayList<Safe> list = new ArrayList<Safe>();
        Safe safe = null;

        try{
s = con.prepareStatement("select AU_ID,DOMAIN_ID,SAFE,SAFE from auditstats");

rs = ps.executeQuery();
while(rs.next())
{
    safe = new Safe(rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4));
    list.add(safe);

}
}
       catch(Exception ee)
    {System.out.println("WHERE ARE YOU DOMAIN");}
    return list;

}

and your servlet snippet will change to...

DBCoding ob3 = new DBCoding();
                if (safe.equals("ALL"))
                {   ArrayList<Safe> list =  ob3.getTablenodomainsd();
                    request.setAttribute("safe", list);
                    }

                else
                {
                    ArrayList<String> al3 = new ArrayList<String>();
                    al3 = ob3.getTable3(safe, domain);
                request.setAttribute("safe", al3);

                }
}   
        }
RequestDispatcher rd = request.getRequestDispatcher("page2.jsp");

rd.forward(request, response);

and your jsp page will be:

<%
  ArrayList<Safe> list= new ArrayList<Safe>(); %>
  <%if(request.getAttribute("safe")!=null)
  {
      list= (ArrayList<Safe>)request.getAttribute("safe");
      %>
<table>
     <tr>
         <th>AU_ID </th>
         <th>DOMIAN_ID </th>
         <th>SAFE </th>
     </tr>
      <%
        Safe safe = null;
        for(int j=0;j<list.size();j++){
            safe = list.get(j);
      %>

       <tr>
         <td><%safe.getAuId() %></td>
         <td><%safe.getDomainId() %></td>
         <td><%safe.getSafe() %></td>
      </tr>

     <%
      } %>
</table>

<%     }%>  

By the way,your coding style is not standard. You should consider using Hibernate framework for database operation and Spring MVC for implementing MVC pattern. In JSP you should use JSTL tag library, Expression Langauge. Try to avoid all java code inside JSP.

Md Zahid Raza
  • 941
  • 1
  • 11
  • 28
  • Thanks raza. I created POJO and and in JSP I gave: safe.Auditid safe.DomainId safe.Safee The rows and columns are correctly displayed, but it prints the value as safe.Auditid, safe.DomainId and safe.Safee. It is not getting the actual database values. Where did i go wrong? – Sanjivi Aug 21 '16 at 18:56
  • If you are copying my code provided. Then copy now, I have made some edits. Initially I provided these code as hint for doing yourself. Value is printed as it is because that is printed statically, meaning treated as static html content. If you have to print dynamic content then it should be inside scriptlet tag. I have made edit...copy now and try again. If still problem..post your updated code and I will help then. – Md Zahid Raza Aug 22 '16 at 07:11