request对象的方法
request对象封装的是请求的数据,由服务器创建,作为实参传递给Servlet的方法,一个请求对应一个request对象,request对象可以获得请求数据。
1、获取请求行信息
(1)get提交
<body bgcolor="#f5f5dc">
<center>
<h3>登录</h3>
<form action="http://localhost:8080/MyServlet_war_exploded/abc" method="get">
用户名:<input type="text" name="myname" size=""><br>
密 码:<input type="password" name="mypassword" size="" ><br><br>
<input type="reset" value="取消">
<input type="submit" value="登录">
</form> </center>
</body>
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletOutputStream out=response.getOutputStream();
String method= request.getMethod();
System.out.println(method);
String URI=request.getRequestURI();
System.out.println(URI);
StringBuffer URL=request.getRequestURL();
System.out.println(URL);
String path=request.getContextPath();
System.out.println(path);
}

运行结果:

getMethod():获取提交方式
getRequestURI():URI
getRequestURL():URL
getContextPath():项目名称
(2)post提交

2、获取请求头信息
(1)获取请求头的一条信息:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String header=request.getHeader("Host");
System.out.println(header);
}

(2)获取请求头的所有信息:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Enumeration<String> headerNames=request.getHeaderNames();//获取键的名字
while(headerNames.hasMoreElements()){
String headerName= headerNames.nextElement();
String headerValue=request.getHeader(headerName);
System.out.println(headerName+":"+headerValue);
}
}


3、获取用户信息
(1)get提交:
<center>
<h3>登录</h3>
<form action="http://localhost:8080/MyServlet_war_exploded/abc" method="get">
用户名:<input type="text" name="userName" size=""><br>
密 码:<input type="password" name="password" size="" ><br><br>
<input type="reset" value="取消">
<input type="submit" value="登录">
</form> </center>
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name=request.getParameter("userName");
String password=request.getParameter("password");
System.out.println(name+":"+password);
}


(2)post提交:


post提交与get提交的运行结果相同。
4、对用户提交的数据的同键不同值的处理
(1)获取提交数据的值:
<form action="http://localhost:8080/MyServlet_war_exploded/abc" method="post">
<input type="checkbox" name="book" value="xiangzi">《骆驼祥子》<br>
<input type="checkbox" name="book" value="xiyou">《西游记》<br>
<input type="checkbox" name="book" value="shuihu">《水浒传》<br>
<input type="checkbox" name="book" value="hongloumemg">《红楼梦》<br>
<input type="checkbox" name="book" value="sanguo">《三国演义》<br>
<input type="reset" value="取消">
<input type="submit" value="确定">
</form>


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String[] books= request.getParameterValues("book");
if(books!=null){
for(String shu:books){
System.out.println(shu);
}
}
}
(2)获取名字:
<center>
<h3>您喜欢的书有哪些:</h3>
<form action="http://localhost:8080/MyServlet_war_exploded/abc" method="post">
<input type="checkbox" name="book1" value="xiangzi">《骆驼祥子》<br>
<input type="checkbox" name="book2" value="xiyou">《西游记》<br>
<input type="checkbox" name="book3" value="shuihu">《水浒传》<br>
<input type="checkbox" name="book4" value="hongloumemg">《红楼梦》<br>
<input type="checkbox" name="book5" value="sanguo">《三国演义》<br>
<input type="reset" value="取消">
<input type="submit" value="确定">
</form>
</center>
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Enumeration<String> names=request.getParameterNames();
while(names.hasMoreElements()){
System.out.println(names.nextElement());
}
}


(3) 获取键和值:
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<String,String[]> map=request.getParameterMap();
for(Map.Entry<String,String[]> entry:map.entrySet()){
System.out.println(entry.getKey());
for(String str:entry.getValue()){
System.out.println(str);
}
}
}


