Reputation: 15703
According to the Quickstart section in the Documentation, a somewar.war file is in the default configuration deployed as localhost:8080/somewar/.
According to this, all I have to do is something this:
package de.swt1321.servlet;
import java.io.OutputStream;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns={"/","/index.html"}, loadOnStartup=1)
public class ServletTest extends HttpServlet {
private static final java.nio.charset.Charset UTF8 = java.nio.charset.Charset.forName("UTF8");
@Override
protected void doGet(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException
{
byte[] HTML = "<html><head><title>Hello World!</title></head><body><h1>IT WORKED!</h1></body></html>".getBytes(UTF8);
res.setStatus(HttpServletResponse.SC_OK);
res.setHeader("content-type","text/html;charset=utf8");
res.setIntHeader("content-length",HTML.length);
OutputStream os = res.getOutputStream();
os.write(HTML);
os.flush();
}
}
Except that he conveniently went from "create the class" to "deploy the project". And this is where I'm stuck - apparently I'm doing something wrong. I'm using buildr to package the whole thing in a war, and end up with something like this:
ServletTest.war:
META-INF -> MANIFEST.MF
WEB-INF ->
classes ->
de ->
swt1321 ->
servlet->
ServletTest.class
lib -> javaee-web-api-6.0.jar
Putting that into the webapps folder, I'd expect to get the html from my response. Instead, I get a page that contains 2 links called META-INF and WEB-INF that lead to 404 pages.
I'm afraid I'm missing something that I should be doing here, unfortunately all I can find online are code snippets that jump right to "now then package the whole thing as a web application..." and the jetty documentation seems to assume I already know how an web application looks like (and seems to be mostly concerned with various configuration options, which I actually might give a crap about if I actually got to get the damn thing working in the first place).
What SHOULD I be doing to get it to work?
EDIT
Changed the title because while I initially tested this with Jetty 9, it doesn't work with Tomcat 7 either, with essentially same problem - apparently the application is recognized (shows up in the manager) but the servlet doesn't seem to work (if it's being used at all). I'm pretty frustrated at this point.
Upvotes: 0
Views: 331
Reputation: 7182
jetty-8 and jetty-9 support servlet 3.0, but you must make sure annotation scanning is enabled in the start.ini, edit that file, add 'annotations' to the OPTIONS line and uncomment the jetty-annotations.xml line
also you seem to be missing a web.xml file in your war
we seem to get this question a fair amount so I'll see about enabling annotations by default, though they make startup time so much longer then it needs to be since most people don't bother with them it seems so why penalize everyone...we'll give it some thought.
[edit] imo if you are starting out you are better off learning the details behind the annotations like how to configure servlets in the web.xml...annotations are just a bit of magic on top of the servlet-api
Upvotes: 1