1、在jsp中建立表单
<pre>
用户名:<input type="text" name ="uname" />
密码: <input type="password" name ="pwd" />
性别: <input type="radio" name ="sex" value="male"/>男
<input type="radio" name ="sex" value="female"/>女
爱好: <input type="checkbox" name ="hobby" value="1"/>吃饭
<input type="checkbox" name ="hobby" value="2"/>睡觉
<input type="checkbox" name ="hobby" value="3"/>打豆豆
城市: <select name="city">
<option value="1">上海</option>
<option value="2">北京</option>
<option value="3">武汉</option>
</select>
个人简介: <textarea rows =""cols = "" name = "info">
</textarea>
<input type="submit" name ="sub" value="注册"/>
</pre>
2、在servlet中获取表单信息并输出到屏幕
String uname = request.getParameter("uname");
String pwd = request.getParameter("pwd");
String sex = request.getParameter("sex");//选中的单选框的内容
//多选框,拼接选中的内容并删除最后一个逗号
String[] arr = request.getParameterValues("hobby");
StringBuilder hobbys=new StringBuilder();
for(String hobby:arr) {
hobbys.append(hobby+",");
}
hobbys.delete(hobbys.length()-1, hobbys.length());
//单选框和多行的文本
String city = request.getParameter("city");
String info = request.getParameter("info");
//将获取到的表单信息输出到屏幕
response.getWriter().println("用户名:"+uname);
response.getWriter().println("密码:"+pwd);
response.getWriter().println("性别"+sex);
response.getWriter().println("爱好"+hobbys);
response.getWriter().println("城市:"+city);
response.getWriter().println("个人简介:"+info);
网友评论