今天写代码遇到一个问题,在jsp页面中通过form的得到的select标签的值,form提交给自身页面,然后通过request.getParameter()方法取得值
测试代码如下(文件名为:testselect.jsp):
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <% request.setCharacterEncoding("GB18030");//加上这一句解决的 String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; //存放下来菜单对应值的数组 ArrayList nu = new ArrayList(); nu.add("一"); nu.add("二"); nu.add("三"); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'testselect.jsp' starting page</title> </head> <body> 通过request.getParameter("number")方法取得下拉框选取的值 <form method=post action="testselect.jsp"> <!-- 提交给自身 --> <select name=number> <% for (int i = 0; i < nu.size(); i++) { out.print("<option>" + nu.get(i) + "</option>"); } %> </select> <input type="submit" value="提交" name="submit"> </form> </body> <% //取得提交的数字,并显示 String n = (String) request.getParameter("number"); out.print("选的值是:" + n); %> </html>
运行界面: