如何在jsp中动态设置复选框的值(值来自数据库)

时间:2011-06-30 22:47:14

标签: jsp checkbox

我有一个jsp文件,我必须在其中显示带有日期和说明的注释 由于特定日期可能有多个音符(如在调度程序中),因此一次显示多个音符。现在我希望用户可以通过复选框的帮助来修改数据库中的特定注释。现在,当笔记的值从数据库动态传递时,我如何设置复选框的值。

fetchcontent.java

public class CnmsDes extends HttpServlet{
    public void doPost.......{
        List list=new ArrayList();
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        System.out.println("Oracle Connect Example.");
        Connection conn = null;
        String url = "jdbc:oracle:thin:@localhost:1521:xe";
        String driver = "oracle.jdbc.driver.OracleDriver";
        String userName = "system"; 
        String password = "mint";
        Statement st;
        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url,userName,password);
            System.out.println("Connected to the database");

            String  ndate  = request.getParameter("date");
            String  eid  = request.getParameter("empid");
            String strar[] = ndate.split("/");
            String cdate = strar[0]+"/" + strar[1]+"/"+ strar[2];
            if(eid==null||eid=="Enter Your Employee ID"){response.sendRedirect("viewnotes.jsp");}
            String query = "select * from CNMS_NOTES where emp_id='"+eid+"' and note_date='"+cdate+"'";

            Statement stmt=conn.createStatement();
            ResultSet rs=stmt.executeQuery(query);


            while(rs.next()){
                list.add(rs.getString("note_date"));
                list.add(rs.getString("title"));
                list.add(rs.getString("description"));

            }

        }
        catch(Exception e){out.print(e);}
        request.setAttribute("description",list);
        RequestDispatcher rd = request.getRequestDispatcher("/displaynotes.jsp");
        rd.forward(request, response);
    } 
}

displaynotes.jsp

<%@page language="java" import="java.util.*" %>
<html>
    <body background="images/bg1.jpg">     
        <form>
            <table class="t" border="1" width="650" align="center" >
                <tr>
                    <td width="10"><b></b></td>
                    <td width="100"><b>Date</b></td>
                    <td width="150"><b>Title</b></td>
                    <td width="100"><b>Description</b></td>            
                </tr>

                <%  
                    Iterator itr;
                    List data=(List)request.getAttribute("description");            
                    int k=data.size();
                    request.setAttribute("size",k);        
                    for(itr=data.iterator();itr.hasNext();) {
                %>

                <tr class="a">          
                    <td>
                        <input type="checkbox" name="cbdate" value="<%=request.getParameter("checkeddate") %>">
                    </td>
                    <td width="100">
                        <input type="text" name="checkeddate" value="<%=itr.next()%>" class="b">
                    </td>
                    <td width="150">
                        <input type="text" name="checkedtitle" value="<%=itr.next()%>" class="b">
                    </td>
                    <td width="200">
                        <textarea cols="39" rows="3"><%=itr.next()%></textarea>
                    </td>        
                </tr>
                <% } %>
            </table>
            <table border="1" align="center">
                <tr>
                    <td>
                        <input type="submit" value="MODIFY" onclick="this.form.action='expriment.jsp';">
                    </td>
                    <td>
                        <input type="submit" value="DELETE" onclick="this.form.action='abc.jsp';">
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

我要做的是创建一个新的List数组,然后将当前列表对象添加到该数组中。

或者我真的可以继续创建一个名为Notes的新类文件,该文件将保存3个变量ndate,eid,cdate然后为它们制作getter / setter。然后我会在while(rs.next)循环中调用一个新的Notes类实例,使用我制作的setter方法设置每个值,然后将其添加到我制作的列表数组中。

public class Notes{

Date ndate;
String Title;
Date cdate;
int eid;

//create getter methods
//create setter methods
}
  

现在是CnmsDes类

public class CnmsDes extends HttpServlet{
    public void doPost.......{List list=new ArrayList();
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        System.out.println("Oracle Connect Example.");
        Connection conn = null;
        String url = "jdbc:oracle:thin:@localhost:1521:xe";
        String driver = "oracle.jdbc.driver.OracleDriver";
        String userName = "system"; 
        String password = "mint";
        Statement st;
        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url,userName,password);
            System.out.println("Connected to the database");

            String  ndate  = request.getParameter("date");
            String  eid  = request.getParameter("empid");
            String strar[] = ndate.split("/");
            String cdate = strar[0]+"/" + strar[1]+"/"+ strar[2];
            if(eid==null||eid=="Enter Your Employee ID"){response.sendRedirect("viewnotes.jsp");}
            String query = "select * from CNMS_NOTES where emp_id='"+eid+"' and note_date='"+cdate+"'";

            Statement stmt=conn.createStatement();
            ResultSet rs=stmt.executeQuery(query);

            List<Notes> notesList = new ArrayList<Notes>();
            while(rs.next()){
                Notes myNotes = new Notes;
                myNotes.setNdate(rs.getDate("note_date"));
                myNotes.setTitle(rs.getString("title"));
                //you get the idea
                notesList.add(myNotes);
            }

        }
        catch(Exception e){out.print(e);}
        request.setAttribute("description",list);
        RequestDispatcher rd = request.getRequestDispatcher("/displaynotes.jsp");
        rd.forward(request, response);
    } 
}