美文网首页
ServletContext 应用

ServletContext 应用

作者: zzqsmile | 来源:发表于2021-09-22 17:14 被阅读0次

1.共享数据

package com.zzqsmile.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.getInitParameter()  //初始化参数
        // this.getServletConfig()  //获取servlet配置
        // this.getServletContext() //servlet上下文

        ServletContext servletContext = this.getServletContext();

        String username = "zzqsmile";

        servletContext.setAttribute("username",username);

    }

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

package com.zzqsmile.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 servletContext = this.getServletContext();

        Object username = (String) servletContext.getAttribute("username");

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        resp.getWriter().println("名字:"+username);

    }

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

2.获取初始化参数

web.xml

<!--配置一些web应用的初始化参数-->
    <context-param>
        <param-name>mysql_url</param-name>
        <param-value>jdbc:mysql://localhost:3306/mynatis</param-value>
    </context-param>
package com.zzqsmile.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 ServletDemo03 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        String mysql_url = servletContext.getInitParameter("mysql_url");
        resp.getWriter().println(mysql_url);
    }

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

3.请求转发(200)

package com.zzqsmile.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;

public class ServletDemo4 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        System.out.println("进入了 ServletDemo4");
//        RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/getp"); //转发请求路径
//        requestDispatcher.forward(req,resp);    //调用forward实现请求转发
        servletContext.getRequestDispatcher("/getp").forward(req,resp);

    }

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

图片.png

4.读取资源文件

Properties

  • 在java目录下新建properties (java_db.properties)
  • 在resources目录下新建properties (db.properties)

思路:需要一个文件流;

package com.zzqsmile.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 ServletDemo05 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        InputStream resourceAsStream = this.getServletContext().getResourceAsStream("/WEB-INF/classes/com/zzqsmile/servlet/java_db.properties");
        InputStream resourceAsStream1 = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");

        Properties properties = new Properties();
        properties.load(resourceAsStream);
        properties.load(resourceAsStream1);


        String username = properties.getProperty("username");
        String password = properties.getProperty("password");

        String user = properties.getProperty("user");
        String pwd = properties.getProperty("pwd");

        resp.getWriter().println(username+":"+password);
        resp.getWriter().println(user+":"+pwd);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
图片.png

相关文章

  • ServletContext 应用

    1.共享数据 2.获取初始化参数 web.xml 3.请求转发(200) 4.读取资源文件 Properties ...

  • 作用域对象

    ServletContext 针对一个web应用。一个web应用只有一个servletContext对象,该对象保...

  • Servlet 3.0之Servlet Context

    1. ServletContext接口介绍 ServletContext接口定义了servlet运行时Web应用的...

  • Java的四大域对象之ServletContext

    ServletContext 生命周期:Web应用被加载时,创建ServletContext对象,服务器关闭或者...

  • ServletContext--当前web应用

    1,servletcontext简介:web容器启动时他会为每个web应用程序都创建一ServletContext...

  • ServletContext

    ServletContext概述 服务器会为每个应用创建一个一个ServletContext对象 ServletC...

  • ServletContext对象

    初识ServletContext对象 每一个web应用都有且仅有一个ServletContext对象,又称Appl...

  • 1. ServletContext sc = this.getServletContext(); //获取应用的路...

  • ServletContext/读取properties文件

    ServletContext 被服务器共享 web应用关闭 tomcat关闭 web应用reload Servle...

  • 11. ServletContext应用

    获取初始化参数 转发和重定向 读取资源文件 popertiesclasspath: java和resource路径...

网友评论

      本文标题:ServletContext 应用

      本文链接:https://www.haomeiwen.com/subject/jlfygltx.html