由用户填充到ComboBox(选择JPS)填充的ArrayList

时间:2016-05-08 02:16:36

标签: java jsp servlets arraylist

这就是我想要做的事情,用户输入一个名字,名称被添加到放置在servlet上的ArrayList中,然后名称应该显示在组合框中,下面就是我所拥有的。它拖曳了错误" stray c foreach start tag"

的index.jsp

<body>
    <form action="ServletGuardar.do" method="post">
        Name<input type="text" name="name">
        <input type="submit" name="save" value="Save">
    </form>
    <hr>
    <form action="ServletGuardar.do" method="post">
    Names<select name="database1">
        <c:forEach items="${lista}" var="databaseValue">
            <option value="${databaseValue}">
                ${databaseValue}
            </option>
        </c:forEach>
    </select>
    </form>
</body>

SERVLET

ArrayList lista = new ArrayList();
String name = request.getParameter("name");
lista.add(name);

request.setAttribute("lista", "lista");

request.getRequestDispatcher("index.jsp").forward(request, response);
}

1 个答案:

答案 0 :(得分:2)

我测试了您的代码并通过更改以下内容使其正常工作

  1. 如果您的jsp没有引用标记库,则添加它
  2. 在服务器端,您没有添加ArrayList对象来请求 而不是你把它添加为String作为已经提到的另一个答案。
  3. 同时检查从第一个表单提交名称的时间,打印和 如果在服务器端获得名称值,请检查日志。
  4. <强>的index.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    
    <!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=UTF-8">
    <title>Index</title>
    </head>
    <body>
        <form action="./ServletGuardar.do" method="post">
            Name<input type="text" name="name">
            <input type="submit" name="save" value="Save">
        </form>
        <hr>
        <form action="/ServletGuardar.do" method="post">
    
        Names<select name="database1">
            <c:forEach items="${list}" var="databaseValue">
                <option value="${databaseValue}">
                    ${databaseValue}
                </option>
            </c:forEach>
        </select>
    
        </form>
    </body>
    </html>
    

    Servlet的doGet方法

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            // TODO Auto-generated method stub
    
            ArrayList<String> list = new ArrayList<String>();
    
            String name = request.getParameter("name");
            System.out.println("name=" + name);
    
            list.add(name);
    
            request.setAttribute("list", list);
    
            request.getRequestDispatcher("index.jsp").forward(request, response);
    
            // response.getWriter().append("name="+name+".Served at:
            // ").append(request.getContextPath());
        }
    

    <强>的web.xml

      <servlet>
            <servlet-name>HelloServlet</servlet-name>
            <servlet-class>com.ServletGuardar</servlet-class>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>HelloServlet</servlet-name>
            <url-pattern>/ServletGuardar.do</url-pattern>
        </servlet-mapping>