我是java2ee的绝对初学者,我需要一些帮助。 我正在关注youtube教程(https://www.youtube.com/watch?v=sB7-ChpjWVw),并且我对该主题也有一些非常基本的了解。
重点是: 无论我如何尝试,每当尝试显示从web.xml到servlet的servlet-init-parameter时,它总是以空值结尾。 奇怪的事实:对上下文参数进行相同操作不会发生任何问题。
我尝试覆盖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>
答案 0 :(得分:0)
删除注释@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 ); }
答案 1 :(得分:0)
使用@WebServlet批注或web.xml
元素。不要混在一起。 https://www.codejava.net/java-ee/servlet/webservlet-annotation-examples展示了在注释中指定初始化参数的示例。