关闭

获取请求参数

140人阅读 评论(0) 收藏 举报
分类:
package servlet;

import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AServlet extends HttpServlet {

	/**
	 *获取请求参数
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("Get:"+request.getParameter("xxx"));
		System.out.println("Get:"+request.getParameter("yyy"));
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String user=request.getParameter("username");
		String psw=request.getParameter("psw");
		System.out.println("username:"+user);
		System.out.println("passward:"+psw);
		
		String [] str=request.getParameterValues("hobby"); //获取多值参数
		System.out.println("hobby:"+Arrays.toString(str));
		
		Enumeration names=request.getParameterNames(); //获取参数名称
		while(names.hasMoreElements())
		{
			System.out.println(names.nextElement());
		}
		
		Map <String ,String []> map=request.getParameterMap();  //获取键值对
		for(String name:map.keySet()){
			String []value=map.get(name);
			System.out.println(name+":"+Arrays.toString(value));
		}
	}

}
<pre name="code" class="html">>
    	<h1>测试请求参数</h1>
		<a href="/day1119_1/AServlet?xxx=XXX&yyy=YYY">点击这里</a>
		<form action="/day1119_1/AServlet" method="post">
			用户名:<input type="text" name="username"/><br />
			密 码:<input type="password" name="psw" /> <br />
			爱 好:<input type="checkbox" name="hobby" name="cf" />吃饭
			     <input type="checkbox" name="hobby" name="sj" />睡觉
			      <input type="checkbox" name="hobby" name="dddd" />打豆豆<br />
				 <input type="submit"  value="提交" />
		</form>
  </body>
</html>


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:87360次
    • 积分:3212
    • 等级:
    • 排名:第10621名
    • 原创:237篇
    • 转载:31篇
    • 译文:0篇
    • 评论:3条
    最新评论