Tomcat之Servlet设置及请求

Servlet 设置及请求

获取配置信息

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取config对象
        ServletConfig config = this.getServletConfig();
        // 取出servlet配置信息
        String value = config.getInitParameter("username");
        System.out.println(value);

        // 获取context域对象
        ServletContext application = this.getServletContext();
        // 获取全局配置信息
        String value = application.getInitParameter("key");
        System.out.println(value);
}

获取服务器上文件的路径并读取

使用Context 域对象获取

可以获取到服务器上的任意资源路径

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
    ServletContext application = this.getServletContext();
    // path 获取的是服务器上的真实路径 (绝对路径)
    String path = application.getRealPath("/WEB-INF/ xxx (文件路径)");
    // 读取文件 使用 properties
    Properties properties = new Properties();
    properties.load(new FileInputStream(path));
    System.out.println(properties.getProperty("key"));
}

请求转发

注意: 浏览器只是发起一次请求

Servlet 请求转发 在内部操作

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("开始");
        // 获取context域对象
        ServletContext application = this.getServletContext();
        // 从context域中 获取请求转发器
        RequestDispatcher dispatcher = application.getRequestDispatcher("/demo04");
        // 进行请求转发
        dispatcher.forward(request, response);
        System.out.println("结束");
}

public class Demo04 extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("我是demo04 已被执行");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

这里写图片描述

响应对象

HttpServletResponse 服务的响应对象

  1. 响应行 http/1.1 状态码 200
  2. 响应头 告知浏览器的操作 例如响应文件需要下载 以什么编码格式解析数据
  3. 响应体 响应回浏览器的数据
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // 设置服务器的编码格式
    // 默认tomcat编码格式是 iso-8859-1
/*    resp.setCharacterEncoding("UTF-8");   */
    // 告诉浏览器使用什么编码格式查看
    // 添加响应头
/*    resp.setHeader("Content-type", "text/html;charset=utf-8");   */
    // setContentType 代替以上两种方法
    resp.setContentType("text/html;charset=utf-8");

    // 从响应对象HttpServletResponse中获取
    PrintWriter out = resp.getWriter();
    out.write("打印成功");

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

通过访问Servlet 下载文件

用户发送请求 →

请求到访问Servlet →

Servlet 处理请求 →

把服务器上的文件 以流的形式 使用response 响应用户

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // 获取服务器上的文件路径
    String realPath = this.getServletContext().getRealPath("文件路径");
    // 使用字符串切割 获取文件的名字
    int index = realPath.lastIndexOf("/");
    String filename = realPath.substring(index + 1);
    // 需要修改文件名字的字符集
    filename = new String(filename.getBytes, "iso-8859-1");
    // 添加响应头 需要拼接的名字
    resp.setHeader("content-disposition", "attachment;filename=" + filename);
    // 告知浏览器文件的下载格式
    resp.setHeader("content-type", "image/ 文件格式 ");
    // 从服务器中读取文件
    FileInputStream fis = new FileInputStream(realPath);
    // 需要获取response中的 字节流
    ServletOutputStream sos = resp.getOutputStream();
    // 进行读写
    int len = 0;
    byte[] b = new byte[1024];
    while((len = fis.read(b)) != -1){
        sos.write(b, 0, len);
    }
    // 关闭流
    fis.close();
}

请求重定向

浏览器发起请求(请求Servlet)

Servlet给浏览器一个响应

在响应中会携带一个重定向响应头(头中有重定向的访问地址)

浏览器接到这个响应后 发现重定向头 再一次发起请求 去访问重定向头中的地址

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // 通过添加请求头的方法 请求重定向 
    // 添加重定向响应头
    // 注意: 添加头信息请求地址时候 需要写明工程名
    response.setHeader("location", "/详细路径");
    response.setStatus(302);
}

这里写图片描述

请求重定向和请求转发的区别

请求重定向是发起的两次请求(请求地址发生了变化)

请求转发只是一次请求

响应时要注意的细节

从response中获取的字符流和字节流 在同一个servlet中不能同时使用

HttpServletRequst 请求

请求行 请求头 请求体

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // 获取请求网址
    System.out.println(request.getRequestURL());
    System.out.println(request.getRequestURI());
    // 获取请求的类型 (用浏览器直接请求都是get请求)
    System.out.println(request.getMethod());
    // 获取请求的路径 (相对路径)
    System.out.println(request.getContextPath());

    // 获取请求中携带参数
    // 参数是 你提交表单时 表单的name属性
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    System.out.println(username + " " + password);

    // 判断浏览器
    // 可以通过请求头中的信息获取用户使用浏览器
    String header = request.getHeader("User-Agent");
    if (header.toLowerCase().contains("chrome")) {
        System.out.println("用的是谷歌");
    } else if (header.toLowerCase().contains("firefox")) {
        System.out.println("用的是火狐");
    } else {
        System.out.println("其他浏览器");
    }
    System.out.println(header);

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值