Servlet中的一些基本方法

获取servlet配置信息(需要在web.xml中进行配置)

getServletConfig()
然后getInitParameter()
获getInitParameterNames()
然后就可以获取web.xml中的配置信息了在这里插入图片描述

获取ServletContext对象

1.getServletConfig().getServletContext();
2.getServletContext();

  • ServletContext对象中的方法
  • 1.setAttribute
  • 2.getAttribute
  • 3.removeAttribute
  • 域对象的作用:
  • 1.存值取值
  • 2.进行传值
  • 3.可以获取全局配置信息 web.xml
  • 4.可以获取项目中所有资源的在TomCat上的绝对路径 getRealPath
  • 5.可以进行请求转发
    可以在一个servlet进行setAttribute,然后在另一个servlet中getAttribute获取属性

3.获取服务器上的真实路径
String realPath1 = application.getRealPath("/WEB-INF/classes/a.properties");

4.利用Context域进行转发

        ServletContext application = getServletContext();
        // 获取请求转发器     参数:要转发到的路径 
        // 注意:请求转发只能转发站内的路径 并且传入的地址相对于工程的
        /*
         * 请求转发注意
         * 1.请求转发用户只发送了一次请求
         * 2.网址没有发生变化(用户并不知道内部你怎么操作)
         * 3.只能转发站内
         */
        RequestDispatcher dispatcher = application.getRequestDispatcher("/demo07");
        // 转发请求
        dispatcher.forward(request, response);

响应(response)

/*
 * 响应(response)响应回浏览器(用户)
 * 响应行
 *      响应状态码 200(成功) 302(重定向) http协议1.1
 * 响应头
 *      告诉浏览器 我要做什么
 * 响应体
 *      响应的内容
 */
public class Demo08 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TomCat默认编码格式 Tomcat9 之前 是ISO-8859-1
        // response.setCharacterEncoding("UTF-8");
        // 设置响应头告诉浏览器以什么编码格式来解析响应
        // response.setHeader("Content-Type", "text/html;charset=UTF-8");
        
        // 相当于上面两句二合一
        response.setContentType("text/html;charset=UTF-8");
        // 利用response获取 字符流 和 字节流
        PrintWriter writer = response.getWriter();
        writer.write("你好");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

下载图片

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 下载图片
        // 获取图片在服务器上的真实路径
        ServletContext application = getServletContext();
        String realPath = application.getRealPath("/WEB-INF/classes/哈哈.png");
        // 通过file类获取文件名
        File file = new File(realPath);
        String fileName = file.getName();
        // 需要设置图片名的编码格式iso-8859-1
        
        fileName = new String(fileName.getBytes(), "iso-8859-1");
        // 通过设置响应头来告诉浏览器 我给你的资源 下载用
        response.setHeader("content-disposition", "attachment;filename=" + fileName);
        // 设置下载内容的格式(去TomCat下的config中的web.xml查找资源格式)
        response.setHeader("Content-Type", "image/png");
        // 读取图片 字节流
        FileInputStream fis = new FileInputStream(realPath);
        ServletOutputStream outputStream = response.getOutputStream();
        // 使用响应中的字节流 将图片写回浏览器
        byte[] b = new byte[1024];
        int len;
        while ((len = fis.read(b)) != -1) {
            outputStream.write(b, 0, len);
        }
        fis.close();
    }

response重定向

    private void chongdingxiang(HttpServletResponse response) {
        System.out.println("你好1");
        // 通过响应response进行请求重定向
        // 可以进行站内重定向  相当于8080后的斜杠
        // 也可以进行站外重定向
        response.setHeader("location", "/xx/demo11");
        // 需要添加重定向的状态码
        response.setStatus(302);
        /*
         * 注意
         * 1.重定向 发送了两次请求(网址变了)
         * 2.重定向 会执行完第一次请求的方法 在进行第二次请求
         */
        System.out.println("成了");
    }

request

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取请求方式 get/post GET
        String method = request.getMethod();
        System.out.println(method);
        // 获取用户请求的url(统一资源定位符) http://localhost:8080/xx/demo12
        StringBuffer requestURL = request.getRequestURL();
        System.out.println(requestURL);
        // 获取用户请求的uri /xx/demo12
        String requestURI = request.getRequestURI();
        System.out.println(requestURI);
        // 获取相对路径 /xx
        String contextPath = request.getContextPath();
        System.out.println(contextPath);
        //http://localhost:8080/xx/demo12?username=wl&password=123
        // 获取用户请求的参数    参数相当于key
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username);
        System.out.println(password);
    }

猜你喜欢

转载自blog.csdn.net/qq_37113621/article/details/82989307