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标头及其他信息. 方法 ...
随机推荐
- Go-json解码到接口及根据键获取值
Go-json解码到接口及根据键获取值 package main import ( "encoding/json" "fmt" "github.com ...
- Go 语言基础——错误处理
#### 学习目标 掌握错误处理 掌握自定义错误处理 掌握defer关键字的使用 ------ #### 错误处理 GO没有异常处理机制 Go语言引入了一个关于错误处理的标准模式,即error接口,该 ...
- listary的使用心得
1.关键字里面的 web 不仅仅可以打开网页也可以打开电脑上的应用程序. 2.但是呢,有时候虽然 URL 指向了正确的 exe 但是却打不开相应的软件.这是为什么呢?(有其他方法可以打开) 我在这里提 ...
- 简单的JavaScript字符串加密解密
简单的JavaScript字符串加密解密 <div> <input type="text" id="input" autofocus=&quo ...
- python 实现多个线程间消息队列传递,一个简单的列子
#-*-coding:utf8-*-"""Producer and consumer models: 1. There are many producers and co ...
- 宝塔安装Lsky Pro图床教程
欢迎访问我的个人博客皮皮猪:http://www.zhsh666.xyz Lsky Pro图床是一个支持本地.阿里云 OSS.腾讯云 COS.七牛云.又拍云等储存方式的基于PHP的开源图床. 项目主页 ...
- CocosCreator上的游戏(调试)发布到微信小程序
1.下载CocosCreator,微信开发者工具 官网地址:http://www.cocos.com/download 官网下载:https://developers.weixin.qq.com/mi ...
- Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square)
Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square) 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 ...
- 猫眼电影和电影天堂数据csv和mysql存储
字符串常用方法 # 去掉左右空格 'hello world'.strip() # 'hello world' # 按指定字符切割 'hello world'.split(' ') # ['hello' ...
- 探索Asp net core3中的 项目文件、Program.cs和通用host(译)
引言 原文地址 在这篇博客中我将探索一些关于Asp.net core 3.0应用的基础功能--.csproj 项目文件和Program源文件.我将会描述他们从asp.net core 2.X在默认模版 ...
