码迷,mamicode.com
首页 > Web开发 > 详细

Jsp中的表单

时间:2017-05-01 10:03:20      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:form   UI   pos   表单   www   title   字符串   htm   head   

实例1:

<%@ 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=utf-8">
<title>Insert title here</title>
</head>
<body>
<form id="form1" action="reuset1.jsp" method="post" >
用户名:<br/>
<input type="text" name="username">
<hr/>
性别:<br/>
男:<input type="radio" name="gender" value="男">
女:<input type="radio" name="gender" value="女">
<hr/>
喜欢的颜色:<br/>
红色:<input type="checkbox" name="color" value="红色">
绿色:<input type="checkbox" name="color" value="绿色">
蓝色:<input type="checkbox" name="color" value="蓝色">
<hr/>
来自的国家:<br/>
<select name="country">
	<option value="中国"> 中国</option>
	<option value="美国">美国</option>
	<option value="俄罗斯">俄罗斯</option>
</select>
<hr/>
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
</body>
</html>

 显示如下:

技术分享

处理表单

<%@ 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=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String name = request.getParameter("username");
String[] color = request.getParameterValues("color");
%>
姓名:<%=name %>
<hr/>
喜欢的颜色:<%for(String c : color)
{out.println(c + "");}%>
</body>
</html>

  结果

技术分享

注意获取get的方式中文参数,比较复杂,需要借助于URLDecoder类进行转码,或者重新编码或解码。

<%@ 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=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
String rawName = request.getParameter("username");
//将字符串使用ISO-8859-1分解成字节数组
byte[] rawBytes = rawName.getBytes("ISO-8859-1");
//将字节数组重新解码成字符串
String username = new String(rawBytes,"UTF-8");
%>
原始查询字符串:<%=rawName %><hr/>
重新编码解码的字符串:<%=username %>
</body>
</html>

  结果:

技术分享

 

 

  

Jsp中的表单

标签:form   UI   pos   表单   www   title   字符串   htm   head   

原文地址:http://www.cnblogs.com/zydev/p/6790871.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!