ServletContext
Web容器在启动的时候,它会为每一个Web程序都创建一个对应的ServletContext对象,它代表了当前的Web应用。
作用:
1.共享数据。
我在此Servlet中保存的数据,在其他Servlet中也可以使用。
存放数据文件:
package com.edwin.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author Edwin D
* @date 2020.6.2 上午 7:59
*/
public class HelloServlet_2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// this.getInitParameter();初始化参数
// this.getServletContext();Servlet配置
// this.getServletContext();Servlet上下文
ServletContext i = this.getServletContext();
String username = "Edwin";//数据
i.setAttribute("Username",username);
// 将一个值存在了ServletContext中,名字为:Username,值为username(Edwin)。
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
读取数据文件:
package com.edwin.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author Edwin D
* @date 2020.6.2 上午 8:56
*/
public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext i = this.getServletContext();
String username = (String) i.getAttribute("Username");
resp.setContentType("text/html");
resp.setCharacterEncoding("UTF-8");
resp.getWriter().println("姓名:"+username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>com.edwin.servlet.HelloServlet_2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Get</servlet-name>
<servlet-class>com.edwin.servlet.GetServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Get</servlet-name>
<url-pattern>/Get</url-pattern>
</servlet-mapping>
</web-app>
测试结果:
注意访问页面的顺序:点击运行,先访问Hello(此页面为空),但是必须要先访问,确保:
String username = "Edwin";//数据
数据传入ServletContext,才可以被后面的GetServlet访问到。
结果:
主界面:
直接访问GetServlet:Null值
转到Hello:空值
再访问GetServlet:
2.获取初始化参数
编写Servlet:
package com.edwin.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author Edwin D
* @date 2020.6.2 下午 10:21
*/
public class ParameterServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext i = this.getServletContext();
String url = i.getInitParameter("url");
resp.getWriter().println(url);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
设置参数:
<!--配置一些Web应用的初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/Database</param-value>
</context-param>
<servlet>
<servlet-name>Url</servlet-name>
<servlet-class>com.edwin.servlet.ParameterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Url</servlet-name>
<url-pattern>/Url</url-pattern>
</servlet-mapping>
输出效果:
更改路径:
3.请求转发
原理图:
Servlet代码:
package com.edwin.servlet;
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;
import java.io.IOException;
/**
* @author Edwin D
* @date 2020.6.2 下午 10:33
*/
public class ForwardServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext i = this.getServletContext();
// 方法一:
// RequestDispatcher requestDispatcher = i.getRequestDispatcher("/Url");//请求转发的路径
// requestDispatcher.forward(req,resp);//调用forward请求转发。
// 方法二:简化版方法一
i.getRequestDispatcher("/Url").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
配置代码:
<!--Forward-->
<servlet>
<servlet-name>For</servlet-name>
<servlet-class>com.edwin.servlet.ForwardServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>For</servlet-name>
<url-pattern>/Forward</url-pattern>
</servlet-mapping>
效果:
区别于另外一种模式:
4.读取资源文件
Properties:
-
在java目录下新建一个Properties。
-
在resource目录下新建一个Properties。
运行程序后,你会发现,两个文件都被打包到同一个路径下:classes。我们俗称为这个路径为:classpath
思路:创建一个文件流:
Properties:
username = root
password = 1234
Servlet:
package com.edwin.servlet;
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.InputStream;
import java.util.Properties;
/**
* @author Edwin D
* @date 2020.6.3 下午 12:46
*/
public class CheckPropertiesServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream i = this.getServletContext().getResourceAsStream("/WEB-INF/classes/com/edwin/servlet/ac.properties");
Properties prop = new Properties();
prop.load(i);
String Un = prop.getProperty("username");
String Pwd = prop.getProperty("password");
resp.getWriter().println(Un+":"+Pwd);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
Xml:
<!--Properties-->
<servlet>
<servlet-name>Prop</servlet-name>
<servlet-class>com.edwin.servlet.CheckPropertiesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Prop</servlet-name>
<url-pattern>/Prop</url-pattern>
</servlet-mapping>
输出:
参考文献
《【狂神说Java】JavaWeb入门到实战》
2020.06.03