request对象的方法的更多相关文章
- request对象的方法及其参数的传递
先设计一个简单的登录界面index.htm: <html><head><title>request的使用</title></head>< ...
- day65 request对象,以及方法,response对象,render,redirect
这里的都是我们会频繁使用到的,用得多了自然就会了,我们写项目都是少不了这些用法的,所以这就把老师的博客粘过来就好了, Request对象 官方文档 属性 所有的属性应该被认为是只读的,除非另有说明. ...
- request对象多种方法封装表单数据
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, ...
- java webservice服务器端获取request对象的三种方式
有的时候在webservice里我们需要获取request对象和response对象,比如想要获得客户端的访问ip的时候就需要这么做,下面说三种方式,当然三种方式可能是针对不同方式部署webservi ...
- webservice服务器端获取request对象的三种方式
有的时候在webservice里我们需要获取request对象和response对象,比如想要获得客户端的访问ip的时候就需要这么做,下面说三种方式,当然三种方式可能是针对不同方式部署webservi ...
- Request对象与Response对象
1.Request对象 Request对象是来获取请求消息的,是由服务器(Tomcat)创建的. Request对象继承体系结构: ServletRequest -- 接口 ...
- Request对象的主要方法
setAttribute(String name,Object):设置名字为name的request的參数值 getAttribute(String name):返回由name指定的属性值 getAt ...
- Java面试题之Request对象的主要方法
setAttribute(String name,Object):设置名字为name的request的参数值 getAttribute(String name):返回由name指定的属性值 getAt ...
- JAVA-JSP内置对象之request对象的其他方法
相关资料:<21天学通Java Web开发> request对象的其他方法1.request对象除了可以用来获得请求参数,还可以用来获得HTTP标头及其他信息. 方法 ...
随机推荐
- Spring boot中的 JsonConverter
上图是spring-web包的部分目录 为了找出调用了哪种类型的Convert 我找到父类 HttpMessageConverter 查看了它的引用 在spring-boot-autoconfigur ...
- Kali Linux无法访问网络的问题
首先 ping www.baidu.com ping: unkown host www.baidu.com 然后 ping 8.8.8.8 connect:network is unreachable ...
- Mac查看及清理QQ、微信之前下载的图片、视频或DB等
之前写过一篇清理Mac空间的文章: Mac系统清理.占用空间大.空间不够.查看系统文件大小分布 其实这篇文章不是太全,有些资源还是清理不彻底,正好前段时间需要找微信下载的资源,其实可以算作空间清理的续 ...
- IIS配置后本地访问正常,但外网无法访问
很久没有部署IIS网站项目了,都有些手生了,这不今天就遇到了问题.首先确定的是,我的网站配置没有问题,因为内网访问正常.内网访问情况如下: 但是外网访问时确是这样的: 怎么回事儿呢?我就想是不是防火墙 ...
- 900E关于导航站
--------------------------以下更新于20190826------------------------- 作用: 导航站为方便网址收藏之用,收录一些常用的网站,目前主要以本科常 ...
- 搭建SFTP服务器,允许一个或多个用户拥有一个或多个目录的rwx权限
1.引言 sftp可以为传输文件提供一种安全的网络的加密方法.sftp 与 ftp 有着几乎一样的语法和功能.SFTP 为 SSH的其中一部分,是一种传输档案至 Blogger 伺服器的安全方式.其实 ...
- junit单元测试框架
一般我们写代码总想对方法测试一下结果,就存在这些问题: 1.如果方法需要测试,都需要在main方法上调用 2.目前的结果都需要我们人工对比 所以就需要用到 junit 进行测试: 1·下载 junit ...
- Android Studio启动模拟器失败
启动Android Studio的模拟器报“Emulator: Process finished with exit code -1073741819 (0xC0000005)”错误教程: 1.进入该 ...
- Java NIO系列之[说在前面的话]
在开始这个系列文章之前,先聊一些题外话,说说我为什么要写Java NIO这个系列技术文章(不看完会错失一个亿的),因为Java NIO并不像JVM,中间件源码那么有吸引力,但这个技术点是java的基础 ...
- JAVA解除tomcat 对浏览器特别字符 | () {} [] 的限制
1.打开tomcat本机地址打开conf文件夹(一定要关闭Tomcat启动在修改) 2.记事本打开或者编辑软件打开(我这里推荐的编辑软件是以下如图) 打开进去加入这两行代码 3打开server.xml ...
