请问怎么设置Servlet的初始化参数!

时间:2021-02-24 21:06:04
config对象能够使用getInitParameter(name)获得名字为name的初始化参数,小弟新手,请问那么name对应的初始化参数是怎么设置的?

6 个解决方案

#1



  <servlet>
    <servlet-name>action</servlet-name>
    <description>Struts控制器</description>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>
其中debug和config是参数名称

#2


<context-param>
      <param-name>name</param-name>
      <param-value>value</param-value>
    </context-param>

#3


非常同意楼上的!

#4


<web-app>
<servlet>
<servlet-name>hello</servlet-name>   //命名
<servlet-class>myweb.test.HelloWorld</servlet-class> //编译后位置
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>  //路径
</servlet-mapping>
<web-app>

#5


请问楼上的大侠,能解释一下吗?我把脚本拷到我的jsp文件里,好像不起作用啊!

#6


不是拷贝到jsp,是考到web。xml中servlet的定义部分
一般在对应的servlet的init()方法内获得参数,比如:
System.out.println("The WSE system is initializing...");
super.init(config);
String encoding =
config.getServletContext().getInitParameter("charset");

String home = this.getInitParameter("home");
if (home != null) {
getServletContext().setAttribute("HomeDir", home);
} else {
getServletContext().setAttribute("HomeDir", "c:\\WESPrj");
}
System.out.println("HomeDir:" + home);

// &#212;&#218;&#207;&#223;&#211;&#195;&#187;§&#193;&#208;±í
Set onlineUsers = new TreeSet();
this.getServletContext().setAttribute("onlineUsers", onlineUsers);
// end

//get mail host and authentication(user name and password)
String mailhost = this.getInitParameter("mailhost");
System.out.println("mailhost:" + mailhost);
getServletContext().setAttribute("MailHost", mailhost);

String systememail = this.getInitParameter("systememail");
getServletContext().setAttribute("SystemEmail", systememail);
System.out.println("systememail:" + systememail);

String mailhostuser = this.getInitParameter("mailhostuser");
getServletContext().setAttribute("MailHostUser", mailhostuser);
System.out.println("mailhostuser:" + mailhostuser);

#1



  <servlet>
    <servlet-name>action</servlet-name>
    <description>Struts控制器</description>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>
其中debug和config是参数名称

#2


<context-param>
      <param-name>name</param-name>
      <param-value>value</param-value>
    </context-param>

#3


非常同意楼上的!

#4


<web-app>
<servlet>
<servlet-name>hello</servlet-name>   //命名
<servlet-class>myweb.test.HelloWorld</servlet-class> //编译后位置
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>  //路径
</servlet-mapping>
<web-app>

#5


请问楼上的大侠,能解释一下吗?我把脚本拷到我的jsp文件里,好像不起作用啊!

#6


不是拷贝到jsp,是考到web。xml中servlet的定义部分
一般在对应的servlet的init()方法内获得参数,比如:
System.out.println("The WSE system is initializing...");
super.init(config);
String encoding =
config.getServletContext().getInitParameter("charset");

String home = this.getInitParameter("home");
if (home != null) {
getServletContext().setAttribute("HomeDir", home);
} else {
getServletContext().setAttribute("HomeDir", "c:\\WESPrj");
}
System.out.println("HomeDir:" + home);

// &#212;&#218;&#207;&#223;&#211;&#195;&#187;§&#193;&#208;±í
Set onlineUsers = new TreeSet();
this.getServletContext().setAttribute("onlineUsers", onlineUsers);
// end

//get mail host and authentication(user name and password)
String mailhost = this.getInitParameter("mailhost");
System.out.println("mailhost:" + mailhost);
getServletContext().setAttribute("MailHost", mailhost);

String systememail = this.getInitParameter("systememail");
getServletContext().setAttribute("SystemEmail", systememail);
System.out.println("systememail:" + systememail);

String mailhostuser = this.getInitParameter("mailhostuser");
getServletContext().setAttribute("MailHostUser", mailhostuser);
System.out.println("mailhostuser:" + mailhostuser);