<%@ 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 action="/WEB06/RegisterServlet" method="get"><!--或者post-->
用户:<input type="text" name="username" value="请输入你的账号"><br>
密码:<input type="password" name="pwd" ><br>
性别:男<input type="radio" name="ssex" value="nan">女<input type="radio" name="ssex" value="nv"><br>
爱好:篮球<input type="checkbox" name=hobby value="lan"> 足球 <input type="checkbox" name=hobby value="zu">
乒乓球<input type="checkbox" name=hobby value="pin"><br>
<input type="submit" value="注册 ">
</form>
</body>
</html>
package com.oracle.demo01;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RegisterServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 解决post请求乱码(推荐)
// request.setCharacterEncoding("UTF-8");
// 获取请求参数
String uname=request.getParameter("username");
// 解决get请求
uname=new String(uname.getBytes("ISO-8859-1"),"UTF-8");
System.out.println(uname);
// 获取多个参数
String[] hobbys=request.getParameterValues("hobby");
for(String h:hobbys){
System.out.println(h);
}
String[] hobb=request.getParameterValues("ssex");
for(String hh:hobb){
System.out.println(hh);
}
// 获取所有参数所在的Map集合
Map<String,String[]>map=request.getParameterMap();
System.out.println("--------------");
// 获取所有key所在的Set集合
Set<String>keys=map.keySet();
// 遍历取到每一个key值
for(String key:keys){
String [] values=map.get(key);
System.out.println(key+":");
for(String value:values){
System.out.print(value);
}
System.out.println();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}