今天寫代碼遇到一個問題,在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>
運行介面: