Java Servlet @WebInitParam Annotation Example
- Details
- Written by Nam Ha Minh
- Last Updated on 26 June 2019   |   Print Email
The @WebInitParamannotation is used to specify an initialization parameter for a servlet or a filter. It is used in conjunction with the @WebServlet and @WebFilter annotations.
Syntax of @WebInitParam Annotation:
@WebInitParam (
name = <name>,
value = <value>,
description = <value>
)
Attributes of @WebInitParam Annotation:
Name | Type | Required | Description |
name | String | Required | Name of the parameter |
value | String | Required | Value of the parameter |
description | String | Optional | Description of the parameter |
Some Examples of @WebInitParam Annotation:
- Specify an initialization parameter for a servlet:
import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; @WebServlet( urlPatterns = "/uploadFiles", initParams = @WebInitParam(name = "location", value = "D:/Uploads") ) public class FileUploadServlet extends HttpServlet { // implement servlet doPost() and doGet()... } - Specify an initialization parameter for a filter:
@WebFilter( urlPatterns = "/uploadFilter", initParams = @WebInitParam(name = "fileTypes", value = "doc;xls;zip") ) public class UploadFilter implements Filter { // overrides filter methods .. } - Specify multiple initialization parameters:
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
@WebServlet(
urlPatterns = "/uploadFiles",
initParams = {
@WebInitParam(name = "location", value = "D:/Uploads"),
@WebInitParam(name = "maxUploadSize", value = "9900000")
}
)
public class FileUploadServlet extends HttpServlet {
// implement servlet doPost() and doGet()...
}Related Java Servlet Annotations:
- @WebServlet annotation
- @WebListener annotation
- @WebFilter 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
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments