这就是我想要做的事情,用户输入一个名字,名称被添加到放置在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);
}
答案 0 :(得分:2)
我测试了您的代码并通过更改以下内容使其正常工作
<强>的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>