Request&Response

基本使用

Request:获取Web请求数据

Response:设置响应数据

新建maven web项目,pom.xml文件里导入坐标:


        
            javax.servlet
            javax.servlet-api
            4.0.1
            provided
        
    

    
        
            
                org.apache.tomcat.maven
                tomcat7-maven-plugin
                2.2
                
                    
                    9090
                    
                    /
                
            
        
    

新建一个类:

package org.example;

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

@WebServlet("/demo")
public class demo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //获取request对象 获取请求数据
        String name=req.getParameter("name");//url?name="??"
        //使用response对象,设置响应数据
        resp.setHeader("content-type","text/html;charset=utf-8");
        resp.getWriter().write("

"+name+"访问..

"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("Post.."); } }

启动tomcat服务器,访问:

Request&Response_第1张图片

Request&Response_第2张图片

不同参数,页面产生不同效果。

Request获取请求数据

获取请求行

新建样例:

package org.example;

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

@WebServlet("/req1")
public class req1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      //返回请求方式
        String method= req.getMethod();
        System.out.println("method = " + method);
        //返回虚拟目录(项目访问路径)
        String contextPath=req.getContextPath();
        System.out.println("contextPath = " + contextPath);
        //获取URL(统一资源路径)
        StringBuffer url=req.getRequestURL();
        System.out.println("url.toString() = " + url.toString());
        //获取URI(同一资源标识符)
        String uri= req.getRequestURI();
        System.out.println("uri = " + uri);
        //获取请求参数
        String queryString = req.getQueryString();
        System.out.println("queryString = " + queryString);


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Post..");
    }
}

启动服务器,访问路径添加数据,查看控制台输出:

Request&Response_第3张图片

 获取请求头

在上述代码后添加:

//获取请求头:user-agent:浏览器版本信息
        String agent = req.getHeader("user-agent");
        System.out.println("agent = " + agent);

启动服务器,访问资源,查看控制台输出:

 

获取请求体

编写一个表单:




    
    Title


Request&Response_第4张图片

在上述代码doPost方法下编写:

 @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        获取字符输入流
        BufferedReader br=req.getReader();
        //获取数据
        String line = br.readLine();
        System.out.println(line);

    }

 开启服务器,访问资源,查看控制台信息:

填写表单:

Request&Response_第5张图片

 提交:

Request&Response_第6张图片

控制台:

Request通用方式获取请求参数

新建类:

package org.example;

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


@WebServlet("/req2")
public class req2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("GET..");
        //获取所有Map集合
        Map map=req.getParameterMap();
        for(String key:map.keySet()){
            //
            System.out.print(key+" ");
            String vals[] = map.get(key);
            for(String value: vals){
                System.out.print(value+" ");
            }
            System.out.println();

        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}

启动服务器,访问路径:

查看控制台输出:

GET..
username zs 
password 123 
hobby 2 

 url数据以键值对出现。

编写req.html文件:




    
    Title




吃饭 睡觉

在上述Java代码doGet后面添加:

  //根据key参数
        String[]hobbies=req.getParameterValues("hobby");
        for(String hobby:hobbies){
            System.out.println(hobby);
        }

启动服务器,进入req.html,填写数据:

Request&Response_第7张图片

提交后url:

查看控制台输出:

GET..
username admin 
password abc 
hobby 1 2 
1
2

 将req.html提交方式编程post提交,走doPost逻辑,在上述java代码的doPost方法下编写程序:

 @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("POST..");
        //获取所有Map集合
        Map map=req.getParameterMap();
        for(String key:map.keySet()){
            //
            System.out.print(key+" ");
            String vals[] = map.get(key);
            for(String value: vals){
                System.out.print(value+" ");
            }
            System.out.println();

        }
        System.out.println("**********");
        //根据key参数获取数组
        String[]hobbies=req.getParameterValues("hobby");
        for(String hobby:hobbies){
            System.out.println(hobby);
        }
        System.out.println("**********");
        //根据key参数获取元素
        String passwords = req.getParameter("password");
        System.out.println("passwords = " + passwords);

    }

然后重新启动tomcat服务器,访问页面:

Request&Response_第8张图片

以表单形式提交,没有url信息在其后:

 

查看控制台输出:

POST..
username admin 
password abc 
hobby 1 2 
**********
1
2
**********
passwords = abc

 发现数据已经被接收到,同时也发现doget和dopost的代码内容几户相同,可以通用,就可以在dopost里的直接调用doget方法。

你可能感兴趣的:(Web,前端,java)