HTTP通信协议是客户与服务器之间的一种提交(请求)信息与响应信息(request/respone)的通信协议。
JSP中内置对象request封装了用户提交的信息,该对象方法可以获取用户提交的信息。
客户通常用HTML表单 向服务器某个JSP页面提交信息,表单格式为:
<form action="提交信息的目地页面" method="get | post' name=form>
提交手段,如:
<input type="text" name="ok">
</form>
get方法提交信息会显示在浏览器的地址栏中,post方法不会显示。
</form action="cat.jsp" method="get" name=form>
<input type="text" value="ok" name="boy">
<input type="submit" value="提交" name=submit>
</form>
cat.jsp页面里通过调用request.getParameter(''boy"); 获取提交的文本参数
一个表单的数据提交手段通常包含以下标签:
<input ....>
<select ....></select>
<option ...></option>
<textarea ...></textarea>
1.<input>基本格式
基本格式
<input type="GUI类型" name="名字">
a.文本框:text
<input type="text" name="dog" value="cat" size="12" align="left" maxlength="20">
b.单选框:radio
<input type="radio" name="R" value="a" align="top" checked="java"> A选项
<input type="radio" name="R" value="b" align="top" checked="ok"> B选项
<input type="radio" name="R" value="c" align="top" >C选项
c.复选框:checkbox
<input type="checkbox" name="color_green" value="green" align="top"> Green
<input type="checkbox" name="color_red" value="red" align="bottom"> Red
<input type="checkbox" name="color_blue" value="blue"> Blue
d.口令框:password
<input type="password" name="pws" size=12 maxlength="8"> 登录密码
<br>
<input type="password" name="passwd" size="12" maxlength="8" value="12345678"> 用户密码
e.提交键:submit
为了能把表单提交给服务器,一个页面至少有一个submit
<input type="submit" value="提交" size="12" name=submit>
f.重置键:reset 重置页面
<input type="reset">重置选项
2.select option格式
a.下拉列表
<select name="shuie">
<option value="cat"> 你选择了小狗
<option value="dog"> 你选择了小猫
<option value="600"> n=600
</select>
b.滚动列表 select 指定size属性就成了垂直滚动列表了size是显示多少行值,比如option 有20个,size=5 那么可以看到5行option选项
selected="selected" 属性默认选中项, 不指定此属性 默认无选中项
<select name="sc" size=3>
<option value="1"> 1
<option value="2"> 2
<option value="3"> 3
<option selected="selected" value="4"> 4
<option value="5"> 5
</select>
3.<TextArea>格式
在表单中指定一个能输入多行文本的文本区域
<textarea name="inputstr" rows="5" cols="20">
4.表格<table></table> 表格行里可以包含input类型输入框
<table >
<tr>
<th>元素内容加粗显示</th>
<td>不加粗显示</td>
</tr>
</table>什么
table 的border属性指定表格是否有边框,值是边框的宽度。 bordercolor边框颜色
th td 对齐属性有valign="top" align="left"
<table border="5" bordercolor="green">
<tr width="100">
<td width="10">AC</td>
<td width="20">DC</td>
<td width="10">CC</td>
<td width="20">EC</td>
</tr>
<tr width="100">
<th width="15">BA</th>
<TH width="15">CA</th>
</tr>
</table>
客户访问页面
Demo.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="UTF-8"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="answer.jsp" method="post" name=form>
<p>中国古代诗人李白那个():
<br>
<input type="radio" name="P" value="a"> 唐
<input type="radio" name="P" value="b"> 宋
<input type="radio" name="P" value="c"> 元
<input type="radio" name="P" value="d" checked="checked"> 明
<br>
<p>红楼梦作者:
<br>
<input type="radio" name="R" value="a" checked="checked"> 曹雪芹
<input type="radio" name="R" value="b"> 罗贯中
<input type="radio" name="R" value="c"> 施耐庵
<input type="radio" name="R" value="d"> 吴承恩
<br>
<p>复选框
<br>
<input type="checkbox" name="color_green" value="green" align="top"> Green
<input type="checkbox" name="color_red" value="red" align="bottom" checked="ok"> Red
<input type="checkbox" name="color_blue" value="blue"> Blue
<br>
<p>密码框
<br>
<input type="password" name="pws" size=12 maxlength="8"> 登录密码
<br>
<input type="password" name="passwd" size="12" maxlength="8" value="12345678"> 用户密码
<br>
<p>整数参数
<input type="text" name="val" value="256" >
<br>
<br>
<p>重置
<input type="reset">重置选项
<br>
<p>下拉列表
<br>
<select name="shuie">
<option value="cat"> 你选择了小狗
<option value="dog"> 你选择了小猫
<option value="600"> n=600
</select>
<br>
<p>滚动列表<br>
<select name="sc" size=3>
<option value="1"> 1
<option value="2"> 2
<option value="3"> 3
<option selected="selected" value="4"> 4
<option value="5"> 5
</select>
<br>
<p>文本输入区域
<br>
<textarea name="inputstr" rows="5" cols="20" title="TextTitle">
</textarea>
<br>
<p>表格
<BR>
<table border="5" bordercolor="green">
<tr width="400">
<td valign="top" align="left" width="10">AC</td>
<td valign="bottom" width="20">DC</td>
<td width="10">CC</td>
<td width="20">EC</td>
</tr>
<tr width="100">
<th width="15">BA</th>
<TH width="15">CA</th>
</tr>
<tr width="400">
</tr>
</table>
<input type="submit" value="提交" size="12" name=submit>
</form>
</body>
</html>
answer.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String s1 = request.getParameter("P");
String s2 = request.getParameter("R");
String s3 = request.getParameter("val");
String s4 = request.getParameter("color_green");
String s5 = request.getParameter("color_red");
switch(s1)
{
case "a":
out.print("<br>"+s1);
break;
case "b":
break;
case "c":
break;
case "d":
break;
default:
}
out.print("<br>"+s1);
out.print("<br>"+s2);
out.print("<br> val:"+Integer.valueOf(s3));
out.print("<br> Color:" + s4);
out.print("<br> Color:" + s5);
out.print("<br> "+ request.getParameter("shuie"));
out.print("<br> S"+ request.getParameter("inputstr"));
%>
</body>
</html>