1.处理请求和响应的过程request,response,关于request可以从三个方面着手学习。1:如何获取请求头  行  体   2:请求中文处理     3:请求对象的其它常用方法

1.1:request常用方法:

package com.test;

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 UserServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // request.getMethod()方法
System.out.println("方法:"+request.getMethod()); // 获取请求的路径URI(统一资源标识符)和URL(同一资源定位符)
System.out.println("URI:"+request.getRequestURI());
System.out.println("URL:"+request.getRequestURL()); // 获取请求的协议类型
System.out.println("协议:"+request.getProtocol()); // 获取IP地址
System.out.println("IP地址:"+request.getRemoteAddr()); // 获取端口号
System.out.println("服务器端口号:"+request.getLocalPort()); // 获取请求头 头信息都可以获取到
System.out.println("请求头名称:"+request.getHeader("Accept"));
System.out.println("请求头名称:"+request.getHeader("Accept-Language"));
System.out.println(); // 获取所有请求头信息
Enumeration<String> headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()){
String element = headerNames.nextElement();
System.out.println(element+" "+request.getHeader(element));
}
} public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response);
} }

结果:

1.2:request请求中文处理

GET乱码解决:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body> <h1>GET方式</h1>
<form method="GET" action="servlet/UsersServlet">
帐号:<input type="text" name="username" /><br />
<input type="submit"value="提交" />
</form> </body>
</html>

Servlet:

package com.test;

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class UsersServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 解决GET乱码
String name = request.getParameter("username"); name = new String(name.getBytes("iso-8859-1"),"utf-8"); System.out.println(name); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response);
} }

POST乱码解决:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<h1>POST方式</h1>
<form method="POST" action="servlet/UsersServlet">
帐号:<input type="text" name="username" /><br />
<input type="submit"value="提交" />
</form>
</body>
</html>

Servlet:

package com.test;

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class UsersServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 解决POST乱码
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("username"); System.out.println(name); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response);
} }

总结要记住得内容:

1 处理get请求乱码  String string = new String(parameter.getBytes("iso-8859-1"),"utf-8");

2 处理post请求乱码     request.setCharacterEncoding("utf-8");

Request容器(存取删)(域对象)和请求转发

总结:

当一个Web资源收到客户端的请求后,如果希望服务器通知另外一个资源处理.

可以通过 转发对象 RequestDispatcher 对象的forward(request,response)方法,将当前请求传递给其他的Web资源进行处理,这种方式称为请求转发。

注:在这些转发的过程中,所有的Servlet共享同一个请求对象。在转发中,客户端是感觉不到服务器内部在跳转。而且客户端的浏览器的地址栏中是不会发生任何变化的。

因为在多个Servlet中可以进行转发,导致多个Servlet之间共享同一个request对象,于是在发出转发的Servlet中,可以把request对象当做一个容器,然后给其中保存数据,在其他的Servlet中可以取出前面的Servlet给request对象中保存的数据。request对象如果当作容器的话,它只是在当前请求中有效。当请求响应结束了,这个容器就消失了。

转发得小结:

1 转发可以调用其他得servlet程序

2 转发可以获取保密路径(WEB-INF)下得资源

另:

1 使用request对象,可以获取请求行

2 使用request对象,可以获取请求头

3 使用request对象,可以处理中文乱码

4 使用request对象,调用下一个servlet(请求转发)

5 使用request对象,在一个请求转发过程中,让两个servlet共享数据,是一个容器。

最新文章

  1. (转)sqlplus中文显示乱码的问题
  2. php 字符串转数组
  3. Hive On Spark hiveserver2方式使用
  4. winform中利用反射实现泛型数据访问对象基类(2)
  5. PHP Mail 简介
  6. MySQL 调优基础:Linux内存管理 Linux文件系统 Linux 磁盘IO Linux网络
  7. 模板类之间的友元关系实现Blob和BlobPtr
  8. wpf中,一个简单的自定义treeview
  9. Struts国际化
  10. iOS开发——工厂模式
  11. AdaBoost 算法原理及推导
  12. 记住 Python 变量类型的三种方式
  13. JDBC的批处理操作三种方式
  14. Linux入门之常用命令(13) crontab
  15. vue 单文件组件中样式加载
  16. [Swift]LeetCode653. 两数之和 IV - 输入 BST | Two Sum IV - Input is a BST
  17. Python 3 Anaconda 下爬虫学习与爬虫实践 (2)
  18. Session小结
  19. 谷歌搜索技巧(转)https://www.runningcheese.com/google
  20. RT-thread内核对象--事件集

热门文章

  1. 猜数字游戏-python
  2. 如何修改vs2005/vs2010的tfs的登录名和密码 .
  3. iOS开发常见问题(不断更新)
  4. zxing 二维码扫描 配置和使用
  5. CodeIgniter框架——函数(时间函数、装载函数)剖析+小知识点
  6. JavaScript Array 对象(length)方法 (contact、push,pop,join,map、reverse、slice、sort)
  7. diy 重要的不是这个工具怎么解决了问题
  8. Vuejs2.0 cnpm 安装脚手架项目模板
  9. js验证表单大全3
  10. javascript实例:点亮灯泡