Servlet initialization parameters are used to course of the information when servlet initializing. Earlier than servlet 3, this knowledge have to placed on deployment descriptor and servlet will learn the information in init() methodology utilizing ServletConfig Object. For each servlet, the information is exclusive and never been shared in between of two servlets.
Earlier than Servlet Three Initialization parameter declared in net.xml as following
1
2
3
4
5
6
7
8
|
<servlet>
<servlet–identify>myserlvet</servlet–identify>
<servlet–class>com.demo.MyServlet</servlet–class>
<init–param>
<param–identify>e mail</param–identify>
<param–worth>tousifxxxx@xxx.com</param–worth>
</init–param>
</servlet>
|
Servlet Three include @WebInitParam
annotation, utilizing which developer can outline the initialization parameter in Servlet itself. Later servlet can entry that parameter by utilizing config.getInitParameter()
methodology.
1
2
3
4
5
6
7
|
@WebServlet(
urlPatterns = {“/initparam”},
initParams = {
@WebInitParam (identify = “e mail”, worth = “tousifxxx@xxx.com”),
@WebInitParam (identify = “cellphone”, worth = “92709xxxxx”)
}
)
|
Following instance reveal using @WebInitParam annotation in Servlet 3.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
bundle org.techzoo.servlet3;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
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 = {“/initparam”},
initParams = {
@WebInitParam (identify = “e mail”, worth = “tousifxxx@xxx.com”),
@WebInitParam (identify = “cellphone”, worth = “92709xxxxx”)
}
)
public class ServletInitParamDemo extends HttpServlet {
non-public static last lengthy serialVersionUID = 1L;
non-public String e mail = “”, cellphone = “”;
public ServletInitParamDemo() {
tremendous();
}
@Override
public void init(ServletConfig config) throws ServletException {
tremendous.init(config);
e mail = config.getInitParameter(“e mail”);
cellphone = config.getInitParameter(“cellphone”);
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(“textual content/html”);
String html = “
Entry knowledge utilizing @WebInitParam“ ;PrintWriter out = response.getWriter();
html +=“
E-mail : “+e mail+“Telephone No. : “+cellphone+“ “ ;out.println(html);
}
}
|
The output will appear like much like following…
Supply techzoo.org