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.
@WebInitParam (
name = <name>,
value = <value>,
description = <value>
)
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 |
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()...
}
@WebFilter(
urlPatterns = "/uploadFilter",
initParams = @WebInitParam(name = "fileTypes", value = "doc;xls;zip")
)
public class UploadFilter implements Filter {
// overrides filter methods ..
}
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()...
}
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.