Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-dev] How does a minimal Servlet 3.0 web application look like?

I'm trying to create a Servlet 3.0 Web application. I've never done anything
like this before (well I've looked a bit into rails and noir, but right now
I'm required to use a pure java solution as this is for a university
project).

Basically I read that with Servlet 3.0 I just need the @WebServlet
annotation, and to implement Servlet in one way or another to make something
a servlet, without further configuration by means of web.xml or anything
like that. 

That's all great and all, except it doesn't work. I wrote something like
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(name="HelloWorldServlet",value="/hello", loadOnStartup=1)
public class ServletTest extends HttpServlet {
    private static final java.nio.charset.Charset UTF8 =
java.nio.charset.Charset.forName("UTF8");
    
    @Override
    public void doGet(HttpServletRequest req,
                          HttpServletResponse res) throws ServletException,
IOException
    {   
        byte[] HTML = "<html><head><title>Hello World!</title></head><body>
IT FUCKING WORKED!
</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();
    }   
}


And put that under WEB-INF/classes in a war file, and then putting that war
file in my webapps folder in Jetty (I'm using Jetty  9).

I only get static html content though (I put an index.html file under the
root of my war), and get 404 on /hello, which I'd have suspected to give me
the response I create in that servlet. Please tell me what I'm doing wrong
and how I can make it be less wrong.



--
View this message in context: http://jetty.4.n6.nabble.com/How-does-a-minimal-Servlet-3-0-web-application-look-like-tp4959725.html
Sent from the Jetty Dev mailing list archive at Nabble.com.


Back to the top