0

I'm having an issue in web.xml file.

I'm getting the error, 'Server Tomcat v8.0 Server at localhost failed to start.' when I try to run my application. But this error can be bypassed when tag is used to web.xml file as shown below.

But the new issue is, when I context parameters are used in web.xml , they cannot be used from the listener class.

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

The database value always recieved as null. When element tags and tags are removed database string extracts the correct value. But tag needs servlet tags to be worked.

Can someone tell me a solution for this. Here below I have given the web.xml file

<?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>
user3789200
  • 1,166
  • 2
  • 25
  • 45
  • I expect you need to Encode that database param-value. It probably does not like the //, etc. Or escape. jdbc:mysql:////localhost:3307//studentmanagement – Anthony Horne Jun 10 '17 at 17:37
  • Hi Anthony. No it did not work. When element tag is not there and tags are not there this works fine. context parameters are taking perfectly by listner class. I tried what u suggested. – user3789200 Jun 10 '17 at 17:48
  • jdbc%3Amysql%3A%2F%2Flocalhost%3A3307%2Fstudentmanagement but it gives the same error – user3789200 Jun 10 '17 at 17:49
  • What's the `element` tag? – Dave Newton Jun 10 '17 at 17:59
  • I got to know about the tag from this video. https://www.youtube.com/watch?v=spTPIwFpHvE&t=1s otherwise I'm getting the error 'Server Tomcat v8.0 Server at localhost failed to start.' and can't do anything. But when it is put cannot access context parameters. – user3789200 Jun 10 '17 at 18:04
  • you meant the ending tag. No it doesn't work – user3789200 Jun 10 '17 at 18:05
  • I'm stuck with this for the whole day. Have no idea on how to proceed and where I am missing.. I'm using eclipse – user3789200 Jun 10 '17 at 18:09
  • if you, for the sake of test remove the second and third context-param, does it still fail to start? – Anthony Horne Jun 10 '17 at 18:13
  • When tag is used, the error is not coming. but context parameters cannot be accessed. its coming as null when tag is used. for the question you asked. yeas even when the 2nd and 3rd context-params are removed still the null value is parsing – user3789200 Jun 10 '17 at 18:18

1 Answers1

0

There is no such tag as element. Your web.xml should look like:

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

Notice that the context parameters are earlier and the name space declaration indicates Servlet 3.1. The order used to matter - I don't think it does any more but I still order them to follow the old spec.

stdunbar
  • 16,263
  • 11
  • 31
  • 53