7Feb/10Off
Servlet 3.0 Without Configuration Hell
Servlets 3.0 become easier.
Lets try simple InfoServlet example.
Our web.xml will look like:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>info</display-name> </web-app>
And our InfoServlet.java would look like:
package example;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = { "/info" })
public class InfoServlet extends HttpServlet {
private static final long serialVersionUID = 5585664118773284494L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
PrintWriter out = response.getWriter();
out.println("Info Servlet!");
out.close();
}
}
And that is it.
Why "InfoServlet"? Because it can be easily combined with earlier post about Maven versioning. In InfoServlet you can read a file with a version and show on a web page.