---
title: "Servlet获取初始化参数"
author: "cakin24"
date: "2017-10-15T09:20:27Z"
tags: ["Servlet", "参数"]
categories: ["编程语言"]
slug: "servlet"
draft: false
lastmod: "2017-10-15T09:20:27Z"
id: 2396349
url: "https://cakin24.iteye.com/blog/2396349"
---

一 获取初始化参数

在web.xml中配置Servlet时，可以配置一些初始化参数。

而在Servlet中可以通过ServletConfig接口提供方法来取得这些参数。

 

二 界面

&lt;%@ page language="java" import="java.util.\*" contentType="text/html; charset=utf-8" %&gt;

&lt;%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%&gt;

 

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;

&lt;html&gt;

  &lt;head&gt;

    &lt;base href="&lt;%=basePath%&gt;"&gt;

   

    &lt;title&gt;My JSP 'index.jsp' starting page&lt;/title&gt;

        &lt;meta http-equiv="pragma" content="no-cache"&gt;

        &lt;meta http-equiv="cache-control" content="no-cache"&gt;

        &lt;meta http-equiv="expires" content="0"&gt;   

        &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;

        &lt;meta http-equiv="description" content="This is my page"&gt;

        &lt;!--

        &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;

        --&gt;

  &lt;/head&gt;

 

  &lt;body&gt;

    &lt;h1&gt;获取初始化参数演示案例&lt;/h1&gt;

    &lt;hr&gt;

    &lt;a href="servlet/GetInitParameterServlet"&gt;获取初始化参数Servlet&lt;/a&gt;

  &lt;/body&gt;

&lt;/html&gt;

 

三 配置文件

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;web-app xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance"](http://www.w3.org/2001/XMLSchema-instance%22); xmlns="[http://java.sun.com/xml/ns/javaee"](http://java.sun.com/xml/ns/javaee%22); xmlns:web="[http://java.sun.com/xml/ns/javaee/web-app\_2\_5.xsd"](http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd%22); xsi:schemaLocation="[http://java.sun.com/xml/ns/javaee](http://java.sun.com/xml/ns/javaee)[http://java.sun.com/xml/ns/javaee/web-app\_2\_5.xsd"](http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd%22); version="2.5"&gt;

  &lt;display-name&gt;&lt;/display-name&gt;

  &lt;servlet&gt;

    &lt;description&gt;This is the description of my J2EE component&lt;/description&gt;

    &lt;display-name&gt;This is the display name of my J2EE component&lt;/display-name&gt;

    &lt;servlet-name&gt;GetInitParameterServlet&lt;/servlet-name&gt;

    &lt;servlet-class&gt;servlet.GetInitParameterServlet&lt;/servlet-class&gt;

    &lt;init-param&gt;

      &lt;param-name&gt;username&lt;/param-name&gt;

      &lt;param-value&gt;admin&lt;/param-value&gt;

    &lt;/init-param&gt;

    &lt;init-param&gt;

      &lt;param-name&gt;password&lt;/param-name&gt;

      &lt;param-value&gt;123456&lt;/param-value&gt;

    &lt;/init-param&gt;

  &lt;/servlet&gt;

  &lt;servlet-mapping&gt;

    &lt;servlet-name&gt;GetInitParameterServlet&lt;/servlet-name&gt;

    &lt;url-pattern&gt;/servlet/GetInitParameterServlet&lt;/url-pattern&gt;

  &lt;/servlet-mapping&gt;

  &lt;welcome-file-list&gt;

    &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;

  &lt;/welcome-file-list&gt;

&lt;/web-app&gt;

 

四 Servlet

package servlet;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class GetInitParameterServlet extends HttpServlet {

    private String username;//用户名

    private String password; //密码

    

    

    

    public String getUsername() {

        return username;

    }

    public void setUsername(String username) {

        this.username = username;

    }

    public String getPassword() {

        return password;

    }

    public void setPassword(String password) {

        this.password = password;

    }

    /\**

     * Constructor of the object.

     \*/

    public GetInitParameterServlet() {

        super();

    }

    /\**

     * Destruction of the servlet. &lt;br&gt;

     \*/

    public void destroy() {

        super.destroy(); // Just puts "destroy" string in log

        // Put your code here

    }

    /\**

     * The doGet method of the servlet. &lt;br&gt;

     *

     * This method is called when a form has its tag value method equals to get.

     *

     * @param request the request send by the client to the server

     * @param response the response send by the server to the client

     * @throws ServletException if an error occurred

     * @throws IOException if an error occurred

     \*/

    public void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        doPost(request,response);

    }

    /\**

     * The doPost method of the servlet. &lt;br&gt;

     *

     * This method is called when a form has its tag value method equals to post.

     *

     * @param request the request send by the client to the server

     * @param response the response send by the server to the client

     * @throws ServletException if an error occurred

     * @throws IOException if an error occurred

     \*/

    public void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");

        PrintWriter out = response.getWriter();

        out.println("&lt;!DOCTYPE HTML PUBLIC \\"-//W3C//DTD HTML 4.01 Transitional//EN\\"&gt;");

        out.println("&lt;HTML&gt;");

        out.println("  &lt;HEAD&gt;&lt;TITLE&gt;A Servlet&lt;/TITLE&gt;&lt;/HEAD&gt;");

        out.println("  &lt;BODY&gt;");

        out.println("&lt;h2&gt;"+"用户名："+this.getUsername()+"&lt;/h2&gt;");

        out.println("&lt;h2&gt;"+"密码："+this.getPassword()+"&lt;/h2&gt;");

        out.println("  &lt;/BODY&gt;");

        out.println("&lt;/HTML&gt;");

        out.flush();

        out.close();

    }

    /\**

     * Initialization of the servlet. &lt;br&gt;

     *

     * @throws ServletException if an error occurs

     \*/

    public void init() throws ServletException {

        // Put your code here

      this.setUsername(this.getInitParameter("username"));

      this.setPassword(this.getInitParameter("password"));

    }

}

 

五 测试

 

![](http://dl2.iteye.com/upload/attachment/0127/3757/c06f8d9e-2adc-3acc-81fe-f0c6f1241460.png)