1. 程式人生 > >用init-param如何從web.xml中獲得引數?

用init-param如何從web.xml中獲得引數?

1.用init-param獲取:

例:1.1.1

ServletHello1.java:

package com;
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 ServletHello1 extends HttpServlet {
    protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String salary = getInitParameter("zhangsan");
        System.out.println("salary is "+salary);
    }
}

 

在web.xml中加入:

 


    <servlet>
        <servlet-name>ServletHello1</servlet-name>
        <servlet-class>com.ServletHello1</servlet-class>
             <init-param>
                 <param-name>zhangsan</param-name>
                 <param-value>1000</param-value>
             </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>ServletHello1</servlet-name>

詳情請見:http://www.mark-to-win.com/index.html?content=Jsp/jspUrl.html&chapter=Jsp/jsp2_web.html#GetParamWithinitparam