javaweb(Servlet的概述,原理,Mapping问题,ServletContext)

(1) Servlet的概述

就是sun公司开发动态web的一门技术,Sun在这些API中提供一个接口叫做:Servlet
开发一个Servlet程序,只需要完成两个小步骤:
* 编写一个类,实现Servlet接口
* 把开发好的Java类部署到web服务器中
(把实现了Servlet接口的Java程序叫做,Servlet)Serlvet接口Sun公司有两个默认的实现类:HttpServlet,GenericServlet

(2) 第一个Servlet程序(hello,Servlet)

* 构建一个普通的Maven项目,删掉里面的src目录,以后的学习就在这个项目里面建立Moudel;这个空的工程就是Maven主工程
* 关于Maven父子工程的理解:

父项目中会有

 
        servlet-01
    

子项目会有

 
       javaweb-02-servlet
       com.kuang
       1.0-SNAPSHOT
   

父项目中的java子项目可以直接使用son extends father

* Maven环境优化
  • 修改web.xml为最新的
  • 将maven的结构搭建完整
    javaweb(Servlet的概述,原理,Mapping问题,ServletContext)_第1张图片
编写一个普通类实现Servlet接口,这里我们直接继承HttpServlet
package com.yues.web01;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class helloo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter(); //响应流
        writer.print("Hello,Serlvet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
编写Servlet的映射

为什么需要映射:我们写的是JAVA程序,但是要通过浏览器访问,而浏览器需要连接web服务器,所以我们需要再web服务中注册我们写的Servlet,还需给他一个浏览器能够访问的路径

 
    
        hello
        com.kuang.servlet.HelloServlet
    
    
    
        hello
        /hello
    
配置Tomcat,至此,第一个Servlet程序就写好啦

(3) Servlet原理

浏览器发起请求给服务器(web容器),web容器要进行相应处理,(如果是第一次访问的话,会产生一个.class文件,当然这只加载一次)将请求(包含请求头和请求体)和响应(响应头和响应体)调用Servlet的service方法,service方法需要传入两个参数,一个是请求一个是响应,然后这个方法是通过我们人为去操作重写的,决定要去怎么处理这个请求,并且如何做出响应,处理完成之后,web容器就响应浏览器

Servlet是由Web服务器调用,web服务器在收到浏览器请求之后,会:
javaweb(Servlet的概述,原理,Mapping问题,ServletContext)_第2张图片

(4) Mapping问题

一个Servlet可以指定一个映射路径

   
       hello
       /hello
   

一个Servlet可以指定多个映射路径

 
        hello
        /hello
    
    
        hello
        /hello2
    
    
        hello
        /hello3
    
    
        hello
        /hello4
    
    
        hello
        /hello5
    

​一个Servlet可以指定通用映射路径

 
        hello
        /hello/*
    默认请求路径    
    
        hello
        /*
    

指定一些后缀或者前缀等等….



    hello
    *.qinjiang

优先级问题
指定了固有的映射路径优先级最高,如果找不到就会走默认的处理请求;



    error
    com.kuang.servlet.ErrorServlet


    error
    /*

(5) ServletContext

web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用

* 共享数据

什么是共享数据呢?就是说,在一个Servlet中保存的数据,可以在另一个Servlet中拿到

example
public class contextstuday extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        servletContext.setAttribute("name","小可爱");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
public class getcontext extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        ServletContext context = this.getServletContext();
        String name = (String) context.getAttribute("name");
        resp.getWriter().print(name);

    }

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

Servlet的映射


    three
    com.yues.web01.contextstuday


    three
    /three



    four
    com.yues.web01.getcontext


    four
    /four

* 获取初始化参数

example
public class getinit extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ServletContext servletContext = this.getServletContext();
        String flowername = servletContext.getInitParameter("flowername");
        resp.getWriter().print(flowername);
    }

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

    flowername
    beautiful



    five
    com.yues.web01.getinit


    five
    /five

* 请求转发

javaweb(Servlet的概述,原理,Mapping问题,ServletContext)_第3张图片
这个图是说:比如A给B发送了一个请求,但是B将这个请求交给了C去处理,然后C再响应给B,B再响应给A,这个过程就是请求转发,在这个过程中,地址栏的url路径不发生改变,在A发送请求再到A接受到响应的过程中,A始终是与B进行交互的

example
public class achieve extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        servletContext.getRequestDispatcher("/one").forward(req,resp);
    }

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

    six
    com.yues.web01.achieve


    six
    /six

* 读取资源文件

在读取配置文件的过程中,要注意,我们要的路径是web资源下的路径,而不是像之前所学习的一个固定的路径,这时候需要在该项目生成的taget文件在找到WEBINF,进而获得配置文件的路径
public class readfile extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        InputStream in = servletContext.getResourceAsStream("/WEB-INF/classes/ff.properties");
        Properties properties = new Properties();
        properties.load(in);
        String username = properties.getProperty("username");
        String password = properties.getProperty("password");
        resp.getWriter().print(username+"\t"+password);
    }

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

demo

public class judge extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String realPath = "E:\\javaweb-servlet\\servlet01\\src\\main\\resources\\aaa.png";
        System.out.println("下载文件的路径:"+realPath)
       String fileName = realPath.substring(realPath.lastIndexOf("\\") + 1);
resp.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(fileName,"UTF-8");
ileInputStream in = new FileInputStream(realPath);
  
        int len = 0;
        byte[] buffer = new byte[1024];
  ServletOutputStream out = resp.getOutputStream();
      
        while ((len=in.read(buffer))>0){
            out.write(buffer,0,len);
        }

        in.close();
        out.close();
    }

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

    two
    com.yues.web01.judge


    two
    /two

你可能感兴趣的:(javaweb(Servlet的概述,原理,Mapping问题,ServletContext))