HttpServletRequest的使用
当HTTP转发给Web容器处理时,Web容器会收集相关信息,并产生HttpServletRequest对象,使用这个对象可以取得所有HTTP请求中的信息,可以在Servlet中进行处理,也可以转发给其他的Servlet/Jsp处理。
1. 请求信息的取得
getQueryString()方法可以取得HTTP请求的查询字符串。
getParameter()方法能指定请求参数名称来取得相应的值,如取得下面请求参数为name 的值:
http://localhost:8080/WebApp1/hello.do?name=acmagic;
获取到的name 的值就为acmagic,如果请求中没有参数的名称,则返回null.
如果窗口上一个参数名称有多个值,此时可以用getParameterValues()方法取得一个String数组。例如:
http://localhost:8080/WebApp1/hello.do?choose=sans&choose=lily&choose=jack
String[] names = request.getParameterValues("choose");
如果要知道有多少请求参数,可以使用getParameterNames()方法,他会返回一个Enumeration对象,其中包括所有请求参数的名称。例如:
Enumeration<String> e = request.getParameterNames();
while (e.hasMoreElements()) {
String param = (String) e.nextElement();
out.println(param + "<br>");
}
如果要知道浏览器请求的标头,可以用getHeader()方法,也可以使用getHeaderNames()获取浏览器请求的所有标头值。
完整代码:
package com.web.http;
import java.awt.print.Printable;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class GetRequestMessage extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;protected void getRequestMessage(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 传送给浏览器utf-8编码的文字
response.setContentType("text/html;charset=utf-8");
//请求字符编码,如果有中文才不会乱码
request.setCharacterEncoding("utf-8");
PrintWriter output = response.getWriter();
output.println("<html>");
output.println("<head>");
output.println("<title>getRequestMessage</title>");
output.println("</head>");
output.println("<body>");
// 取得应用程序的路径
output.println("<h1>This WebApp at :" + request.getContextPath() + "</h1>");
// 取得请求字符串
output.println("<p>Request string: " + request.getQueryString() + "</p>");
// 取得参数中对应的值
output.println("<p>name = " + request.getParameter("name") + "</p>");
// 一个参数对应几个值
String[] chooses = request.getParameterValues("choose");
output.print("<p>chooses are: " + chooses[1] + "</p>");
// 请求参数的个数
Enumeration<String> e = request.getParameterNames();
while (e.hasMoreElements()) {
String param = (String) e.nextElement();
output.println(param + ":" + request.getParameter(param) + "<br><br>");
}// 获取所有标头名
Enumeration<String> h = request.getHeaderNames();
while (h.hasMoreElements()) {
String head = (String) h.nextElement();
output.println(head + ": " + request.getHeader(head) + "<br>");
}output.println("<body>");
output.println("</html?");
output.close();
}@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
getRequestMessage(request, response);
}@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
getRequestMessage(request, response);
}}</font></font>
运行结果如下:
(未完。。。)
HttpServletRequest的使用的更多相关文章
- java中HttpServletRequest常用获取url、资源名等方法总结
HttpServletRequest客户端获取请求,客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中.所以我们能够从对象中获取相应信息 端口 request.get ...
- eclipse开发servlet,HttpServletRequest报红叉解决方案
eclipse开发servlet,HttpServletRequest报红叉解决方案 今天突然间有兴致,想打一会代码,于是开发一个Servlet,代码和配置路径都没问题,HttpServlet居然报错 ...
- java web学习总结(十) -------------------HttpServletRequest对象
一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...
- java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.isAsyncStarted()Z 的解决
jetty 9 嵌入式开发时,启动正常,但是页面一浏览就报错如下: java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest ...
- The method getDispatcherType() is undefined for the type HttpServletRequest
在使用百度的ueditor的时候,老是报错: The method getDispatcherType() is undefined for the type HttpServletRequest 原 ...
- HttpServletRequest获取请求参数
private static String getRequestParameter(HttpServletRequest request, HttpServletResponse response) ...
- 解决httpServletRequest.getParameter获取不到参数
用httpServletRequest.getParameter接收post请求参数,发送端content Type必须设置为application/x-www-form-urlencoded:否则会 ...
- Struts1.x有两个execute方法,不要重写错哦HttpServletRequest才是对的(转)
Struts1.x 的 Action 有两个 execute 哦,小心搞错! by agate - Published: 2008-05-01 [9:42 下午] - Category: 程序编码 不 ...
- httpServletRequest对象、filter、servlet、servlet容器、catalina、tomcat、以及web容器之间的关系
学习servlet的时候经常感到疑惑 HttpServletRequest是服务器创建的?还是servlet容器创建的? 过滤器是服务器创建的?还是servlet容器创建的? serlet容器和tom ...
- HttpServletRequest的Attribute和Parameter区别
HttpServletRequest类既有getAttribute()方法,也由getParameter()方法,这两个方法有以下的组件通过getParameter()方法来获得请求参数,例如假定we ...
随机推荐
- Java实现Socket通信
一对一通信: 服务器端: public class ServerDemo { public static void main(String[] args) { ServerSocket serverS ...
- 微信小程序网络封装-简单高效
废话引言 小程序虽然出世很久了,但一直没怎么接触到小程序开发.吉他兴趣班老师想弄一个小程序发布课程信息和打卡功能,作为IT一员就自愿加入了这个小程序开发小组中.虽然小程序面向的是前端工程师,但作为移动 ...
- PHP设计模式:观察者模式
示例代码详见https://github.com/52fhy/design_patterns 观察者模式 观察者模式(Observer)是对象的行为模式,又叫发布-订阅(Publish/Subscri ...
- tomcat 启动速度慢背后的真相
1. tomcat 启动慢 在线上环境中,我们经常会遇到类似的问题,就是tomcat 启动比较慢,查看内存和cpu,io都是正常的,但是启动很慢,有的时候长达几分钟,这到底是什么原因导致的. 1.1 ...
- Java-Maven(八):配置远程中央仓库的各种方法
一.远程仓库的配置 在平时的开发中,我们往往不会使用默认的中央仓库,默认的中央仓库访问的速度比较慢,访问的人或许很多,有时候也无法满足我们项目的需求,可能项目需要的某些构件中央仓库中是没有的,而在其他 ...
- HTTPS加密传输数据,加强P2P平台网络安全和信任
12月28日,银监会会同工信部.公安部.网信办等部门研究起草了<网络借贷信息中介机构业务活动管理暂行办法(征求意见稿)>(以下简称<办 法>),并向社会公开征求意见.该< ...
- OAuth 2.0授权之授权码授权
OAuth 2.0 是一个开放的标准协议,允许应用程序访问其它应用的用户授权的数据(如用户名.头像.昵称等).比如使用微信.QQ.支付宝登录等第三方网站,只需要用户点击授权按钮,第三方网站就会获取到用 ...
- Navicat备份、还原mysql数据库
注:本文为原创,转载请附带链接:https://www.cnblogs.com/stm32stm32
- [转]Magento2命令行配置之性能测试生成数据
本文转自:https://blog.csdn.net/xz_src/article/details/72799539 性能测试数据概述 使用Magento性能工具包或其他工具进行性能测试,你必定产生大 ...
- jQuery找到GridView控件ItemTemplate模版内的控件
可以使用下面的方法,jQuery找到GridView控件ItemTemplate模版内的CheckBox: 使用jQuery的find()方法: