-1

I'm doing a project with registration/login forms with tomcat7 for two types of users, and i created everything: login, registration1, registration2, and some validating class for each one. I have some problems in my web.xml, saying "invalid content was found starting with element 'servlet-name'.I have all the files in some directories, with the exception of the 2 registration in /src, and the validating ones in /login/(all servlets + logins). Here's my code:

    <?xml version="1.0" encoding="UTF-8"?>
<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">
  <servlet>
    <servlet-name>login</servlet-name>
    <servlet-class>Login</servlet-class>
    <servlet-name>RegisterMedico</servlet-name>
    <servlet-class>/progettoTesi/src/RegisterMedic</servlet-class>
    <servlet-name>RegisterUser</servlet-name>
    <servlet-class>progettoTesi/src/RegisterUser</servlet-class>

  </servlet>
  <servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>progettoTesi/WebContent/login</url-pattern>
    <servlet-name>RegisterMedic</servlet-name>
    <url-pattern>progettoTesi/src/RegisterMedic</url-pattern>
    <servlet-name>RegisterUser</servlet-name>
    <url-pattern>progettoTesi/src/RegisterUser</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

I can't understand what my error is: the name of my servlets are correct. Is this a java problem? How can i solve this issue?

Termininja
  • 6,620
  • 12
  • 48
  • 49
Feanor
  • 21
  • 5

1 Answers1

2

You're trying to define several servlets within the same <servlet> tag and that is not going to work. Give each servlet its own <servlet> and <servlet-mapping> tag.

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <servlet>
    <servlet-name>login</servlet-name>
    <servlet-class>Login</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>RegisterMedico</servlet-name>
    <servlet-class>RegisterMedic</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>RegisterUser</servlet-name>
    <servlet-class>RegisterUser</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>RegisterMedic</servlet-name>
    <url-pattern>/RegisterMedic</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>RegisterUser</servlet-name>
    <url-pattern>/RegisterUser</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

Alternatively, you can use @WebServlet annotations on your servlet classes so you don't need to configure them in the web.xml at all. Example here: @WebServlet annotation with Tomcat 7

Also the url-pattern and servlet-class are very wrong. The servlet-class should be a fully qualified classname (package + classname). The url pattern is literally the pattern in the url that must "map" to the servlet. Example: an url-pattern "/beep" would map to "http://localhost:8080/beep".

Community
  • 1
  • 1
Gimby
  • 5,095
  • 2
  • 35
  • 47
  • Really thanks! But now, whenever i put something inside my servlets, i get the 404 HTTP status saying i can't find /indexAvanz ecc ecc. Is this a problem relating to servlets? I post my registrationAvanz code – Feanor Aug 03 '16 at 13:11
  • @Feanor the servlet class and url-pattern are also very wrong now that I look at it. – Gimby Aug 03 '16 at 13:12
  • @Feanor please don't post follow-up questions as an answer, that is not how stackoverflow works. You should remove it. I edited my answer with fixed class name and url-pattern. – Gimby Aug 03 '16 at 13:19
  • i'm sorry, i deleted the answer. But how come do i have that 404 problem? – Feanor Aug 03 '16 at 13:22
  • @feanor Basically because you're just guessing how to configure this stuff and blaming java when it won't work. I did some changes in the web.xml content in my answer, that should work better. If not, do consider actually studying this. I'm debugging for you here without being able to run your application. – Gimby Aug 03 '16 at 13:48
  • I really appreciate this, and i'm entirely thanking you. Whenever i start that, i can easily reach the html/jsp pages for login or register, but when i log in or register, i get the 404 problem. Is that related to my web.xml? Because i can't find some useful guides for this – Feanor Aug 03 '16 at 13:58
  • @Feanor then you're not looking, there are useful guides all over the internet. Consider reading this free book: http://pdf.coreservlets.com . That's basically all I can say, I cannot debug your problems for you. I have no idea how you setup your server, deploy your application or where all the files are. – Gimby Aug 03 '16 at 14:35