获取从servlet到JSP页面的Arraylist值

时间:2017-06-06 16:04:42

标签: arraylist jstl sybase servlet-3.0

我需要帮助将arraylist从servlet传递到jsp页面。我正在尝试从Database填充下拉值。我已成功连接到数据库,并且值存储在列表中。该列表将传递给servlet。在servlet中,我通过打印在控制台中检索到的值进行检查。它也很好用。此外,我将属性设置为列表,并使用请求调度程序转发到我的jsp页面。在我的jsp页面中,我正在尝试使用jstl获取值。我还添加了jstl jar并在jsp页面的顶部添加了jstl标记。我已经多次搜索jstl语法,并在我的jsp中实现了它。但是在我的下拉列表中仍然没有检索到值。 在此先感谢。请有人帮助我。

            ------Success.jsp
            <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
                pageEncoding="ISO-8859-1"%>
            <%-- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> --%>
            <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
            <%--  <%@ page import = "pack.DtoForm" %>
            <jsp:useBean id="dform" scope="page" class="pack.DtoForm" />
            <jsp:useBean id="Countries" class="pack.ViewValuesInDB" scope="page"/>
            <%@ page import="java.util.*" %> --%>


            <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
            <html>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
            <title>Success page</title>
            </head>
            <body>
            <h1>Success page</h1>
            <div class="dbstyle">
            <h4>Let's start learning connecting to the database!</h4>
            <br>
            <!-- <form action="DAOServlet" method="get" name="Countries"> -->

              <label >Countries*:</label>
            <select id="Countries" name="Countries" >
            <c:forEach items="${list}" var="listt">
            <option value="${listt.Countries}">${listt}</option>
            </c:forEach>
            </select>

            </div>


            <!--  </form> -->
            </div>

            <div>
            </div>
            </body>
            </html></pre> 

            DtoForm----------------------

            package pack;



            public class DtoForm {

                  private String Countries;

                  public String getCountries() {
                        return Countries;
                  }

                  public void setCountries(String countries) {
                        this.Countries= countries;
                  }
            }

            ViewValuesInDB----------------------------

            package pack;
            import java.sql.*;
            import java.util.ArrayList;
            import com.sybase.jdbcx.*;

            public class ViewValuesInDB {

                  Connection con = null;
                  Statement stmt = null;
                  SybDriver sybDriver = null;
                  DtoForm dform=new DtoForm();

                   //List<DtoForm>dform =new ArrayList<DtoForm>();
                  ArrayList<String> list=new ArrayList<String>();

                   public ArrayList<String> makeConnection() throws Exception {
                  try
                  { 
                     sybDriver = (SybDriver) Class.forName(
                       "com.sybase.jdbc2.jdbc.SybDriver").newInstance();
                     System.out.println("Driver loaded");
                     con = DriverManager.getConnection(
                                    "jdbc:sybase:Tds:ip:port","username", "password");
                     System.out.println("Database connected");
                     Statement stmt = con.createStatement();
                     String query = "select Countries from Country";
                     ResultSet rs = stmt.executeQuery(query);
                     while (rs.next()) {
                           String a=rs.getString(1);
                           dform.setCountries(a.toString());
                           list.add(dform.getCountries());
                     }

                    for(int i=0;i<list.size();i++)
                    {
                          System.out.println(list.get(i));
                    }
                     rs.close();
                     stmt.close();
                     con.close();

                    }

                  catch(Exception e)
                  {
                  System.out.println("Error is "+e.getMessage());
                  }
                  return list;
                  }

                  public static void main(String args[]) throws Exception {
                        System.out.println("inside DB main function");
                        ViewValuesInDB sc = new ViewValuesInDB();
                    sc.makeConnection();
                  }

            }


---------------genrated html---------

            <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
            <html>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
            <title>Success page</title>

            <style>
            .dbstyle
            {
            width:100%;
            height:540px;
            background-color:#FDF5E6;
            }
            </style>
            </head>
            <body>
            <h1>Success page</h1>
            <div class="dbstyle">
            <h4>Let's start learning connecting to the database!</h4>
            <br>
            <!-- <form action="DAOServlet" method="get" name="Countries"> -->

              <label >Countries*:</label>
            <div>








            <select id="Countries" name="Countries" >

            </select>

            </div>


            <!--  </form> -->
            </div>

            <div>
            </div>
            </body>
            </html>
       -------servlet modified--------
        package pack;

        import java.util.ArrayList;

        import javax.servlet.http.HttpServlet;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;


        public class DropdownServlet extends HttpServlet {
            /**
             * 
             */
            private static final long serialVersionUID = 1L;


            protected void doGet(HttpServletRequest request,
                    HttpServletResponse response) {
        System.out.println("action is called inside dropdown");
                ViewValuesInDB daoobj = new ViewValuesInDB();
                ArrayList<String> list;
                try {
                    list = daoobj.makeConnection();
                    System.out.println(list);
                    if(null == list) 
                     {System.out.println("list is null");
                     }
                 else{
                     System.out.println("list has values");
                 }

                    request.setAttribute("list", list);
                     request.getRequestDispatcher("Success.jsp").forward(request,response);  


                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

        }


            public static void main(String args[]) throws Exception {
                 System.out.println("inside servlet function");
                 DropdownServlet sc = new DropdownServlet();
              sc.doGet(null, null);
             }
        }

0 个答案:

没有答案