【问题标题】:Errors causing from web xml fileWeb xml 文件导致的错误
【发布时间】:2023-03-09 04:07:01
【问题描述】:

我在 web.xml 文件中遇到问题。

我收到错误消息,“本地主机上的服务器 Tomcat v8.0 服务器无法启动。”当我尝试运行我的应用程序时。但是当标签用于web.xml文件时可以绕过这个错误,如下所示。

但新问题是,当我在 web.xml 中使用上下文参数时,它们不能从侦听器类中使用。

ServletContext sc=event.getServletContext();
String database= sc.getInitParameter("Database");

数据库值始终为空。 当元素标签和标签被删除时,数据库字符串会提取正确的值。但是标签需要 servlet 标签才能工作。

谁能告诉我一个解决方案。 下面我给出了 web.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<element>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>A</display-name>

  <servlet>
    <servlet-name>login</servlet-name>
    <servlet-class>com.manage.control.EnrollmentServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/EnrollmentServlet</url-pattern>
  </servlet-mapping>


  <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>



  <listener>
    <listener-class>com.test.listener.DeatabaseNameListener</listener-class>
  </listener>

  <context-param>
    <param-name>Database</param-name>
    <param-value>jdbc:mysql://localhost:3307/studentmanagement</param-value>
  </context-param>

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

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

</web-app>
</element>

【问题讨论】:

  • 我希望您需要对该数据库参数值进行编码。它可能不喜欢 // 等或转义。 jdbc:mysql:////localhost:3307//学生管理
  • 嗨,安东尼。不,它没有用。当元素标签不存在并且 标签不存在时,这可以正常工作。侦听器类完美地采用了上下文参数。我尝试了你的建议。
  • jdbc%3Amysql%3A%2F%2Flocalhost%3A3307%2Fstudentmanagement 但它给出了同样的错误
  • element 标签是什么?
  • 我从这个视频中了解了 标签。 youtube.com/watch?v=spTPIwFpHvE&t=1s 否则我会收到错误“本地主机上的服务器 Tomcat v8.0 服务器无法启动。”并且什么也做不了。但是放的时候不能访问上下文参数。

标签: servlets jakarta-ee web.xml


【解决方案1】:

没有元素这样的标签。您的 web.xml 应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">
  <display-name>A</display-name>

  <context-param>
    <param-name>Database</param-name>
    <param-value>jdbc:mysql://localhost:3307/studentmanagement</param-value>
  </context-param>

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

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

  <listener>
    <listener-class>com.test.listener.DeatabaseNameListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>login</servlet-name>
    <servlet-class>com.manage.control.EnrollmentServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/EnrollmentServlet</url-pattern>
  </servlet-mapping>

  <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>

</web-app>

请注意,上下文参数更早,名称空间声明指示 Servlet 3.1。过去的顺序很重要 - 我认为它不再起作用,但我仍然命令他们遵循旧规范。

【讨论】:

    猜你喜欢
    • 2011-12-25
    • 1970-01-01
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多