0

I'm using an embeddable Tomcat in a maven project structure
(like here: https://devcenter.heroku.com/articles/create-a-java-web-application-using-embedded-tomcat)
but im not deploying to Heroku.
I can access the index.jsp (even before adding a web.xml) at localhost:8080/
But I can't workout how to access my servlet (keep getting a 404, even before adding web.xml).
Tried at localhost:8080/Archery
Tried at localhost:8080/servlets/servlet.ArcheryShootServlet
Tried at localhost:8080/servlet/servlet.ArcheryShootServlet
Tried at localhost:8080/servlet.ArcheryShootServlet
Tried at localhost:8080/target/Archery
Tried at localhost:8080/target/ArcheryShootServlet
Tried at localhost:8080/target/servlet.ArcheryShootServlet

I've tried putting them into the resources folder that was already part of the project.

Project Structure

I've tried adding a webResources folder, and adding it to the
pom file configuration:

   <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.heroku.sample</groupId>
  <artifactId>embeddedTomcatSample</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>embeddedTomcatSample Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
    <tomcat.version>7.0.34</tomcat.version>
  </properties>
  <dependencies>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
        <version>${tomcat.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-logging-juli</artifactId>
        <version>${tomcat.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <version>${tomcat.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jasper</artifactId>
        <version>${tomcat.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jasper-el</artifactId>
        <version>${tomcat.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jsp-api</artifactId>
        <version>${tomcat.version}</version>
    </dependency>
    <dependency>
      <groupId>org.swinglabs</groupId>
      <artifactId>swing-layout</artifactId>
      <version>1.0.3</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>embeddedTomcatSample</finalName>
    <plugins>
    <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>1.1.1</version>
        <configuration>
        <webResources>
                    <resource>
                        <directory>WebContent/WEB-INF</directory>
                        <includes>
                            <include>**/*.properties</include>
                            <include>**/*.xml</include>
                            <include>**/*.css</include>
                            <include>**/*.html</include>
                        </includes>
                    </resource>
                </webResources>
        </configuration>
    </plugin>   
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>appassembler-maven-plugin</artifactId>
            <version>1.1.1</version>
            <configuration>
                <assembleDirectory>target</assembleDirectory>
                <programs>
                    <program>
                        <mainClass>launch.ArcheryServer</mainClass>
                        <name>webapp</name>
                    </program>
                </programs>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>assemble</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
  </build>
</project>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>ArcheryShootServlet</servlet-name>
        <servlet-class>servlet.ArcheryShootServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ArcheryShootServlet</servlet-name>
        <url-pattern>/Archery</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>/WEB-INF/index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

context.xml

 <?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/Archery"/>

ArcheryShootServlet.java

    package servlet;

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

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(
        name = "Archery", 
        urlPatterns = {"/Archery"}
    )

public class ArcheryShootServlet extends HttpServlet {
protected void processRequest(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
String xmlSent = req.toString();    
System.out.println(xmlSent);
    ServletOutputStream out = resp.getOutputStream();
    PrintWriter test = resp.getWriter();
test.write("hello");
    out.write("hello heroku".getBytes());
out.write(xmlSent.getBytes());

    out.flush();
    out.close();
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. doGet and doPost call processRequest">...

}

jsky
  • 2,225
  • 5
  • 38
  • 54

1 Answers1

2

Your application context path is /Archery (defined in context.xml)

Your servlet path is also /Archery (urlPatterns property of @WebServlet) It's also duplicated by your web.xml

So, your URL should be localhost:8080/Archery/Archery

First server[:port], followed by context path, followed by servlet path

Anyway, you will have to fix project structure in order for this to work. If you are following Maven conventions web resources directory should be src/main/webapp The web.xml (and context.xml) should be put under WEB-INF directory under this directory.

After you have packaged the war, just make sure these files are there (under WEB-INF)

Ori Dar
  • 18,687
  • 5
  • 58
  • 72
  • just tried this path, still no go :( Just found this http://stackoverflow.com/questions/6535676/webservlet-annotation-with-tomcat7 which says xml needs to conform to 3.0 for annotations to work. So it should just ignore mine. But i will try without them. – jsky Jul 31 '13 at 10:36
  • still 404 with the annotations commented out. – jsky Jul 31 '13 at 10:40
  • do i need a global web.xml? – jsky Jul 31 '13 at 10:42
  • Please see my edited response. This should still work because you also have a servlet mapping in web.xml – Ori Dar Jul 31 '13 at 10:53
  • I need a tutorial in Maven conventions. Moving under webapp/WEB-INF folder did the trick, however, it is accessing at `localhost:port/Archery` even with the annotations there. So i guess it is ignoring them because of the version. or maybe just cos its duplicate. thanks @orid – jsky Jul 31 '13 at 11:07
  • actually i think what happens is that Alias is a variable, and it just gets overwritten even if you set it twice and even if you have both context and servlet paths the alias is the final path route. – jsky Aug 01 '13 at 01:55