鱼少学习多摸

day98-javaweb-context应用

javaweb--servlet的一些应用

servletcontext

简介:当web容器启动的时候,会为每个web容器创建一个对应的servletcontext

代表当前的web应用

功能: 共享数据

setAttribute,将一个数据保存在servletcontext中

复制代码
 public class HelloServlet extends HttpServlet {
     @Override
     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         System.out.println("hello");
         ServletContext servletContext = this.getServletContext();
         //web容器启动的时候,会为每个web容器创建一个对应的servletcontext对象
         //代表当前的web应用
         //共享数据
         String username = "gugu";
         servletContext.setAttribute("username",username);
         //将一个数据保存在servletContext中
     }
 }
复制代码

 

getAttribute,取出保存在servletcontext中的数据

 
复制代码
public class GetServlet extends HttpServlet {
     @Override
     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         ServletContext servletContext = this.getServletContext();
         String username = (String) servletContext.getAttribute("username");
 ​
         resp.setContentType("text/html");
         resp.setCharacterEncoding("utf-8");
         resp.getWriter().print("name: "+username);
     }
 }
复制代码

 

params,传递相应的参数

首先在web.xml中配置web应用的初始化参数

 
<!--    配置web应用的初始化参数-->
     <context-param>
         <param-name>url</param-name>
         <param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
     </context-param>

 

读取xml中的对应参数

复制代码
 public class ServletDemo03_params extends HttpServlet {
     @Override
     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         ServletContext servletContext = this.getServletContext();
         String url = servletContext.getInitParameter("url");
         resp.getWriter().print(url);
     }
 ​
     @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         super.doPost(req, resp);
     }
 }
复制代码

 

dispatcher,转发请求,可以转发到相应的servlet请求中而保持网址不变

 
复制代码
public class ServletDemo04_dispatcher extends HttpServlet {
     @Override
     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         ServletContext servletContext = this.getServletContext();
         System.out.println("okok");
         RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/params");
         //转发的请求路径
         requestDispatcher.forward(req,resp);//调用forward实现请求转发
//servletContext.getRequestDispatcher("/hello").forward(req,resp);
 ​
     }
 ​
     @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         super.doPost(req, resp);
     }
 }
复制代码

 

其中可以不设置参数直接调用.forward()

properties获取properties中的数据

在roperties中编写所需要的数据

 username=root
 password=123456

 

使用InputStreamproperties.load读取properties数据为流

复制代码
 public class ServletDemo05_properties extends HttpServlet {
     @Override
     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         InputStream resourceAsStream = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
         Properties properties = new Properties();
         properties.load(resourceAsStream);
         String username = properties.getProperty("username");
         String password = properties.getProperty("password");
 ​
         resp.getWriter().print(username+password);
 ​
     }
 ​
     @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         super.doPost(req, resp);
     }
 }
复制代码

 

 
posted @   北海之上  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
/* 粒子吸附*/
点击右上角即可分享
微信分享提示