8.1 Introduction to servlet annotations
Starting with version 7, Tomcat began to support the Servlet API 3.0 web servlet specification . In particular, a package appeared in it called javax.servlet.annotation
. It contains various types of annotations that can be used to annotate a servlet class. If you use an annotation, then the deployment descriptor (web.xml) is not required.
List of the most useful annotations:
annotation | Description | |
---|---|---|
1 | @WebServlet |
Declares a servlet |
2 | @WebInitParam |
Specifies an initialization parameter |
3 | @WebFilter |
Declares a web filter |
4 | @WebListener |
Declares the web lisener |
5 | @ServletSecurity |
Allows you to configure security settings |
Example:
@WebServlet( urlPatterns = {"/api/*"} )
public class Example extends HttpServlet {
protected void doGet( HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Hello World!");
}
}
One annotation is enough to declare a servlet and map it to serve all requests that will come to the URL given by the /api/*
.
8.2 Setting up servlet mapping
The annotation has @WebServlet
many parameters that allow very flexible configuration of the servlet it describes. Let's take a look at the main ones:
Attribute | Description | |
---|---|---|
1 | name |
Servlet unique name (as in web.xml) |
2 | displayName |
Human readable servlet name |
3 | description |
Servlet Description |
4 | value |
Sets the url for mapping |
5 | urlPatterns |
Specifies a list of urls to map (used instead of value) |
6 | initParams |
Allows you to set the start parameters of the servlet |
7 | asyncSupported |
Specifies that the servlet can run asynchronously (HTTP/2) |
8 | loadOnStartup |
Sequence number to control servlet start priority |
9 | smallIcon |
Sets a small servlet icon |
10 | largeIcon |
Sets a large servlet icon |
There are several interesting points here.
First, note that value
and urlPatterns
are interchangeable attributes. They both allow you to specify a list of URLs that the servlet should map to.
Secondly, the parameter asyncSupported
indicates whether the servlet will correctly process asynchronous requests over the HTTP/2 protocol.
And the third important attribute is initParams
, it allows you to set a list of parameters that will be placed in the ServletContext.
Example:
@WebServlet(
urlPatterns = {"/sendFile", "/uploadFile"},
loadOnStartup = 1,
asyncSupported = true,
initParams = {
@WebInitParam(name = "saveDir", value = "c:/uploaded"),
@WebInitParam(name = "allowedTypes", value = "jpg,gif,png")
}
)
public class ImageUploadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
String saveDir = getInitParameter("saveDir");
String fileTypes = getInitParameter("allowedTypes");
PrintWriter writer = response.getWriter();
writer.println("saveDir = " + saveDir);
writer.println("fileTypes = " + fileTypes);
}
}
8.3 Setting up filter mapping
As mentioned above, filters are a utility type of servlets, and they are configured like servlets. Here are the attributes an annotation has @WebFilter
:
Attribute | Description | |
---|---|---|
1 | filterName |
Filter unique name (as in web.xml) |
2 | displayName |
Human readable filter name |
3 | description |
Filter Description |
4 | value / urlPatterns |
Sets a list of urls to map |
5 | dispatcherTypes |
Specifies a list of DispatcherTypes |
6 | servletNames |
Specifies a list of servlets to apply to |
7 | initParams |
Allows you to set the starting parameters of the filter |
8 | asyncSupported |
Specifies that the filter can run asynchronously (HTTP/2) |
9 | smallIcon |
Sets a small filter icon |
10 | largeIcon |
Sets a large filter icon |
An example of a filter that intercepts all requests to specific servlets :
@WebFilter(servletNames = {"MyOwnServlet", "UploadServlet"})
public class MyFilter implements Filter {
// implements Filter's methods here...
}
An example of a filter given multiple parameters :
@WebFilter(
urlPatterns = "/uploadFilter",
initParams = @WebInitParam(name = "fileTypes", value = "doc;xls;zip;txt;jpg;png;gif")
)
public class UploadFilter implements Filter {
// implements Filter's methods here...
}
An example of a filter given types for RequestDispatcher :
@WebFilter(
urlPatterns = "/admin",
dispatcherTypes = {DispatcherType.REQUEST, DispatcherType.FORWARD}
)
public class MyFilter implements Filter {
// implements Filter's methods here...
}
You will learn more about the purpose of filters and servlets in the following lectures.
GO TO FULL VERSION