JavaWeb(二)

JavaWeb

设置全局配置信息

获取config对象
取出servlet配置信息
获取context域对象
获取全局配置信息

public class Demo01 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取config对象
        ServletConfig config = this.getServletConfig();
        //取出servlet配置信息
        String username = config.getInitParameter("username");
        System.out.println(username);
        //获取context域对象
        ServletContext application= this.getServletContext();
        //获取全局配置信息
        String password = application.getInitParameter("key");
        System.out.println(password);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
获取服务器上的真实文件路径 并读取

使用Context域对象获取 可以获取到服务器上的任意资源路径

public class Demo02 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取a文件
        ServletContext  context = this.getServletContext();
        //获取服务器上的真实路径(绝对路径 磁盘路径)
        String path = context.getRealPath("/WEB-INF/classes/a.properties");
        System.out.println(path);
        //读取文件
        Properties properties = new Properties();
        properties.load(new FileInputStream(path));
        System.out.println(properties.getProperty("key"));
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

请求转发

注意:浏览器只是发起了1次请求
Servlet内部做的请求转发 浏览器并不知道

public class Demo03 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("我要吃饭");
        System.out.println("叫外卖来送");
        //获取Context域对象
        ServletContext application= this.getServletContext();
        //从Context域中 获取请求转发器
        RequestDispatcher requestDispatcher = application.getRequestDispatcher("/demo04");
        //进行请求转发
        requestDispatcher.forward(request, response);
        System.out.println("送到了。!!!");
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

public class Demo04 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("我是美团外卖");   
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
HttpServletResponse 服务器的响应对象

响应对象中都有什么?
响应行 http/1.1 状态码200
响应头 告诉浏览器我要做什么 例如响应给你的文件需要下载
响应体 响应回浏览器的数据

public class Demo05 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置服务器的编码格式 默认Tomcat  iso-8859-1
        response.setCharacterEncoding("UTF-8");
        //告诉浏览器 要使用什么编码格式来查看
        response.setHeader("Content-type", "text/html;charset=UTF-8");  

        //这句话代表上面两句话:
        response.setContentType("text/html;charset=UTF-8");

        //给浏览器响应一句话
        //从响应对象HttpServletResponse中获取流对象
        //注意: 这个流对象不是自己创建  要从响应中获取
        PrintWriter out = response.getWriter();
        out.write("好饿!");
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
下载文件

用户发送请求 请求到访问Servlet
Servlet处理请求(把服务器上的图片 以流的形式 使用response 响应给用户浏览器)

public class Demo06 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取类名
        System.out.println(response.getClass().getName());
        System.out.println(request.getClass().getName());
        //获取服务器上的图片路径
        String realPath = this.getServletContext().getRealPath("/WEB-INF/classes/团子大家族.png");
        //字符串切割 获取图片名字
        int index = realPath.lastIndexOf("/");
        String filname = realPath.substring(index+1);
        //修改文件文字的字符集
        filname = new String(filname.getBytes(), "iso-8859-1");
        System.out.println(filname);
        //添加响应头(需要拼接文件名子)
        response.setHeader("content-disposition", "attachment;filename="+filname);
        //告诉浏览器文件下载的格式添加响应头
        response.setHeader("content-type", "image/png");
        //从服务器中读取图片
        FileInputStream  fis = new FileInputStream(realPath);
        // 注意:获取response中的字节流进行数据响应
        ServletOutputStream sos = response.getOutputStream();
        //边读边写
        int a = 0;
        byte[] b = new byte[1024];
        while((a = fis.read(b))!=-1) {
            // 响应回浏览器
            //如果只是单纯的把图片响应回去浏览器并不知道你要干啥(下载或浏览) 需要通过响应头 通知浏览器 我这个文件是下载用的
            sos.write(b, 0, a);
        }
        fis.close();// 注意:自己创建的流自己关
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
请求重定向

浏览器发起请求(请求Servlet)
Servlet 给浏览器一个响应 再响应中会携带一个重定向响应头(头中有重定向的访问地址)
浏览器接到这个响应后 发现重定向头 再一次发起请求 去访问重定向头中的地址
请求重定向和请求转发的区别
请求重定向是发起两次请求(请求地址发生了变化)
请求转发只是一次请求
响应时要注意的细节:从response中获取的字符流和字节流 不能同时使用

public class Demo07 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应的字符集
        response.setContentType("text/html;charset=UTF-8");

        //添加刷新头 每秒刷新一次
        response.setIntHeader("refresh", 1);
        //添加随机数
        response.getWriter().write(Math.random()+" ");

        //3秒后 跳转一个请求地址
        response.setHeader("refresh", "3;url=//web-0327/demo08");
        response.getWriter().write("3秒后跳转");

    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
HttpServletRequest 用户的请求对象
  • HttpServletRequest 用户的请求对象包含
  • 请求行
  • 请求头
  • 请求体

    public class Demo08 extends HttpServlet {
        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 s = request.getHeader("User-Agent");
            System.out.println(s);  
        }
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值