Request对象介绍(客户端到服务器)

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共享数据,是一个容器。

 

 

 

 

posted on 2018-05-21 16:48  lichangyun  阅读(1210)  评论(0编辑  收藏  举报