HttpServletRequest 中的方法

    xiaoxiao2021-03-25  129

    HttpServletRequest 中的方法

    request.getContextPath();与this.getServletContext().getContextPath()获取一样//返回值为字符串类型,获取当前系统路径

    request.getHeaderNames();  //返回值为 请求头的所有名字。【在取出里面的数据时,使用while()来遍历,foreach来遍历该数据的方法好像不行】

    request.getHeader("请求头名字"); //返回名字为请求头名字的值

    request.getIntHeader(“请求头名字”);// 返回名字为请求头名字的值【只能用于请求头名字的值为整形,否则会出现错误】

    request.getProtocol();//返回文本传输的协议

    request.getQueryString(); //返回的是拼接在查询URL后面的值【?后面的值(value)】

    request.getParameter("要查询值的名字");  //返回的拼接在查询url后面的一个值.用于web客户端与服务器之间,通过容器获得get、post等方式传入的数据

    request.setAttribute(String name,String value);和request.getAttribute(Stingname); //用于web容器内部

    request.setStatus(404)//不改变当前的界面

    request.sendError(404) //这个让界面跳转到一个默认的错误界面

    request.setHeader(String name,String value);//修改头信息

    request.setHeader(“refresh”, “3;url=..”);//刷新界面,3秒后跳转到url界面

    重定向

          Response.setStatus(302);

          Response.setHeader(“location”,Stringurl);//若使用此方法重定向,则必须有上面的语句,否则就会失败

    resp.sendRedirect(String url);//重定向,只需要这一句

     

    this.getServletContext().getRequestDispatcher("/Servlet1").forward(request,response)//转发

    若要获取context-param中的内容使用getInitParamrter获取;

    this.getServletContext().getInitParameter("name");  //【注】该方法无法获取SetAttribute(Stringname,String value),该方法只能使用getAttribute(String name)来获取value值

    cookie 

    获取cookie值,首先使用

    Cookie [] cookies = request.getCookies();

    方法,获取所有的cookie

    在通过cc.getName()+",,,"+cc.getValue()获取cookie的name和value

    转载请注明原文地址: https://ju.6miu.com/read-4025.html

    最新回复(0)