【发布时间】:2015-12-19 11:12:37
【问题描述】:
我在 html 页面上有下拉列表。在提交表单时,我需要将值传递给 servlet。当我从下拉列表中选择红色时,提交表单的 url 框为, http://localhost:8080/sampleapp/%E2%80%9Dcolor.do%E2%80%9D?%94lb%94=%941%94
预期的网址 http://localhost:8080/sampleapp/color.do?color=1
Html 和 web.xml 条目如下所示。有人能找到解决此问题的方法吗?
home.html
<form action=”colors.do” method=”post”>
Select the colors
<p>
<select name=”color” size=”1” onchange='this.form.submit()'>
<option value=”red”>1</option>
<option value=”green”>2</option>
<option value=”yellow”>3</option>
</select> <br>
<br>
<button type="submit" value="Submit">Submit</button>
</form>
web.xml
<servlet>
<servlet-name>color App</servlet-name>
<servlet-class>com.test.ColorServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>color App</servlet-name>
<url-pattern>/colors.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>home.html</welcome-file>
</welcome-file-list>
小服务程序
public class ColorServlet extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getParameter("color"));
}
}
【问题讨论】: