JSP-Servlet:如何从servlet

时间:2016-08-20 22:47:46

标签: java jsp servlets

我想通过servlet将JSP表单值发送到数据库,并在另一个JSP中接收结果。但是,我能够显示通过数据库查询收到的完整结果,但我无法拆分它们并只显示我想要的值。

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

我的servlet片段:

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>             
     <%} 

     }%>  

通过这种方式,我收到完整的数据(所有列/行),它们将显示在page2.jsp中。但是我怎样才能获得单独的数据呢?

1 个答案:

答案 0 :(得分:0)

您应该做的是创建一个包含一行所有数据的POJO。像...

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

    //constructor, getters, setters

}

现在你的getTablenodomian()方法应该看起来像......

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;

}

并且您的servlet代码段将更改为...

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

您的jsp页面将是:

<%
  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>

<%     }%>  

顺便说一下,你的编码风格不是标准的。您应该考虑使用Hibernate框架进行数据库操作,使用Spring MVC来实现MVC模式。 在JSP中,您应该使用JSTL标记库Expression Langauge。尽量避免使用JSP中的所有Java代码。