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:

 

Other Java Servlet Tutorials:


About the Author:

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.



Add comment