/*
* TestServlet.java
*
*
* 功能:Servlet获得参数
*/
package com.jy.sample.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
/**
* Servlet获得参数的方法演示.
* @author JY
*/
public class GetParamServlet extends HttpServlet {
/** SerialVersionUID */
private static final long serialVersionUID =
1711689663622072980L;
/**
* 处理Get请求
* @param req Request
* @param resp Response
* @throws IOException IO异常
*/
@Override
protected void doGet(HttpServletRequest
req,
HttpServletResponse resp) throws
IOException {
// 设置输出的格式
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>HttpRequest获得参数的方法</title>");
out.println("</head>");
out.println("<body>");
// 设置接收参数所用的编码
req.setCharacterEncoding("UTF-8");
// 通过getParameter()获得的参数
out.println("getParameter()获得的参数<br>");
String value = null;
// 获得name="txt"的表单控件的值
value = req.getParameter("text");
out.println("输入框内输入的值:" + value);
out.print("<br>");
// 获得name="chk"的表单控件的值
value = req.getParameter("checkbox");
out.println("多选框选中的值:" + value);
out.print("<br>");
// 获得name="sel"的表单控件的值
value = req.getParameter("select");
out.println("下拉框选中的值:" + value);
out.println("<br>");
out.println("=====================================<br>");
// 通过getParameterValues()获得的参数
out.println("getParameterValues()获得的参数<br>");
// 获得name="txt"的表单控件的值
String[] paramValue = null;
paramValue = req.getParameterValues("text");
if (null != paramValue) {
out.print("输入框内输入的值:");
for (int i = 0; i < paramValue.length; i++) {
out.print(paramValue[i]);
out.print(" ");
}
}
out.println("<br>");
// 获得name="chk"的表单控件的值
paramValue = req.getParameterValues("checkbox");
if (null != paramValue) {
out.print("多选框选中的值:");
for (int i = 0; i < paramValue.length; i++) {
out.print(paramValue[i]);
out.print(" ");
}
}
out.println("<br>");
// 获得name="sel"的表单控件的值
paramValue = req.getParameterValues("select");
if (null != paramValue) {
out.print("下拉框选中的值:");
for (int i = 0; i < paramValue.length; i++) {
out.print(paramValue[i]);
out.print(" ");
}
}
out.println("<br>");
out.println("=====================================<br>");
// 通过getParameterMap()获得的参数
out.println("getParameterMap()获得的参数<br>");
// 获得参数的Map
Map paramMap = req.getParameterMap();
Object o = null;
String[] val = null;
// 循环Map的key
for (Iterator it = paramMap.keySet()
.iterator(); it.hasNext();) {
o = it.next();
out.print(o);
out.print(":");
// 获得key对应的value
val = (String[]) paramMap.get(o);
if (null != val) {
for (int j = 0; j < val.length; j++) {
out.print(val[j]);
out.print(" ");
}
}
}
out.println("<br>");
out.println("=====================================<br>");
// 通过getParameterNames()获得参数的名称
out.println("getParameterNames()获得参数的名称<br>");
// 获得参数名称枚举
Enumeration en =
req.getParameterNames();
for (;en.hasMoreElements();) {
o = en.nextElement();
out.print(o);
out.print(" ");
}
out.println("<br>");
out.println("=====================================<br>");
out.println("</body>");
out.println("</html>");
}
}
|