最后
全网独播-价值千万金融项目前端架构实战
从两道网易面试题-分析JavaScript底层机制
RESTful架构在Nodejs下的最佳实践
开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】
一线互联网企业如何初始化项目-做一个自己的vue-cli
思维无价,看我用Nodejs实现MVC
代码优雅的秘诀-用观察者模式深度解耦模块
前端高级实战,如何封装属于自己的JS库
VUE组件库级组件封装-高复用弹窗组件
}
运行结果:

##### **3.3、用servletContext实现请求转发**
实现Servlet的转发。
* ServletContextDemo4
package gacl.servlet.study;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletContextDemo4 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data = "<h1><font color='red'>abcdefghjkl</font></h1>";
response.getOutputStream().write(data.getBytes());
ServletContext context = this.getServletContext();//获取ServletContext对象
RequestDispatcher rd = context.getRequestDispatcher("/servlet/ServletContextDemo5");//获取请求转发对象(RequestDispatcher)
rd.forward(request, response);//调用forward方法实现请求转发
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
* ServletContextDemo5
package gacl.servlet.study;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletContextDemo5 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getOutputStream().write("servletDemo5".getBytes());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
运行结果:

访问的是ServletContextDemo4,浏览器显示的却是ServletContextDemo5的内容,这就是使用ServletContext实现了请求转发
##### **3.4、利用ServletContext对象读取资源文件**
利用ServletContext对象读取资源文件,因为文件的位置不同,所有读取的方式也不同,一般来说分为两种情况:
* 在Servlet的context域中读取文件,工程目录下的src目录发布到服务器中,会映射到“/WEB-INF/classes”文件夹下。所以要一一对应。而且这个是相对目录,相对于web服务器的目录。如果要用传统的文件读取文件,则要使用绝对路劲
PrintWriter out = response.getWriter();
ServletContext context = this.getServletContext();
String path = context.getRealPath(“/WEB-INF/classes/itcast.properties”);
InputStream in = new FileInputStream(path);
Properties pro = new Properties();
pro.load(in);
* 如果是非servlet中读取配置文件,则要使用类加载器去读取。稍后讲到
项目目录结构如下:

