Servlet 03 ----【javaweb-06】
SERVLET
ServletContext
web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用。
接下来,让我们来看看ServletContext对象有什么用处吧。
1. 数据传输
我们可以在保存一个类中的servlet的同时,在另一个类获取该servlet数据。
代码展示:
类1:
package com.charels.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; public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//this.getServletContext() Servlet上下文 ServletContext context = this.getServletContext(); String name = "小明"; context.setAttribute("name",name); } }
类2.
package com.charels.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; public class GetServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context = this.getServletContext(); String name = (String)context.getAttribute("name"); resp.setContentType("text/html"); resp.setCharacterEncoding("GBK"); resp.getWriter().print("名字"+name); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
在web.xml注册,两个servlet。
<!-- 注册servlet--> <servlet> <servlet-name>hello</servlet-name> <servlet-class>com.charels.servlet.HelloServlet</servlet-class> </servlet> <!-- servlet的请求路径--> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <servlet> <servlet-name>getc</servlet-name> <servlet-class>com.charels.servlet.GetServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>getc</servlet-name> <url-pattern>/getc</url-pattern> </servlet-mapping>
接下来,让我们康康运行Tomcat后的展示结果:
在这里要注意,根据代码的意思,要获取上下文数据,则需要先访问第一个类的servlet,再访问第二个才能获取数据。
第一个类的网页:
第二个类的网页:
2. 获取初始化参数
代码展示:
首先在web.xml中配置初始化数据:
<!--配置一些web应用初始化参数--> <context-param> <param-name>url</param-name> <param-value>jdbc:mysql://localhost:3306/mybatis</param-value> </context-param>
接下来编写获取初始化数据的代码:
package com.charels.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; public class GetUrl extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context = this.getServletContext(); String url = context.getInitParameter("url"); resp.getWriter().print(url); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
编写完毕后,注册servlet,然后运行Tomcat:
3. 请求与转发
我们可以通过一个servlet的类来实现页面的转发。
代码展示:
package com.charels.servlet; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class Dri extends GetUrl{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("进入Dri"); ServletContext context = this.getServletContext(); context.getRequestDispatcher("/g1").forward(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
结果展示:
控制台:
页面展示:
4. 读取资源文件
首先,在properties的包中创建一个db.properties并编写数据
在servlet类中编写读取资源的代码:
package com.charels.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; public class GetPro extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setCharacterEncoding("gbk"); InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties"); Properties pro = new Properties(); pro.load(is); String username = pro.getProperty("username"); String passsword= pro.getProperty("password"); resp.getWriter().print(username + ':' + passsword); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
编写完毕后,运行Tomcat,展示结果: