how to access tomcat servlet in maven project structure with embeddable tomcat -
i'm using embeddable tomcat in maven project structure
(like here: https://devcenter.heroku.com/articles/create-a-java-web-application-using-embedded-tomcat)
but im not deploying heroku.
can access index.jsp (even before adding web.xml) @ localhost:8080/
can't workout how access servlet (keep getting 404, before adding web.xml).
tried @ localhost:8080/archery
tried @ localhost:8080/servlets/servlet.archeryshootservlet
tried @ localhost:8080/servlet/servlet.archeryshootservlet
tried @ localhost:8080/servlet.archeryshootservlet
tried @ localhost:8080/target/archery
tried @ localhost:8080/target/archeryshootservlet
tried @ localhost:8080/target/servlet.archeryshootservlet
i've tried putting them resources folder part of project.
i've tried adding webresources folder, , adding
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 , dopost call processrequest">...
}
your application context path /archery
(defined in context.xml
)
your servlet path /archery
(urlpatterns
property of @webservlet
) it's duplicated web.xml
so, url should localhost:8080/archery/archery
first server[:port], followed context path, followed servlet path
anyway, have fix project structure in order work. if following maven conventions web resources directory should src/main/webapp
web.xml
(and context.xml
) should put under web-inf directory under directory.
after have packaged war, make sure these files there (under web-inf)
Comments
Post a Comment