代码范例:使用servletContext读取资源文件
package gacl.servlet.study;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 使用servletContext读取资源文件
*
* @author gacl
*
*/
public class ServletContextDemo6 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/\*\*
* response.setContentType(“text/html;charset=UTF-8”);目的是控制浏览器用UTF-8进行解码;
* 这样就不会出现中文乱码了
*/
response.setHeader(“content-type”,“text/html;charset=UTF-8”);
readSrcDirPropCfgFile(response);//读取src目录下的properties配置文件
response.getWriter().println(“
”);
readWebRootDirPropCfgFile(response);//读取WebRoot目录下的properties配置文件
response.getWriter().println(“
”);
readPropCfgFile(response);//读取src目录下的db.config包中的db3.properties配置文件
response.getWriter().println(“
”);
readPropCfgFile2(response);//读取src目录下的gacl.servlet.study包中的db4.properties配置文件
}
/\*\*
* 读取src目录下的gacl.servlet.study包中的db4.properties配置文件
* @param response
* @throws IOException
*/
private void readPropCfgFile2(HttpServletResponse response)
throws IOException {
InputStream in = this.getServletContext().getResourceAsStream(“/WEB-INF/classes/gacl/servlet/study/db4.properties”);
Properties prop = new Properties();
prop.load(in);
String driver = prop.getProperty(“driver”);
String url = prop.getProperty(“url”);
String username = prop.getProperty(“username”);
String password = prop.getProperty(“password”);
response.getWriter().println(“读取src目录下的gacl.servlet.study包中的db4.properties配置文件:”);
response.getWriter().println(
MessageFormat.format(
“driver={0},url={1},username={2},password={3}”,
driver,url, username, password));
}
/\*\*
* 读取src目录下的db.config包中的db3.properties配置文件
* @param response
* @throws FileNotFoundException
* @throws IOException
*/
private void readPropCfgFile(HttpServletResponse response)
throws FileNotFoundException, IOException {
//通过ServletContext获取web资源的绝对路径
String path = this.getServletContext().getRealPath(“/WEB-INF/classes/db/config/db3.properties”);
InputStream in = new FileInputStream(path);
Properties prop = new Properties();
prop.load(in);
String driver = prop.getProperty(“driver”);
String url = prop.getProperty(“url”);
String username = prop.getProperty(“username”);
String password = prop.getProperty(“password”);
response.getWriter().println(“读取src目录下的db.config包中的db3.properties配置文件:”);
response.getWriter().println(
MessageFormat.format(
“driver={0},url={1},username={2},password={3}”,
driver,url, username, password));
}
/\*\*
* 通过ServletContext对象读取WebRoot目录下的properties配置文件
* @param response
* @throws IOException
*/
private void readWebRootDirPropCfgFile(HttpServletResponse response)
throws IOException {
/**
* 通过ServletContext对象读取WebRoot目录下的properties配置文件
* “/”代表的是项目根目录
*/
InputStream in = this.getServletContext().getResourceAsStream(“/db2.properties”);
Properties prop = new Properties();
prop.load(in);
String driver = prop.getProperty(“driver”);
String url = prop.getProperty(“url”);
String username = prop.getProperty(“username”);
String password = prop.getProperty(“password”);
response.getWriter().println(“读取WebRoot目录下的db2.properties配置文件:”);
response.getWriter().print(
MessageFormat.format(
“driver={0},url={1},username={2},password={3}”,
driver,url, username, password));
}
/\*\*
* 通过ServletContext对象读取src目录下的properties配置文件
* @param response
* @throws IOException
*/
private void readSrcDirPropCfgFile(HttpServletResponse response) throws IOException {
/**
* 通过ServletContext对象读取src目录下的db1.properties配置文件
*/
InputStream in = this.getServletContext().getResourceAsStream(“/WEB-INF/classes/db1.properties”);
Properties prop = new Properties();
prop.load(in);
String driver = prop.getProperty(“driver”);
String url = prop.getProperty(“url”);
String username = prop.getProperty(“username”);
String password = prop.getProperty(“password”);
response.getWriter().println(“读取src目录下的db1.properties配置文件:”);
response.getWriter().println(
MessageFormat.format(
“driver={0},url={1},username={2},password={3}”,
driver,url, username, password));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
运行结果如下:

**使用类装载器读取资源文件**
我们在非servlet中读取资源文件时(比如在数据库的dao层读取配置文件),采用类装载器 classLoader,你可以先采用servlet服务先读取,然后在把servlet传递给dao,这样虽然可以实现,但是,这样损坏了我们编代码的设计原则,就是层之间不能有交织在一起的东西。
package gacl.servlet.study;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.MessageFormat;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 用类装载器读取资源文件
* 通过类装载器读取资源文件的注意事项:不适合装载大文件,否则会导致jvm内存溢出
* @author gacl
*
*/
public class ServletContextDemo7 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/\*\*
* response.setContentType(“text/html;charset=UTF-8”);目的是控制浏览器用UTF-8进行解码;
* 这样就不会出现中文乱码了
*/
response.setHeader(“content-type”,“text/html;charset=UTF-8”);
test1(response);
response.getWriter().println(“
”);
test2(response);
response.getWriter().println(“
”);
//test3();
test4();
}
/\*\*
* 读取类路径下的资源文件
* @param response
* @throws IOException
*/
private void test1(HttpServletResponse response) throws IOException {
//获取到装载当前类的类装载器
ClassLoader loader = ServletContextDemo7.class.getClassLoader();
//用类装载器读取src目录下的db1.properties配置文件
InputStream in = loader.getResourceAsStream(“db1.properties”);
Properties prop = new Properties();
prop.load(in);
String driver = prop.getProperty(“driver”);
String url = prop.getProperty(“url”);
String username = prop.getProperty(“username”);
String password = prop.getProperty(“password”);
response.getWriter().println(“用类装载器读取src目录下的db1.properties配置文件:”);
response.getWriter().println(
MessageFormat.format(
“driver={0},url={1},username={2},password={3}”,
driver,url, username, password));
}
/\*\*
* 读取类路径下面、包下面的资源文件
* @param response
* @throws IOException
*/
private void test2(HttpServletResponse response) throws IOException {
//获取到装载当前类的类装载器
ClassLoader loader = ServletContextDemo7.class.getClassLoader();
//用类装载器读取src目录下的gacl.servlet.study包中的db4.properties配置文件
InputStream in = loader.getResourceAsStream(“gacl/servlet/study/db4.properties”);
Properties prop = new Properties();
prop.load(in);
String driver = prop.getProperty(“driver”);
String url = prop.getProperty(“url”);
String username = prop.getProperty(“username”);
String password = prop.getProperty(“password”);
response.getWriter().println(“用类装载器读取src目录下的gacl.servlet.study包中的db4.properties配置文件:”);
response.getWriter().println(
MessageFormat.format(
“driver={0},url={1},username={2},password={3}”,
driver,url, username, password));
}
/\*\*
* 通过类装载器读取资源文件的注意事项:不适合装载大文件,否则会导致jvm内存溢出
*/
public void test3() {
/**
* 01.avi是一个150多M的文件,使用类加载器去读取这个大文件时会导致内存溢出:
* java.lang.OutOfMemoryError: Java heap space
*/
InputStream in = ServletContextDemo7.class.getClassLoader().getResourceAsStream(“01.avi”);
System.out.println(in);
}
/\*\*
* 读取01.avi,并拷贝到e:\根目录下
* 01.avi文件太大,只能用servletContext去读取
* @throws IOException
*/
public void test4() throws IOException {
// path=G:\Java学习视频\JavaWeb学习视频\JavaWeb\day05视频\01.avi
// path=01.avi
String path = this.getServletContext().getRealPath(“/WEB-INF/classes/01.avi”);
/**
* path.lastIndexOf(“\”) + 1是一个非常绝妙的写法
*/
String filename = path.substring(path.lastIndexOf(“\”) + 1);//获取文件名
InputStream in = this.getServletContext().getResourceAsStream(“/WEB-INF/classes/01.avi”);
byte buffer[] = new byte[1024];
int len = 0;
OutputStream out = new FileOutputStream(“e:\” + filename);
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.close();
in.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
运行结果如下:

使用类装载器读取资源文件,存在的问题是;类装载器,每次只会装载一次。
//如果读取资源文件的程序不是servlet的话,
//就只能通过类转载器去读了,文件不能太大
//用传递参数方法不好,耦合性高
public class UserDao {
private static Properties dbconfig=new Properties();
static {
InputStream in=UserDao.class.getClassLoader().getResourceAsStream("db.properties");
try {
dbconfig.load(in);
} catch (IOException e) {
throw new ExceptionInInitializerError(e);
}
//上面代码类装载器只装载一次,下面代码用类装载方式得到文件位置
URL url=UserDao.class.getClassLoader().getResource("db.properties");
String str=url.getPath();
//file:/C:/apache-tomcat-7.0.22/webapps/day05/WEB-INF/classes/db.properties
try {
InputStream in2=new FileInputStream(str);
try {
dbconfig.load(in2);
} catch (IOException e) {
throw new ExceptionInInitializerError(e);
}
} catch (FileNotFoundException e1) {
throw new ExceptionInInitializerError(e1);
}
}
public void update() {
System.out.println(dbconfig.get("url"));
}
}
#### 算法刷题
大厂面试还是很注重算法题的,尤其是字节跳动,算法是问的比较多的,关于算法,推荐《LeetCode》和《算法的乐趣》,这两本我也有电子版,字节跳动、阿里、美团等大厂面试题(含答案+解析)、学习笔记、Xmind思维导图均可以分享给大家学习。
**[开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】](https://bbs.youkuaiyun.com/topics/618166371)**

**写在最后**
**最后,对所以做Java的朋友提几点建议,也是我的个人心得:**
1. 疯狂编程
2. 学习效果可视化
3. 写博客
4. 阅读优秀代码
5. 心态调整