获取请求参数

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>


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fandoudou123/article/details/49924397
个人分类: JAVA web
上一篇获取请求信息——Request对象的方法
下一篇cookie的保存和获取
想对作者说点什么? 我来说一句

netty springmvc dubbo 集成实例

2017年01月07日 414KB 下载

没有更多推荐了,返回首页

关闭
关闭