JAVA_WEB_DAY02---request

# HTTP协议
## 超文本传输协议
1.传输协议:定义了,客户端和服务器端通信时,发送数据格式
客户端发送请求 服务端响应请求
2.特点
    1.基于TCP/IP的高级协议
    2.默认端口是80
    3.基于请求/响应:一次请求对应一次响应
    4.无状态的:每次请求相互独立,不能交互数据
    
3.历史版本
    1. 1.0每次响应都会建立新连接
    2. 1.1复用连接
4. 请求消息数据格式
    1. 请求行
        请求方式
        有7种方式
        常用get 和post get请求参数在请求行中请求的URL有限制不安全 post在请求体中无限制相对安全
        URL路径
         
        请求协议板本
    2. 请求头
        请求头名称:请求头的值
        相当与浏览器告诉服务器的信息
        hast 请求的主机
        user-agent 使用的浏览器版本
        accept-language 可用的语言环境
        referer 告诉服务器我从哪里来
            防盗链
            统计
    3. 请求空行
        空行
    4. 请求体
        get没有请求体
        post
        username=张三

## request
    1. request对象和response对象的原理
        request对象和response对象是由服务器创建我们只是使用
        request是用来获取请求数据
        response是来设置响应消息
    2. request 继承体系结构
        ServletRequest
        继承 
        HttpServletRequest
        实现
        org.apache.catalina.connector.RequestFacade
    3. 功能
        1. 获取请求消息
         1. 获取请求方式
         String getMethod()
         2. 获取虚拟目录
         String getContextPath()
         3. 获取Servlet路径
          String getQueryString()
         5. 获取请求的URI
            String  getRequestURI()
        6. 获取请求URL
            Sting getRequestURL()
        7. 获取协议及版本
            String getProto 
        8. 获取客户机IP地址
            String getRemoteAddr()     
 
//直接new Servlet 文件
package request;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RequestDemo1
 */
@WebServlet("/RequestDemo1")
public class RequestDemo1 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RequestDemo1() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*         1. 获取请求方式
         String getMethod()
         2. 获取虚拟目录
         String getContextPath()
         3. 获取Servlet路径
          String getQueryString()
         5. 获取请求的URI
            String  getRequestURI()
         6. 获取请求URL
            Sting getRequestURL()
         7. 获取协议及版本
            String getProtocol() 
         8. 获取客户机IP地址
            String getRemoteAddr() 
*/
        System.out.println(request.getMethod());
        System.out.println(request.getContextPath());
        System.out.println(request.getQueryString());
        System.out.println(request.getRequestURI());
        System.out.println(request.getRequestURL());
        System.out.println(request.getProtocol());
        System.out.println(request.getRemoteAddr());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

信息: Server startup in 343 ms
GET
/WEB_01
null
/WEB_01/RequestDemo1
http://localhost:8080/WEB_01/RequestDemo1
HTTP/1.1
0:0:0:0:0:0:0:1
        2. 获取请求头数据
        通过请求头名称获取请求头的值
        String getHeader(String name)
        获取所有请求头名称
        String getHeaderNames() 

package request;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RequestDemo2
 */
@WebServlet("/RequestDemo2")
public class RequestDemo2 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RequestDemo2() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//            获取所有请求头名称
//            String getHeaderNames() 
        Enumeration<String> headName = request.getHeaderNames();
//             通过请求头名称获取请求头的值
//            String getHeader(String name)
//        遍历
        while (headName.hasMoreElements()) {
            String    name = headName.nextElement();
            System.out.println(name+"-----"+request.getHeader(name));
            
        }
        
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}


信息: Server startup in 257 ms
host-----localhost:8080
connection-----keep-alive
upgrade-insecure-requests-----1
user-agent-----Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4385.0 Safari/537.36
accept-----text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
sec-fetch-site-----none
sec-fetch-mode-----navigate
sec-fetch-user-----?1
sec-fetch-dest-----document
accept-encoding-----gzip, deflate, br
accept-language-----zh-CN,zh;q=0.9

/*防盗链*/
package request;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RequestDemo2
 */
@WebServlet("/RequestDemo2")
public class RequestDemo2 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RequestDemo2() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String referer = request.getHeader("referer");
        if (referer!=null) {
            if(referer.contains("/RequestDemo2"))
                System.out.println("返回正常");
            else
                System.out.println("返回异常");
            
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}



        3. 获取请求体数据
        *请求体:只有POST请求方式,才有请求体,在请求体中封装了POST请求的请求参数*步骤∶
            1.获取流对象
            BufferedReader getReader():获取字符输入流,只能操作字符数据
            servletInputstream getInputstream()∶获取字节输入流,可以操作所有类型数据本在文件上传知识点后讲解
            2.再从流对象中拿数据
