zoukankan      html  css  js  c++  java
  • JavaWeb获取web.xml初始参数!getInitParameter

    一、getInitParameter:

    解释:getInitParameter () 方法是在GenericServlet接口中定义的一个方法 ,用来调用初始化在 web.xml中存放的参量。

    二、代码解释:

    ①:附:源码!源码图!

    package com.laugh.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 InitServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext Context = this.getServletContext();
            //调用初始地址
            String url = Context.getInitParameter("url");
            resp.getWriter().println(url);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }

    ②:web.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">
    
        <!--我这个头是新的-->
    
        <!--设置web初始化参数-->
        <context-param>
            <param-name>url</param-name>
            <param-value>https://www.cnblogs.com/superyonng</param-value>
        </context-param>
    
    
        <!--我是提供获得初始化路径的方法 InitServlet-->
        <servlet>
            <servlet-name>url</servlet-name>
            <servlet-class>com.laugh.servlet.InitServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>url</servlet-name>
            <url-pattern>/url</url-pattern>
        </servlet-mapping>
    
    
    </web-app>

    ③:输出结果:

    生在内卷,你卷我也卷。 愿与诸君共勉!
  • 相关阅读:
    AspNetPager分页控件
    Asp.Net MVC 路由表
    自动检测海岸线,生成海浪网格
    斜坡刷子,一种好用的地形编辑方式
    用NVPerfHud4剖析Farcry的绘制过程
    初学BSP,上一些测试数据
    让游戏通过红蓝立体眼镜展现立体效果
    没有贴图拉伸的陡峭悬崖
    刚刚做的Bloom(伪HDR)效果
    近两个礼拜地图编辑器的一些进展
  • 原文地址:https://www.cnblogs.com/superyonng/p/15597161.html
Copyright © 2011-2022 走看看