如何在web.xml中配置参数并在页面上显示?

时间:2015-05-09 08:01:06

标签: java jsp servlets

我最近正在练习java servlet编程,并遇到了一段时间困扰我的问题。

请参阅代码。在下面的代码中,我尝试计算访问servlet的计数。我可以在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">
  <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>Counter</servlet-name>
   <servlet-class>SimpleCounter</servlet-class>
   <init-param>
       <param-name>initial</param-name>
       <param-value>1000</param-value>
   </init-param>
 </servlet>

</web-app>

我在web.xml中注册了一个名为“Counter”的servlet,并在加载servlet时将参数“initial”配置为1000。并在servlet运行时在init()方法中显示参数:

package com;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Counter")
public class SimpleCounter extends HttpServlet {

/**
 * @see HttpServlet#HttpServlet()
 */
private static final long serialVersionUID = 1L;

int count;

public void init() throws ServletException{

    //No luck with the saved state, check for an init parameter
    String initial = this.getInitParameter("initial");
    System.out.println(initial);
    try{
        count = Integer.parseInt(initial);

        //print out the parameter
        System.out.println(count);

        return;
    }catch(NumberFormatException e){
        //default to initial count of '0'
        count = 0;
    }

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
                                        throws ServletException, IOException {
    int local_count;

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    synchronized(this){
        local_count = count++;
    }

    out.println("Since loading (and with a possible initialization parameter figured in)");
    out.println("SimpleCounterServlet has been accesed " + local_count + " times.");
}

public void destroy(){
    super.destroy();
}

}

然而,当加载servlet时,控制台打印出 null ,我仍然无法找出一些问题。

Q1:如果我在我的代码中指定了servlet注释 @WebServlet(“/ Counter”),我是否还应该在web.xml文件中指定?< / p>

Q2:如何显示web.xml文件中配置的参数?

Q3:因为示例代码来自一本书,我不确定web.xml是指WEB-INF下的那个还是tomcat服务器下的那个如下图所示: enter image description here

3 个答案:

答案 0 :(得分:0)

Q1 :注释和XML是 - 或者不是两者。许多新的J2EE应用程序使用Annotations因为它很容易。但是也有使用XML的应用程序。 XML的优点是可以在不必编译整个应用程序的情况下进行更改。

Q2 :不确定您的意思,但如果您使用XML映射servlet,则无需在Java代码中指定。

Q3 :它指的是web.xml内的WEB-INF

答案 1 :(得分:0)

在这里,我将对这个问题进行一些详细的测试。

我写了一段代码来在 init()方法中显示servlet信息,如下所示:

String name = this.getServletName();
System.out.println("Servelet Name: " + name);

并打印出来

五月 09, 2015 4:30:02 下午 org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/ServletPractice] has started  
五月 09, 2015 4:30:02 下午 org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/ServletPractice] is completed
Servelet Name: com.SimpleCounter

所以我在web.xml中猜到我没有为标签 servlet-name 设置正确的名称。然后我将标签 servlet-name servlet-class 的内容更改为 com.SimpleCounter 。我们应该警惕servlet注释 @WebServlet(“/ Counter”)与标记 servlet-name 的内容不应该相同。或者无法显示网页。

这是我更新过的web.xml文件:

<servlet>
<servlet-name>com.SimpleCounter</servlet-name>
<servlet-class>com.SimpleCounter</servlet-class> 
<init-param>
    <param-name>initial</param-name>
    <param-value>1000</param-value>
</init-param>

答案 2 :(得分:0)

要在web.xml文件中打印为servlet定义的初始化参数,可以在init方法中使用以下代码片段。

    Enumeration<String> initializationParameters=this.getInitParameterNames();
    while(initializationParameters.hasMoreElements()){
        String parameterName=initializationParameters.nextElement();
        System.out.println("Parameter Name:"+parameterName+"    Parameter Value:"+this.getInitParameter(parameterName));
    }