regist.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/WEB_01/requestDemo5" method="post">
<input type="tpxt" placeholder="请输入用户名" name="username">
<br>
<input type="text" placeholder="请输入密码" name="password">
<br>
<input type="submit" value="注册">
</form>
</body>
</html>
requestDemo5.java


import java.io.BufferedReader;
import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class requestDemo5
 */
@WebServlet("/requestDemo5")
public class requestDemo5 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public requestDemo5() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求消息体 --请求参数
        
        // 1.获取字符流
        BufferedReader br = request.getReader();
        String line = null;
        while((line = br.readLine()) != null)
        {
            System.out.println(line);
        }
        
    }

}

运行regist.html 注册

控制台输出 username=admin&password=123
    2.request对象的其他功能
        1.获取请求参数
            1. string getparameter(String name):根据参数名称获取参数值username=zs&password=123

regist2.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/WEB_01/requestDemo6" method="post">
<input type="text" placeholder="请输入用户名" name="username">
<br>
<input type="text" placeholder="请输入密码" name="password">
<br>
<input type="submit" value="注册">
</form>
</body>
</html>



regist3.html
        
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/WEB_01/requestDemo6" method="get">
<input type="text" placeholder="请输入用户名" name="username">
<br>
<input type="text" placeholder="请输入密码" name="password">
<br>
<input type="submit" value="注册">
</form>
</body>
</html>

requestDemo6.java
import java.io.BufferedReader;
import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class requestDemo5
 */
@WebServlet("/requestDemo6")
public class requestDemo6 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public requestDemo6() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //get获取请求参数
        //根据参数名称获取参数值
        String username = request.getParameter("username");
        System.out.println( "get" );
        System.out.println(username) ;
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //post获取请求参数
        //根据参数名称获取参数值
        String username = request.getParameter("username");
        System.out.println( "post" );
        System.out.println(username) ;

        
    }

}

优化


import java.io.BufferedReader;
import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class requestDemo5
 */
@WebServlet("/requestDemo6")
public class requestDemo6 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public requestDemo6() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //get获取请求参数
        //根据参数名称获取参数值
//        String username = request.getParameter("username");
//        System.out.println( "get" );
//        System.out.println(username) ;
        this.doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //post获取请求参数
        //根据参数名称获取参数值
        String username = request.getParameter("username");
        System.out.println( "post" );
        System.out.println(username) ;

        
    }

}

            2. string[] getParametervalues(String name):根据参数名称获取参数值的数组hoby=xx&hobby-gameI
regist4.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/WEB_01/requestDemo6" method="get">
<input type="text" placeholder="请输入用户名" name="username">
<br>
<input type="text" placeholder="请输入密码" name="password">
<br>
<input type="checkbox" name="hobby" value="game" >游戏
<input type="checkbox" name="hobby" value="study" >学习
<br>

<input type="submit" value="注册">
</form>
</body>
</html>


import java.io.BufferedReader;
import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class requestDemo5
 */
@WebServlet("/requestDemo6")
public class requestDemo6 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public requestDemo6() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //get获取请求参数
        //根据参数名称获取参数值
//        String username = request.getParameter("username");
//        System.out.println( "get" );
//        System.out.println(username) ;
        this.doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //post获取请求参数
        //根据参数名称获取参数值
        String username = request.getParameter("username");
        System.out.println(username) ;
        //根据参数名称获取参数值的数组
        String[] hobbies = request.getParameterValues("hobby" ) ;
        for (String hobby : hobbies) {
        System.out.println(hobby);
        }


        
    }

}


            3. Enumeration<string> getParameterNames():获取所有请求的参数名称


import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class requestDemo5
 */
@WebServlet("/requestDemo6")
public class requestDemo6 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public requestDemo6() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //get获取请求参数
        //根据参数名称获取参数值
