JavaWeb学习笔记--HttpServletRequest、HttpServletResponse对象常用方法

时间:2022-03-04 13:25:34

HttpServletRequest

HttpSession session = request.getSession(true);    //获取会话对象

Cookie[] cookies = request.getCookies();  //获取请求相关的Cookie

 

HttpServletResponse

response.setContentType("text/html;charset=GB2312");  //设置页面格式

response.setContentType("application/pdf");        //设置页面格式为PDF

response.setContentType("application/msword");      //Servlet打开一个word文档

response.setContentType("application/vnd.ms-excel");      //Servlet打开一个excel表

response.setCharacterEncoding("gb2312");  //确保参数信息以汉字编码方式提取

PrintWriter out = response.getWriter();  //获取页面的输出流

 

//字符串作为URL格式编码,其中的某些字符将被十六进制的转义序列进行替换

String encodedUrl = response.encodeURL(contextPath+"/servlet/URLRewriteServer?name=张三&age=27");