7、ServletContext、Web项目Exploded和Archive的区别

创建Servlet

创建Servlet类的快捷方式:

1、修改模板代码

在这里插入图片描述

2、右键src,New Create Servlet,使用注解新建Servlet即可。

在这里插入图片描述

WebServlet:JavaWeb阶段的第一个注解。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface WebServlet {
    String name() default "";

    String[] value() default {};

    String[] urlPatterns() default {};

    int loadOnStartup() default -1;
    
    ......
}

ServletContext

1、共享数据

最大的域对象,代表整个Web应用。

package org.westos.demo;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author lwj
 * @date 2020/8/15 13:47
 */
@WebServlet(name = "MyServlet", value = {"/myServlet"})
public class MyServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // ServletContext 全局上下文对象,代表了整个Web应用
        // ServletContext是最大的全局域对象,域对象:可以在其范围内进行数据的共享
        // Web中四个域对象:ServletContext整个Web应用、Session会话域、Request请求域、PageContext页面域
        // setAttribute(String s, Object o) 、 getAttribute(String s) 、 removeAttribute(String s)
        ServletContext servletContext = this.getServletContext();
        // HttpServlet -> GenericServlet -> ServletConfig (getServletContext())
        servletContext.setAttribute("username", "张三");
        // 向域中存储数据
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
package org.westos.demo;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author lwj
 * @date 2020/8/15 13:59
 */
@WebServlet(name = "MyServlet2", value = "/myServlet2")
public class MyServlet2 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        String username = (String) servletContext.getAttribute("username");
        System.out.println(username);
    }

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

在这里插入图片描述

2、获取项目根目录

1、在Servlet中获取

package org.westos.demo;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author lwj
 * @date 2020/8/15 14:08
 */
@WebServlet(name = "MyServlet3", value = "/myServlet3")
public class MyServlet3 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //web目录下a.txt、WEB-INF/目录下b.txt、src下c.txt,那么如何读取这些文件
        //这些文件是要放在Tomcat服务器上,所以要动态生成前缀路径
        ServletContext servletContext = this.getServletContext();
        String realPath = servletContext.getRealPath("/");
        //获取项目真实路径
        System.out.println("realPath : " + realPath);

        //FileInputStream fis = new FileInputStream(new File(realPath + "a.txt"));
        FileInputStream fis = new FileInputStream(new File(servletContext.getRealPath("/a.txt")));
        char a = (char) fis.read();
        System.out.println("a : " + a);

        //fis = new FileInputStream(new File(realPath + "WEB-INF/b.txt"));
        fis = new FileInputStream(new File(servletContext.getRealPath("/WEB-INF/b.txt")));
        char b = (char) fis.read();
        System.out.println("b : " + b);

        //fis = new FileInputStream(new File(realPath + "WEB-INF/classes/c.txt"));
        fis = new FileInputStream(new File(servletContext.getRealPath("/WEB-INF/classes/c.txt")));
        char c = (char) fis.read();
        System.out.println("c : " + c);

        /*
        realPath : E:\XiBuKaiYuan\JavaWebDirectory\0815\20200815-Servlet-下午\out\artifacts\20200815_Servlet__war_exploded\
        a : a
        b : b
        c : c
         */
    }

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

在这里插入图片描述

在这里插入图片描述

2、在普通Java类中获取

类加载器:只能获取src目录下的文件。

package org.westos.utils;

import java.io.InputStream;

/**
 * @author lwj
 * @date 2020/8/15 15:24
 */
public class MyUtil {
    /*
    普通Java类进行文件读取,打包后是不存在src目录的,那么如果想要读取src下的文件,则需要用类加载的方式
     */

    public static InputStream readFile() {
        return MyUtil.class.getClassLoader().getResourceAsStream("config.properties");
    }
}
package org.westos.demo;

import org.westos.utils.MyUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author lwj
 * @date 2020/8/15 15:29
 */
@WebServlet(name = "MyServlet4", value = {"/myServlet4"})
public class MyServlet4 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(MyUtil.readFile());
    }

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

在这里插入图片描述

3、获取文件的MIME类型

package org.westos.demo;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author lwj
 * @date 2020/8/15 15:37
 */
@WebServlet(name = "MyServlet5", value = "/myServlet5")
public class MyServlet5 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        String mimeType = servletContext.getMimeType(servletContext.getRealPath("/") + "a.txt");
        System.out.println("mimeType : " + mimeType);
    }

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

在这里插入图片描述

Exploded和Archive的区别

打包发布Web项目时,Project Structure -> Artifacts,

选项中有web application exploded,这个是以文件夹形式(War Exploded)发布项目,选择这个,发布项目时就会自动生成文件夹在指定的output directory;

如果选web application archive,就是war包形式,每次都会重新打包全部的,将项目打成一个war包在指定位置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值