//        String username = request.getParameter("username");
//        System.out.println( "get" );
//        System.out.println(username) ;
        this.doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //post获取请求参数
        //根据参数名称获取参数值
        String username = request.getParameter("username");
        System.out.println(username) ;
        //根据参数名称获取参数值的数组
        String[] hobbies = request.getParameterValues("hobby" ) ;
        for (String hobby : hobbies) {
        System.out.println(hobby);
        }

        //获取所有请求的参数名称
        Enumeration<String> parameterNames = request.getParameterNames();
        while(parameterNames.hasMoreElements()) {
        String name = parameterNames.nextElement( );
        System.out.println(name) ;
        String value = request.getParameter(name);
        System.out.println(value) ;
        System.out.println("----------------");
        }

        
    }

}

            4. Map<string,string[]>getParameterMap():获取所有参数的map集合


import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class requestDemo5
 */
@WebServlet("/requestDemo6")
public class requestDemo6 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public requestDemo6() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //get获取请求参数
        //根据参数名称获取参数值
//        String username = request.getParameter("username");
//        System.out.println( "get" );
//        System.out.println(username) ;
        this.doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //post获取请求参数
        //根据参数名称获取参数值
        String username = request.getParameter("username");
        System.out.println(username) ;
        //根据参数名称获取参数值的数组
        String[] hobbies = request.getParameterValues("hobby" ) ;
        for (String hobby : hobbies) {
        System.out.println(hobby);
        }
    //获取所有参数的map集合
        Map<String,String[]> parameterMap = request.getParameterMap();
        //遍历
        Set<String> keyset = parameterMap.keySet();
        for (String name : keyset) {
        //获取键获取值
        String[] values = parameterMap. get( name) ;
        System.out.println(name) ;
        for (String value : values) {
        System.out.println(value);
        }
        System.out.println();
        }
        
        
    }

}
乱码问题 获取参数前
//1.设置流的编码
request.setCharacterEncoding( "utf-8");

        2.请求转发
        1,步骤︰
            1,通过request对象获取请求转发器对象∶RequestDispatcher getRequestDispatcher(String path)
            2,使用RequestDispatcher对象来进行转发∶forward(servletRequest request , ServletResponse response)
        2.特点:
            1.浏览器地址栏路释不发生变化
            2.只能转发到当前服务器内部资源中。
            3.转发是一次请求


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/WEB_01/requestDemo8" method="get">
<input type="text" placeholder="请输入用户名" name="username">
<br>
<input type="text" placeholder="请输入密码" name="password">
<br>
<input type="checkbox" name="hobby" value="game" >游戏
<input type="checkbox" name="hobby" value="study" >学习
<br>

<input type="submit" value="注册">
</form>
</body>
</html>



import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class requestDemo5
 */
@WebServlet("/requestDemo8")
public class requestDemo8 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public requestDemo8() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("8被访问");
        //转发到demo9资源
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/requestDemo9");
        requestDispatcher.forward(request, response) ;

    }

}




import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class requestDemo5
 */
@WebServlet("/requestDemo9")
public class requestDemo9 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public requestDemo9() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        this.doPost(request, response);
    }



    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("9被访问");
        
        
    }

}


        3.共享数据
            1.域对象:一个有作用范围的对象,可以在范围内共享数据
            2.request域:代表一次请求的范围,一般用于请求转发的多个资源*享数据* 方法:
                1. void setAttribute(string name,object obj):存储数据
                2. object getAttitude(string name):通过键获取值
                3. void removeAttribute(string name):通过键移除键值对
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/WEB_01/requestDemo8" method="get">
<input type="text" placeholder="请输入用户名" name="username">
<br>
<input type="text" placeholder="请输入密码" name="password">
<br>
<input type="checkbox" name="hobby" value="game" >游戏
<input type="checkbox" name="hobby" value="study" >学习
<br>

<input type="submit" value="注册">
</form>
</body>
</html>



import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class requestDemo5
 */
@WebServlet("/requestDemo8")
public class requestDemo8 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public requestDemo8() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("8被访问");
        //存储数据到request域中
        request.setAttribute("msg","he11o");

        //转发到demo9资源
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/requestDemo9");
        requestDispatcher.forward(request, response) ;

    }

}



import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class requestDemo5
 */
@WebServlet("/requestDemo9")
public class requestDemo9 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public requestDemo9() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        this.doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("9被访问");
        //获取数据
        Object msg = request.getAttribute("msg");
        System.out.println(msg);

        
    }

}



        4获取ServletContext
            1.Servletcontext getservletcontext()


import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class requestDemo5
 */
@WebServlet("/requestDemo10")
public class requestDemo9 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public requestDemo9() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        this.doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = request.getServletContext();
        System.out.println(servletContext);
        
    }

}



5. 响应消息数据格式

 

上一篇:Servlet-HttpServlet对象


下一篇:java错误:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Bu (转)