0

I have a war file containing:

META-INF
|___MANIFEST.MF
WEB-INF
|___web.xml
|___classes
    |____servlet
         |____StarterServlet.class

my web.xml looks like:

<?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_2_5.xsd"
    version="2.5">

    <display-name>WAR</display-name>

    <servlet>
        <display-name>Starter Servlet</display-name>
        <servlet-name>StarterServlet</servlet-name>
        <servlet-class>servlet.StarterServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>StarterServlet</servlet-name>
        <url-pattern>/starter</url-pattern>
    </servlet-mapping>

</web-app>

And my servlet code is:

package servlet;

import java.io.IOException;
import java.io.PrintWriter;

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

public class StarterServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    public void init(ServletConfig servletConfiguration) throws ServletException {

        super.init(servletConfiguration);

        System.out.println("SERVLET STARTED!");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("DOGET!");

        PrintWriter printWriter = response.getWriter();

        printWriter.println("Hello world");
    }
}

Also, my runtime name is: "war", so i am going to lookup for my context root with this URL:

http://localhost:8080/war/starter.

enter image description here

The main problem is that my servlet does not get initialized, and i can't see any trace on jboss log files,although my war gets deployed correctly.

Where am i doing it wrong?

Thanks

Francesco Rizzi
  • 631
  • 6
  • 24
  • what do you mean servlet does not get initialized? When you go to the url /starter with your browser, what error are you getting? – Jonathan Laliberte Oct 29 '18 at 22:52
  • A 404 error. I can see that my servlet is not initialized because the init method is not invoked and i can't see any "SERVLET STARTED!" Log inside server.log – Francesco Rizzi Oct 30 '18 at 06:05

2 Answers2

1

If you want to change the context root to http://localhost:8080/war/ you have to add a jboss-web.xml to WEB-INF/jboss-web.xml with content:

<jboss-web>
    <context-root>war</context-root>
</jboss-web>

Also leave the default values in Name and Runtime Name when you upload the war.

In the jboss log you should then see a message with Registered web context: /war

wirnse
  • 1,026
  • 8
  • 7
0

Thanks to the answer of @wirnse i got the solution.

Name and runtime name must always contain the extension of the file which is being deployed.

For example: application.war must have name and runtime name following this pattern:

^[a-zA-Z]+.(ear|war|jar)

myapp.war or war.war

Francesco Rizzi
  • 631
  • 6
  • 24
  • 1
    Just some advice. There are 2 other ways to deploy a war. With the cli (there you can also use runtime-name) and to copy it into the deployments folder. With the second your context root is set to your war filename, if there is no jboss-web.xml. While development you maybe want to use your IDE for deployment. I don't know about netbeans, but in eclipse you can run your project which makes an exploded deployment to the deployments folder. – wirnse Oct 30 '18 at 13:19