eclipse - Servlet 初始化参数总是显示为空

标签 eclipse maven tomcat servlets jakarta-ee

我是 java2ee 的绝对初学者,我需要一些帮助。 我正在关注 youtube-tutorial ( https://www.youtube.com/watch?v=sB7-ChpjWVw ),我也有一些关于该主题的真正基础知识。

重点是: 无论我如何尝试,每当我尝试将 web.xml 中的 servlet-init-parameter 显示到我的 servlet 中时,它总是以显示空值结束。 奇怪的事实:当我对上下文参数执行相同操作时,不会出现任何问题。

我已经尝试覆盖 init() 方法而不是 init(ServletConfig config) 方法。 如果我覆盖 init(ServletConfig config) 方法,我调用 super.init(config)。 我试图将所有内容移动到 doGet() 方法中。 该项目已设置为动态 Web 项目。 我已经使用了在 StackOverflow 上找到的每个版本的 getInitParameter(String) 方法。

我想到的是 pom 文件中可能有问题(可能是旧的依赖项?缺少插件?)。

这是 web.xml、servlet 和 pom 的代码。

请救救我,我已经用了 48 小时了。

这是servlet

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

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

/**
 * Servlet implementation class MyFirstServlet
 */
@WebServlet("/MyFirstServlet")
public class MyFirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

private ServletConfig servletConfig;
private String servletInitParameterValue;
private String contextParameterValue;

private String loginTime;

/**
 * @see HttpServlet#HttpServlet()
 */
public MyFirstServlet() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see Servlet#init(ServletConfig)
 */
public void init(ServletConfig config) throws ServletException {

    System.out.println("Basic servlet***********************init()******************");
    super.init(config);
    this.servletConfig=config;

    //use ServletConfig to get servlet init parameters
    servletInitParameterValue =  getServletConfig().getInitParameter("parInit");
    System.out.println("servletInitParameterValue = "+ servletInitParameterValue);


    //use ServletConfig to get context init parameters
    contextParameterValue = config.getServletContext().getInitParameter("ContextParameter");
    System.out.println("contextParameterValue = "+ contextParameterValue );

}





/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    System.out.println("Basic servlet***********************doGet()******************");




    // get request parameter
    String requestParamValue = request.getParameter("param");

    //set response content type
    response.setContentType("text/html");


    //create web-page and write a message inside of it
    response.getWriter().append("Served at: ").append(request.getContextPath());
    PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<body>");
        out.println("<br>");
        out.println("<h3>"+this.getSerlvletInfo() + " invoked at " + new Date().getDate()+". "+ " i got init param "+ servletInitParameterValue
                + " and context param = "+ contextParameterValue + ".<br>"+"You logged in passing parameter value param as "+ requestParamValue
                +"</h3>"+ ".<br>");

        out.println("<a href='ExeServlet'>vai a seconda servlet</a>");
        out.println("</html>");
        out.println("</body>");


}






/**
 * @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);
}







/**
 * @see Servlet#destroy()
 */
public void destroy() {
    System.out.println("i'm destroyed!");
}


public String getSerlvletInfo () {
    return this.servletConfig.getServletName();
}

public ServletConfig getServletConfig () {
    return servletConfig;
}


}

这是 web.xml

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
"http://java.sun.com/dtd/web-app_2_3.dtd" >




<web-app>
  <display-name>Archetype Created Web Application</display-name>


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

  <context-param>
        <param-name>ContextParameter</param-name>
        <param-value>IO SONO IL PARAMETRO DI CONTESTO 2</param-value>
    </context-param>



  <servlet>
         <servlet-name>MyFirstServlet</servlet-name>
     <display-name>MyFirstServlet</display-name>
     <description></description>
    <servlet-class>servlet.MyFirstServlet</servlet-class>
    <init-param>
        <param-name>parInit</param-name>
    <param-value>IO SONO IL PARAMETRO DELLA SERVLET</param-value>
    <description></description>
    </init-param>
 </servlet>
 <servlet>
    <servlet-name>ExeServlet</servlet-name>
<display-name>ExeServlet</display-name>
<description></description>
<servlet-class>servlet.ExeServlet</servlet-class>
<init-param>
    <param-name>company</param-name>
    <param-value>thewebbcompany</param-value>
    <description></description>
    </init-param>
   </servlet>
  <servlet-mapping>
    <servlet-name>MyFirstServlet</servlet-name>
    <url-pattern>/servlet/MyFirstServlet</url-pattern>
   </servlet-mapping>
  <servlet-mapping>
    <servlet-name>ExeServlet</servlet-name>
    <url-pattern>/ExeServlet</url-pattern>
  </servlet-mapping>
 </web-app>

pom 来了

                <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
              <modelVersion>4.0.0</modelVersion>
              <groupId>com.theStartupCentral</groupId>
              <artifactId>pizzashoppingcart</artifactId>
              <packaging>war</packaging>
              <version>0.0.1-SNAPSHOT</version>
              <name>pizzashoppingcart Maven Webapp</name>
              <url>http://maven.apache.org</url>




              <dependencies>
                <dependency>
                  <groupId>junit</groupId>
                  <artifactId>junit</artifactId>
                  <version>3.8.1</version>
                  <scope>test</scope>
                </dependency>
                <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>5.1.3.RELEASE</version>
            </dependency>
                <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.6</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>4.0.1</version>
                <scope>provided</scope>
            </dependency>
            <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>

            <!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
            <dependency>
                <groupId>javax.validation</groupId>
                <artifactId>validation-api</artifactId>
                <version>1.0.0.GA</version>
            </dependency>



              </dependencies>

              <build>
                <plugins>
                    <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                         <fork>true</fork>
                    <executable>C:\Program Files\Java\jdk1.8.0_191\bin\javac</executable>    
                    </configuration>
                </plugin>
                </plugins>
            </build>



            </project>

最佳答案

去掉注解@WebServlet("/MyFirstServlet")

只保留 doGet() 和 doPost() 方法并删除所有其他变量和方法。

使用以下代码访问初始化参数。

public void init()  {
    String servletInitParameterValue =  getServletConfig().getInitParameter("parInit");
    System.out.println("servletInitParameterValue = "+ servletInitParameterValue);

    String contextParameterValue = getServletContext().getInitParameter("ContextParameter");
    System.out.println("contextParameterValue = "+ contextParameterValue ); }

关于eclipse - Servlet 初始化参数总是显示为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54262402/

相关文章:

java - Maven 从终端运行,但不能从 Yosemite 中的 Eclipse 运行

maven - 如何将闭源库发布到jCenter?

maven - Jenkins pipeline - maven 安装依赖项目

java - Maven、Vert.x 和 JodaTime 库 - NoSuchMethodError

apache - 配置 SSL Tomcat 7 导入 comodo apache 证书

java - Tomcat 将 war 名称添加到上下文

eclipse - 在 Eclipse 中禁用实时错误和警告检查

android - Eclipse 没有 java 文档来显示有关类和方法的信息。如何附加那些? +安卓

java - 场景构建器导入到 Eclipse

java - 使用 IP 地址作为我的 Web 应用程序的 URL 时无法检索数据