请求对象.响应

    科技2024-12-16  13

    Request

    请求对象的常用方法-

    获取各种路径:

    返回值方法名说明StringgetContextPath()获取虚拟目录名称StringgetServletPath()获取Servlet映射路径StringgetRemoteAddr()获取访问者ip地址StringgetQueryString()获取请求的消息数据StringgetRequestURI()获取统一资源标识符 比URL大StringBuffergetRequestURL()获取统一资源定位符 @WebServlet("/ServletDemo01") public class ServletDemo01 extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1.获取虚拟目录名称 /req String contextPath = req.getContextPath(); System.out.println(contextPath); //2.获取Servlet映射路径 /ServletDemo01 String servletPath = req.getServletPath(); System.out.println(servletPath); //3.获取访问者ip地址 0:0:0:0:0:0:0:1 String remoteAddr = req.getRemoteAddr(); System.out.println(remoteAddr); //4.获取请求的消息数据 null 并没有输入信息 所以是空 需要自己切割 淘汰 String queryString = req.getQueryString(); System.out.println(queryString); //5.获取统一资源标识符 /req/ServletDemo01 大 String requestURI = req.getRequestURI(); System.out.println(requestURI); //6.获取统一资源定位符 http://localhost:8080/req/ServletDemo01 StringBuffer requestURL = req.getRequestURL(); System.out.println(requestURL); } }

    获取请求头信息:

    返回值方法名说明StringgetHeader(String name)根据请求头获取一个值,键不区分大小写EnumerationgetHeaders(String name)根据请求头名称获取多个值EnumerationgetHeaderNames()获取所有请求头名称 String connection = req.getHeader("connection"); System.out.println(connection);

    获取求情参数信息:

    返回值方法名说明Map<String,String[]>getParameterMap()返回所有参数的键值对EnumerationgetParameterNamer()获取所有名称StringgetParameter(String name)根据名称获取数据 一个键对应一个值String[]getParameterValues(String name)获取所有数据 一个键对应多个值 req.方法名 @WebServlet("/ServletDemo3") public class ServletDemo3 extends HttpServlet { //http://localhost:8080/req/ServletDemo3?username=aaa&&hobby=game&hobby=music @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1.获取虚拟路径 /req String contextPath = req.getContextPath(); System.out.println(contextPath); //2.获取uri和url String requestURI = req.getRequestURI(); StringBuffer requestURL = req.getRequestURL(); //3.获取请求头 //Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36 String header = req.getHeader("user-agent"); System.out.println(header); //4.获取请求体POST封装的是POST的参数部分 username:[aaa] String username = req.getParameter("username"); System.out.println(username); //[game, music] String[] hobbies = req.getParameterValues("hobby"); System.out.println(Arrays.toString(hobbies)); //hobby:[game, music] Map<String, String[]> map = req.getParameterMap(); Set<String> keys = map.keySet(); for (String key : keys) { String[] values = map.get(key); System.out.println(key+":"+Arrays.toString(values)); } } }

    封装数据相关操作:

    Beanutils:和新方法populate(要封装的对象,map集合数据)

    设置编码(乱码问题):

    req.setCharacterEncoding("UTF-8");//中文乱码

    作为域对象共享数据:作用范围小 一次请求

    getAttribute

    setAttribute

    removeAttribute

    请求转发:

    转发是一次请求,才可以使用request域共享数据

    forward()

    req.setAttribute("aaa","123"); req.getRequestDispatcher("ServletDemo6").forward(req,resp);//获取请求调度对象 Object aaa = req.getAttribute("aaa"); System.out.println(aaa);

    请求包含:

    include()

    Response:

    操作响应行:

    resp.setStatus(int 状态码)

    字节流响应消息:

    getOutPutStream():响应字节输出流对象

    //设置浏览器的编码格式 resp.setContentType("text/html;charset=UTF-8"); //1.获取字节输出流对象 ServletOutPutStream os=resp.getOutPutStream(); //2.定义一个消息 String str = "你好"; //3.通过字节流对象输出 os.write(str.getBytes("UTF-8"));

    字符流响应消息:

    getWriter():获取响应字符输出流对象

    //设置浏览器的编码格式 resp.setContentType("text/html;charset=UTF-8"); //1.获取字符输出流对象 默认编码 iSO-8859-1 这个编码不支持中文 PrintWriter pw=resp.getWriter(); //2.定义一个消息 String str = "你好"; //3.同过字符流输出 pw.writer(str);

    字符乱码问题:

    //设置浏览器的编码格式 resp.setContentType(“text/html;charset=UTF-8”);

    Processed: 0.019, SQL: 8