最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Servlet中在service方法里获取初始化方法

互联网 admin 0浏览 0评论

Servlet中在service方法里获取初始化方法

1、首先在web.xml中定义两个初始化变量

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns=""xmlns:xsi=""xsi:schemaLocation=" .xsd"version="4.0"><!--配置servlet让tomcat加载--><servlet><servlet-name>HelloServlet</servlet-name><servlet-class>com.gxa.servlet.HelloServlet</servlet-class><init-param><param-name>name</param-name><param-value>博客</param-value></init-param><init-param><param-name>age</param-name><param-value>14</param-value></init-param></servlet><!--映射路径(虚拟路径)--><servlet-mapping><servlet-name>HelloServlet</servlet-name><!--配置虚拟路径--><url-pattern>/helloServlet</url-pattern></servlet-mapping>
</web-app>

2、在servlet中的service方法里获取初始化参数

package com.gxa.servlet;import javax.servlet.*;
import java.io.IOException;
import java.util.Enumeration;public class HelloServlet implements Servlet {private ServletConfig servletConfig;@Overridepublic void init(ServletConfig servletConfig) throws ServletException {this.servletConfig = servletConfig;}@Overridepublic ServletConfig getServletConfig() {return null;}@Overridepublic void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {System.out.println("hello servlet");//获取初始化参数----第一种方式String name = servletConfig.getInitParameter("name");String age = servletConfig.getInitParameter("age");System.out.println("name:" + name + "-age:" + age);//获取初始化参数----第二种方式Enumeration<String> names = servletConfig.getInitParameterNames();if (names.hasMoreElements()) {String key = names.nextElement();System.out.println("key:" + servletConfig.getInitParameter(key));}response.getWriter().write("hello servlet binggo");}@Overridepublic String getServletInfo() {return null;}@Overridepublic void destroy() {}
}

 

Servlet中在service方法里获取初始化方法

1、首先在web.xml中定义两个初始化变量

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns=""xmlns:xsi=""xsi:schemaLocation=" .xsd"version="4.0"><!--配置servlet让tomcat加载--><servlet><servlet-name>HelloServlet</servlet-name><servlet-class>com.gxa.servlet.HelloServlet</servlet-class><init-param><param-name>name</param-name><param-value>博客</param-value></init-param><init-param><param-name>age</param-name><param-value>14</param-value></init-param></servlet><!--映射路径(虚拟路径)--><servlet-mapping><servlet-name>HelloServlet</servlet-name><!--配置虚拟路径--><url-pattern>/helloServlet</url-pattern></servlet-mapping>
</web-app>

2、在servlet中的service方法里获取初始化参数

package com.gxa.servlet;import javax.servlet.*;
import java.io.IOException;
import java.util.Enumeration;public class HelloServlet implements Servlet {private ServletConfig servletConfig;@Overridepublic void init(ServletConfig servletConfig) throws ServletException {this.servletConfig = servletConfig;}@Overridepublic ServletConfig getServletConfig() {return null;}@Overridepublic void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {System.out.println("hello servlet");//获取初始化参数----第一种方式String name = servletConfig.getInitParameter("name");String age = servletConfig.getInitParameter("age");System.out.println("name:" + name + "-age:" + age);//获取初始化参数----第二种方式Enumeration<String> names = servletConfig.getInitParameterNames();if (names.hasMoreElements()) {String key = names.nextElement();System.out.println("key:" + servletConfig.getInitParameter(key));}response.getWriter().write("hello servlet binggo");}@Overridepublic String getServletInfo() {return null;}@Overridepublic void destroy() {}
}

 

发布评论

评论列表 (0)

  1. 暂无评论