我们常用: request.getParameter(String name)和 request.getParameterValues(String name)
取得用户在表单中输入的数据,但在EL中可以使用隐含对象param和paramValues来取得数据。如下所示:
${param.name} ${paramValues.name}
这里param的功能和request.getParameter(String name}相同,而paramValues和request.getParameterValues(String name)相同。
请看例子: 表单(Form.html) <html> <head> <title>Form.html</title>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312"> </head>
<body>
<form name="Example" method="post" action="Form.jsp">
<p> 姓名:<input type="text" name="Name" size="15" maxlength="15"></p>
<p> 密码:<input type="password" name="Password" size="15" maxlength="15"></p>
<p> 性别:<input type="radio" name="Sex" value="Male" checked>男
<input type="radio" name="Sex" value="Female">女</p>
<p> 年龄:
<select name="Old">
<option value="10">10 ~ 20</option>
<option value="20" selected>21 ~ 30</option>
<option value="30">31 ~ 40</option>
<option value="40">41 ~ 65</option>
</select>
</p>
<p> 兴趣:
<input type="checkbox" name="Habit" value="Read">
看书
<input type="checkbox" name="Habit" value="Game">
电玩
<input type="checkbox" name="Habit" value="Travel">
旅游
<input type="checkbox" name="Habit" value="Music">
听音乐
<input type="checkbox" name="Habit" value="Tv">
看电视</p>
<p>
<input type="submit" value="提交">
<input type="reset" value="清除">
</p>
</form>
</body>
</html>
二、处理表单的JSP文件
<%@ page contentType="text/html;charset=GB2312" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>CH9 - Form.jsp</title>
</head>
<body>
<h2>使用 EL、JSTL 处理表单数据</h2>
<fmt:requestEncoding value="GB2312" />
姓名:<c:out value="${param.Name}" default="Nothing" /> <br>
密码:<c:out value=" ${param.Password} " default="Nothing" /><br>
性别:<c:if test="${param.Sex == 'Male'}">男<br></c:if>
<c:if test="${param.Sex == 'Female'}">女<br></c:if>
年龄:<c:choose>
<c:when test="${param.Old == 10}">10 ~ 20<br></c:when>
<c:when test="${param.Old == 20}">21 ~ 30<br></c:when>
<c:when test="${param.Old == 30}">31 ~ 40<br></c:when>
<c:otherwise>41 ~ 65<br></c:otherwise>
</c:choose>
兴趣:<c:forEach items="${paramValues.Habit}" var="habit">
<c:choose>
<c:when test="${habit == 'Read'}"><li>看书1</li></c:when>
<c:when test="${habit == 'Game'}"><li>电玩</li></c:when>
<c:when test="${habit == 'Travel'}"><li>旅游</li></c:when>
<c:when test="${habit == 'Music'}"><li>听音乐</li></c:when>
<c:when test="${habit == 'Tv'}"><li>看电视</li></c:when>
</c:choose>
</c:forEach>
</body>
</html>
取得用户在表单中输入的数据,但在EL中可以使用隐含对象param和paramValues来取得数据。如下所示:
${param.name} ${paramValues.name}
这里param的功能和request.getParameter(String name}相同,而paramValues和request.getParameterValues(String name)相同。
请看例子: 表单(Form.html) <html> <head> <title>Form.html</title>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312"> </head>
<body>
<form name="Example" method="post" action="Form.jsp">
<p> 姓名:<input type="text" name="Name" size="15" maxlength="15"></p>
<p> 密码:<input type="password" name="Password" size="15" maxlength="15"></p>
<p> 性别:<input type="radio" name="Sex" value="Male" checked>男
<input type="radio" name="Sex" value="Female">女</p>
<p> 年龄:
<select name="Old">
<option value="10">10 ~ 20</option>
<option value="20" selected>21 ~ 30</option>
<option value="30">31 ~ 40</option>
<option value="40">41 ~ 65</option>
</select>
</p>
<p> 兴趣:
<input type="checkbox" name="Habit" value="Read">
看书
<input type="checkbox" name="Habit" value="Game">
电玩
<input type="checkbox" name="Habit" value="Travel">
旅游
<input type="checkbox" name="Habit" value="Music">
听音乐
<input type="checkbox" name="Habit" value="Tv">
看电视</p>
<p>
<input type="submit" value="提交">
<input type="reset" value="清除">
</p>
</form>
</body>
</html>
二、处理表单的JSP文件
<%@ page contentType="text/html;charset=GB2312" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>CH9 - Form.jsp</title>
</head>
<body>
<h2>使用 EL、JSTL 处理表单数据</h2>
<fmt:requestEncoding value="GB2312" />
姓名:<c:out value="${param.Name}" default="Nothing" /> <br>
密码:<c:out value=" ${param.Password} " default="Nothing" /><br>
性别:<c:if test="${param.Sex == 'Male'}">男<br></c:if>
<c:if test="${param.Sex == 'Female'}">女<br></c:if>
年龄:<c:choose>
<c:when test="${param.Old == 10}">10 ~ 20<br></c:when>
<c:when test="${param.Old == 20}">21 ~ 30<br></c:when>
<c:when test="${param.Old == 30}">31 ~ 40<br></c:when>
<c:otherwise>41 ~ 65<br></c:otherwise>
</c:choose>
兴趣:<c:forEach items="${paramValues.Habit}" var="habit">
<c:choose>
<c:when test="${habit == 'Read'}"><li>看书1</li></c:when>
<c:when test="${habit == 'Game'}"><li>电玩</li></c:when>
<c:when test="${habit == 'Travel'}"><li>旅游</li></c:when>
<c:when test="${habit == 'Music'}"><li>听音乐</li></c:when>
<c:when test="${habit == 'Tv'}"><li>看电视</li></c:when>
</c:choose>
</c:forEach>
</body>
</html>