Java Servlet @WebServlet Annotation Example
- Details
- Written by Nam Ha Minh
- Last Updated on 27 June 2019   |   Print Email
Syntax of @WebServlet Annotation:
@WebServlet( attribute1=value1, attribute2=value2, ... ) public class TheServlet extends javax.servlet.http.HttpServlet { // servlet code... }
Attributes of @WebServlet Annotation:
Name | Type | Required | Description |
value or urlPatterns | String[] | Required | Specify one or more URL patterns of the servlet. Either of attribute can be used, but not both. |
name | String | Optional | Name of the servlet |
displayName | String | Optional | Display name of the servlet |
description | String | Optional | Description of the servlet |
asyncSupported | boolean | Optional | Specify whether the servlet supports asynchronous operation mode. Default is false. |
initParams | WebInitParam[] | Optional | Specify one or more initialization parameters of the servlet. Each parameter is specified by @WebInitParam annotation type. |
loadOnStartup | int | Optional | Specify load-on-startup order of the servlet. |
smallIcon | String | Optional | Specify name of the small icon of the servlet. |
largeIcon | String | Optional | Specify name of the large icon of the servlet. |
Some Examples with @WebServlet Annotation:
- A servlet is annotated with only the URL pattern:
import java.io.IOException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/processForm") public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.getWriter().println("Hello"); } }
Here the servlet MyServlet is mapped to the URL pattern /processForm. When accessing this servlet, it will return a “Hello” message. - A servlet is annotated with multiple URL patterns:
import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; @WebServlet(urlPatterns = {"/sendFile", "/uploadFile"}) public class UploadServlet extends HttpServlet { // implement servlet doPost() and doGet()... }
Here the servlet UploadServlet can be accessed through two URL patterns: /sendFile and /uploadFile. - Declare a servlet with additional information:
import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; @WebServlet( name = "MyOwnServlet", description = "This is my first annotated servlet", urlPatterns = "/processServlet" ) public class MyServlet extends HttpServlet { // implement servlet doPost() and doGet()... }
Here we specify name and description for the servlet class MyServlet. - Declare a servlet with some init parameters:
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet( urlPatterns = "/imageUpload", initParams = { @WebInitParam(name = "saveDir", value = "D:/FileUpload"), @WebInitParam(name = "allowedTypes", value = "jpg,jpeg,gif,png") } ) public class ImageUploadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { String saveDir = getInitParameter("saveDir"); String fileTypes = getInitParameter("allowedTypes"); PrintWriter writer = response.getWriter(); writer.println("saveDir = " + saveDir); writer.println("fileTypes = " + fileTypes); } }
Here we declare the ImageUploadServletmapped by the URL pattern /imageUpload and specify two init parameters saveDir and allowedTypes. The doGet() method retrieves values of these parameters and prints them out to the client.
- Declare a servlet with asynchronous operation mode and load-on-startup order:
import javax.servlet.ServletConfig; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; @WebServlet( urlPatterns = "/myController", loadOnStartup = 1, asyncSupported = true ) public class StartupServlet extends HttpServlet { public void init(ServletConfig config) { System.out.println("My servlet has been initialized"); } // implement servlet doPost() and doGet()... }
Here we declare the servlet StartupServlet with loadOnStartup = 1 which means that this servlet is initialized automatically by the servlet container when the server is being started (the message in the init() method will be printed). We also specify the servlet supports asynchronous mode.
Related Java Servlet Annotations:
- @WebFilter annotation
- @WebListener annotation
- @WebInitParam annotation
- @HandlesTypes annotation
- @MultipartConfig annotation
- @ServletSecurity, @HttpMethodContraint and @HttpConstraint annotations
Other Java Servlet Tutorials:
- Java Servlet Quick Start for beginners (XML)
- Java Servlet for beginners (annotations)
- Java Servlet and JSP Hello World Tutorial with Eclipse, Maven and Apache Tomcat
- Handling HTML form data with Java Servlet
- Java File Download Servlet Example
Comments
Type Status Report
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Type Status Report
Message Whiteboard not found
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Apache Tomcat/8.5.39 (Ubuntu)