javaWeb——Servlet中getInitParameter()的使用
ServletConfig概述1、容器初始化一个servlet时,会为这个servlet建一个唯一的ServletConfig2、容器从web.xml读出Servlet初始化参数,并把这些参数交给ServletConfig,然后把ServletConfig传递给servlet的init(ServletConfig config)方法。3、容器只有在创建servlet实例时才会读DD文件中的init
·
ServletConfig概述
1、容器初始化一个servlet时,会为这个servlet建一个唯一的ServletConfig
2、容器从web.xml读出Servlet初始化参数,并把这些参数交给ServletConfig,然后把ServletConfig传递给servlet的init(ServletConfig config)方法。
3、容器只有在创建servlet实例时才会读DD文件中的init-param, 并且在servlet一生中只读一次。
4、方法:
getInitParameter(String name):根据给定的初始化参数,返回匹配的初始化参数值。
getInitParameterNames():返回一个Enumeration对象,里面包含了所有的初始化参数。
getServletContext():返回一个servletContext()对象.
getServletName():返回servlet的名字,即web.xml中的的子元素的值。如果没有配置这个子元素,则返回servlet类的名字。
getInitParameter()的使用
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>MyServlet</display-name> //文件名
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<!-- 类名 -->
<servlet-name>Myservlet</servlet-name> //类名
<!-- 所在的包.类名-->
<servlet-class>servlet.com.Myservlet</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>admin</param-value>
</init-param>
<init-param>
<param-name>psd</param-name>
<param-value>123456</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Myservlet</servlet-name> //类名
<url-pattern>/Myservlet/Myservlet</url-pattern> 路径:/项目名/类名
</servlet-mapping>
</web-app>
Myservlet.java源码
package servlet.com;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Myservlet
*/
//@WebServlet(
// asyncSupported = true,
// urlPatterns = { "/Myservlet" },
// initParams = {
// @WebInitParam(name = "name", value = "admin"),
// @WebInitParam(name = "psd", value = "123456")
// }
// )
//@WebServlet("/Myservlet")
public class Myservlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Myservlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
PrintWriter out = response.getWriter();
response.setHeader("Content-type","text/html; charset=UTF-8");
ServletConfig cfg = getServletConfig();
String username = cfg.getInitParameter("name");
String password = cfg.getInitParameter("psd");
out.println("name: "+username);
out.println("psd: "+password);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)