• 在Servlet中如何如何获取请求的参数?


    获取Servlet初始化参数

        

    Servlet初始化参数配置
    获取Servlet初始化参数2-1
    <web-app>
    <servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>org.jbit.servlet.HelloServlet</servlet-class>
    <init-param>
    <param-name>initParam</param-name>
    <param-value>Hello Servlet</param-value>
    </init-param>
    </servlet>
    <servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/HelloServlet</url-pattern>
    </servlet-mapping>
    </web-app>

    根据参数名称进行读取

    public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {
    System.out.println("处理请求时,doGet()方法被调用。");
    String initParam = getInitParameter("initParam");
    System.out.println(initParam);
    }

    获取Servlet上下文参数

    配置Servlet上下文

    <web-app>
    <context-param>
    <param-name>contextParam</param-name>
    <param-value>Hello Servlet</param-value>
    </context-param>
    <!--省略其他配置-->
    </web-app>

    读取Servlet上下文

    public class HelloServlet extends HttpServlet {
    // …省略其他代码
    public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {
    System.out.println("处理请求时,doGet()方法被调用。");
    String initParam = getInitParameter("initParam");
    String contextParam =
    this.getServletContext().getInitParameter("contextParam");
    System.out.println("Servlet初始化参数" + initParam);
    System.out.println("系统初始化参数" + contextParam);
    }
    }

  • 相关阅读:
    20200924-2 功能测试
    作业要求20200924-4 代码规范,结对要求
    20200924-1 每周例行报告
    20200929-git地址
    白名单
    作业要求 20200917-1 每周例行报告
    词频统计 SPEC
    20200910-1 每周例行报告
    20200924-3 单元测试,结对
    20200924-5 四则运算试题生成,结对
  • 原文地址:https://www.cnblogs.com/longmo666/p/13548952.html
Copyright © 2020-2023  润新知