package cn.itcast.chapter04.request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class RequestParamsServlet
*/
@WebServlet("/RequestParamsServlet")
public class RequestParamsServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RequestParamsServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
String name=request.getParameter("username");
name=new String(name.getBytes("iso8859-1"),"utf-8");
String password=request.getParameter("password");
System.out.println("用户名:"+name);
System.out.println("密 码:"+password);
//获取参数名为“hobby”的值
String[] hobbys=request.getParameterValues("hobby");
System.out.print("爱好:");
for(int i=0;i<hobbys.length;i++){
System.out.print(hobbys[i]+",");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request,response);
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="/javaweb/RequestParamsServlet" mehtod="POST">
用户名:<input type="text" name="username"><br/>
密 码:<input type="password" name="password">
<br />
爱好:
<input type="checkbox" name="hobby" value="sing">唱歌
<input type="checkbox" name="hobby" value="dance">跳舞
<input type="checkbox" name="hobby" value="football">足球<br />
<input type="submit" value="提交">
</form>
</body>