将数组列表从Servlet传输到JSP

时间:2013-12-03 11:53:24

标签: java jquery jsp servlets

我有一些问题。我有从数据库

创建列表的功能
public List<Langganan> getlistLanggananID(String kode){
    System.out.println(kode);
    List<Langganan> tokoList = new ArrayList();
    try {
       String sql = "SELECT idms_langganan, nama_langganan, alamat, kota FROM ms_langganan WHERE idms_langganan = " + "'" + kode + "'";
       Connection conn = datman.logOn();
       Statement st = (Statement) conn.createStatement();
       ResultSet rs = st.executeQuery(sql);
       /* get result from list */
       while (rs.next()){
          Langganan lg = new Langganan();
          lg.setKodelangganan(rs.getString("idms_langganan"));
          lg.setNama(rs.getString("nama_langganan"));
          lg.setAlamat(rs.getString("alamat"));
          lg.setKota(rs.getString("kota"));
          tokoList.add(lg);
       }
    } catch (Exception se) {
        Logger.getLogger(MasterLangganan.class.getName()).log(Level.SEVERE, null, se);
        java.lang.System.out.println("Error on Function MasterLangganan Show List: " + se.toString());   
    }
    return tokoList;

}

它根据特定ID返回列表 - &gt; KODE

然后我通过servlet

执行该函数
 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {
       String kode = request.getParameter("kode");
       MasterLangganan ml = new MasterLangganan();
       ml.getlistLanggananID(kode);
        /* sending list to JSP page */
        request.setAttribute("listlangganan",ml.getlistLanggananID(kode));
    } catch(Exception es){
        Logger.getLogger(setSettings.class.getName()).log(Level.SEVERE, null, es);
    }finally {            
       java.lang.System.out.println("setSettings Servlet has been executed");
    }
}

但是如何将结果从servlet发送到jsp页面?因为我在jsp页面上只得到了错误

这是JSP页面

 <div class="panel-heading">
  <h3 class="panel-title"><i class="fa fa-bar-chart-o"></i> Tambah Langganan</h3>
 </div>
     <%
          MasterLangganan ml = new MasterLangganan();
          ml.getlistLanggananID("000");
     %>
  <div class="panel-body">
     <form name="system_settings" method="POST" action="<%=request.getContextPath() %>/setDataLangganan">
        <div class="col-lg-6">
          <p>Nama</p> 
          <input type="text" class="form-control" name="nama_langganan" readonly="readonly" id="nama_langganan" value="<% ((Langganan)request.getAttribute("listlangganan")).getNama(); %>">
          <p>Alamat</p>
          <input type="text" class="form-control" name="alamat" readonly="readonly" id="alamat" value="<% ((Langganan)request.getAttribute("listlangganan")).getAlamat(); %>">
          <p>Kota</p>
          <input type="text" class="form-control" name="kota" readonly="readonly" id="kota" value="<% ((Langganan)request.getAttribute("listlangganan")).getKota(); %>">
          <p>Sales Area</p>
          <input type="text" class="form-control" name="salesarea" readonly="readonly" id="salesarea" value="<% ((Langganan)request.getAttribute("listlangganan")).getSalesArea(); %>">
</div>
  <div class="col-lg-6">
    <p>Actions</p>
      <input type="button" class="btn btn-info" value="Edit" name="edit" id="edit">
      <input type="button" class="btn btn-warning" value="Cancel" name="cancel" id="cancel">
      <input type="submit" class="btn btn-danger" value="Save" name="save" id="submit" style="display: none;" >
 </div>
</form>

我只需要将列表放在文本字段中,以便可以编辑。

如何正确地做到这一点?

提前感谢。

2 个答案:

答案 0 :(得分:0)

  

将数组列表从Servlet传输到JSP

您需要转发到JSP页面

request.setAttribute("listlangganan",ml.getlistLanggananID(kode));
request.getRequestDispatcher("MyJSP.jsp").forward(request,response);

MyJSP.jsp

 <c:forEach items="${listlangganan}" var="item">

  <!--Here populate the data to table or div-->    
 </c:forEach>

答案 1 :(得分:0)

String kode = request.getParameter("kode");在您的帖子中,您提到kode是一个ID,因此request.getParameter("kode");无效 使它工作做这样一个隐藏的领域

<input type ="hidden" name="kode" value="<%=values you want to pass%>"> and submit this hidden field along with the form.Then only you can receive using `request.getParameter()`