Servlet处理请求

一、设置全局配置信息
public class Demo01 extends HttpServlet{
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//      fun1();
    //获取全局配置信息
    ServletContext appliction = this.getServletContext();
    String key = appliction.getInitParameter("name");
    System.out.println(key);

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
        private void fun1() {
   //取出servlet信息,首先,获取ConfigServlet
   ServletConfig config = this.getServletConfig();
   //取出配置信息,通过键取出对应的值
   String value = config.getInitParameter("username");
   System.out.println(value)
    }
}

Demo01的配置信息
<!--Demo01  -->
    <!-- 配置设置全局配置信息 -->
    <context-param>
    <param-name>name</param-name>
    <param-value>value</param-value>
    </context-param>

  <servlet>
  <servlet-name>demo01</servlet-name>
  <servlet-class>com.lanou3g.com.Demo01</servlet-class>
  <init-param>
  <param-name>username</param-name>
  <param-value>wanglong</param-value>
  </init-param>
  </servlet>
  <servlet-mapping>
  <servlet-name>demo01</servlet-name>
  <url-pattern>/demo01</url-pattern>
  </servlet-mapping>
二、读取文件
public class Demo02 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //读取a文件
            // fun1();
            //读取b文件
            //fun2();
            //读取c文件
            fun3();
    }

    private void fun3() throws IOException, FileNotFoundException {
        //读取c文件
        ServletContext context = this.getServletContext();
        //获取服务器上的真实路径(绝对路径,磁盘的路径)--相对路径
        String path = context.getRealPath("/WEB-INF/c.properties");
        System.out.println(path);
        //读取文件
        Properties properties = new Properties();
        properties.load(new FileInputStream(path));
        System.out.println(properties.getProperty("key"));
    }

    private void fun2() throws IOException, FileNotFoundException {
        //读取b文件
        ServletContext context = this.getServletContext();
        //获取服务器上的真实路径(绝对路径,磁盘的路径)--相对路径
        String path = context.getRealPath("/WEB-INF/classes/com/lanou3g/b.properties");
        System.out.println(path);
        //读取文件
        Properties properties = new Properties();
        properties.load(new FileInputStream(path));
        System.out.println(properties.getProperty("key"));
    }

    private void fun1() throws IOException, FileNotFoundException {
     //获取服务器上真实的路径并且读取,使用Context域对象获取服务器上的任意资源
    //插图,获取a文件
    //首先,获取context域对象
    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(proerties.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("没有,我去找demo04借给你");
     //首先,我们先获取context域对象
     ServletContext context =this.getServletContext();
    //获取转发器,从context域中获取,请求转发器
    RequestDispatcher dispatcher = context.getRequestDispatcher("/demo04");
    //其次,我们进行请求转发
    dispatcher.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("我有钱,我demo04,借给你了Demo03了兄弟");

    }

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

}
四、HttpServletResponse服务器响应对象
//1.响应对象中有什么:
//a)响应行:http/1.1 状态码200 b)响应头:告诉浏览器我要干什么,例如:响应给你的文件需要下载,以什么编码格式查看 c)响应体:响应回浏览器的数据

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 {
    //获取服务器上的图片真实路径
String path = this.getServletContext().getRealPaht("/WEB-INF/classes/团子大家族.pnt");
//字符串切割,获取图片的名字
int index = path.lastIndexOf("/");//获取到地址最后一个/的字符的坐标
String filename = path.substring(index+1);//留头不留尾,+1,从/后面一个开始截取
   //修改文件名字的字符集--因为我们所输入的图片名也可能会显示乱码
   //参数1:图片名字变成字符数组,参数2:编码格式
   filename = new String (filename.getBytes(),"iso-8859-1");
   //添加设置响应头(拼接文件的名字)
   response.setHeader("content-disposition","attachment;filename="+filename);
   //告诉浏览器,文件下载的格式,添加响应头--content-type image/jpeg
   response.setHeader("content-type","image/png");
   //从服务器中读取图片
   FileInputStream fis = new FileInputStream(path);
   //注意:获取response当中的字节流,进行数据的响应
     ServletOutputStream sos = response.getOutputStream();
     int len =0;
     byte[] b= new byte[1024];
     while((len=fis.read(b))!=-1){
         //响应回浏览器
         //如果只是单纯的把图片响应回去
         //浏览器不知道你想干什么比如:下载/浏览
         //这里我们需要通过响应头,通知浏览器,我这个文件给你是下载用的
         sos.write(b,0,len);
     }
     //注意:自己创建的流自己关
     fis.close();
     //边读边写
    }

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

}
五、请求重定向
1.响应要注意的细节:
a)从response中获取的字符流和字节流不能同时使用
b)在同一个servlet不能同时使用
public class Demo07 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//     fun1(response);
//      fun2(response);
//      fun3(response);

    }


    private void fun3(HttpServletResponse response) throws IOException {
        //设置响应的字符集
        response.setContentType("text/html;charset=UTF-8");
        //3秒后,跳转一个网址
        response.setHeader("refresh", "3;url=/sh0web-day02/demo08");
        response.getWriter().write("3秒后跳转");
    }


    private void fun2(HttpServletResponse response) throws IOException {
        //添加刷新头,每一秒刷新一次
        response.setIntHeader("refresh", 1);
        //添加随机数

        response.getWriter().write(Math.random() + "");
    }


     // 通过添加请求头的方式,请求重定向

    private void fun1(HttpServletResponse response) {
        System.out.println("我要借钱");
         System.out.println("我没有去找demo08");
            //添加重定向响应头
            //注意:添加头信息请求地址的时候,需要写明工程名
            response.setHeader("location", "/sh0web-day02/demo08");
            //添加重定向状态码
            response.setStatus(302);
            System.out.println("我去了");
    }

    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("我借了");




            //获取请求的网址
//               http://localhost:8080/sh0web-day02/demo08
            System.out.println(request.getRequestURL());
//              /sh0web-day02/demo08
        System.out.println(request.getRequestURI());
//      获取请求类型  --      GET
        //用浏览器直接请求的都是GET请求
        System.out.println(request.getMethod());
//      获取请求路径(相对路径)---     /sh0web-day02
        System.out.println(request.getContextPath());
    //获取请求中携带的参数   参数是你提交表单时,表单的name属性
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username);
        System.out.println(password);

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

        System.out.println(header);
    }

    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、付费专栏及课程。

余额充值