Java_Web------ServletRequest实现
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java_Web------ServletRequest实现相关的知识,希望对你有一定的参考价值。
Java_Web------ServletRequest实现
public interface ServletRequest
Defines an object to provide client request information to a servlet. The
servlet container creates a ServletRequest
object and passes it as
an argument to the servlet‘s service
method.
A ServletRequest
object provides data including parameter name
and values, attributes, and an input stream. Interfaces that extend
ServletRequest
can provide additional protocol-specific data (for
example, HTTP data is provided by HttpServletRequest
.
ServletRequest是一个Java EE 接口,其中HttpServletRequest是其子接口。这两个接口封装了许多处理web请求的相关方法,如获取参数名称Name、Names,通过参数名称获取参数值、获取请求协议、request请求URI、URL等,功能非常强大!下面仅列举自己练习的几个方法,更多方法可以参考Java EE API。
最后总结:今天我发现查看英文文档其实并没有想象中的那么难,只是每看一行需要花额外多一点点的时间而已!这时间很短,但我感觉对于学习阶段的我来说,看英文文档是很有帮助的,个人感觉很爽!不懂可以下载个有道词典嘛!
在一个Servlet中重写一个service()方法 @Override public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { System.out.println("servie......"); //通过request对象获取参数值 String user = request.getParameter("user"); String password = request.getParameter("password"); System.out.println(user+": "+password); //获取当前请求的所有参数名称 Enumeration<String> names = request.getParameterNames(); while(names.hasMoreElements()){ String name = names.nextElement(); System.out.println(name); } //获取指定参数名的所有value String [] values = request.getParameterValues("0"); for(String value : values){ System.out.println(value); } //遍历Map集合中的键值对(key-value) Map<String,String[]> map = request.getParameterMap(); for (Map.Entry<String, String[]> entry : map.entrySet()) { System.out.println("^^"+entry.getKey() + ": "+ Arrays.asList(entry.getValue())); } System.out.println("=================="); System.out.println("协议名称和版本为:"+request.getProtocol()); System.out.println("服务器名"+request.getServerName()+" ,服务器端口号: "+request.getServerPort()); System.out.println("该请求是否是安全的(即https://)"+request.isSecure()); //HttpServletRequest是ServletRequest的一个子接口 HttpServletRequest httpServletRequest = (HttpServletRequest) request; String httpRequestURL =new String(httpServletRequest.getRequestURL()); System.out.println("Request的URL为:"+httpRequestURL); String method = httpServletRequest.getMethod(); System.out.println("Request请求的方式为: "+ method);
本文出自 “11941149” 博客,请务必保留此出处http://11951149.blog.51cto.com/11941149/1845929
以上是关于Java_Web------ServletRequest实现的主要内容,如果未能解决你的问题,请参考以下文章