Home Java Javaee6 @WebInitParam Annotation Servlet Example

Related Tutorials


 
 

Share on Google+Share on Google+

@WebInitParam Annotation Servlet Example

Advertisement
In this tutorial you will learn how to use the init parameters in servlet 3.0 specificattion.

@WebInitParam Annotation Servlet Example

In this tutorial you will learn how to use the init parameters in servlet 3.0 specificattion.

In the older version of servlet init parameters specifications were given into the web.xml file. But now from the servlet's 3.0 version it can be given at the time of defining a servlet class which is created using the annotation @WebServlet.  Here you can give the name and value of init param using the initParams attribute. To specifies these initParams the annotation @WebInitParam is used, although you can give these information through the web.xml but, for this you will be required to give these information additionally to the servlet. @WebInitParam has the two attributes (i) "name" that specifies the name of parameter and (ii) "value" which specifies the value of the parameter. These init params can be accessed using the getInitParameter(String name) contained by the ServletConfig

Example :

WebInitParamExample.java

package roseindia.net;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(
name = "WebInitParamExample", urlPatterns = {"/hello"}
,initParams = {
@WebInitParam(name= "Site :", value="http://roseindia.net"),
@WebInitParam(name= "Rose", value= "India"),
}
)
public class WebInitParamExample extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h2>Init Param Servlet Example</h2>");
ServletConfig config= getServletConfig();
String pValue= config.getInitParameter("Site :");
out.println("Param Value : "+pValue);
String pValue1= config.getInitParameter("Rose");
out.println("<br>Param Value : "+pValue1);
out.close();
}
}

Before the servlet 3.0 information of init-params were given as :

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>servletAnnotationExample</display-name>
<servlet>
<servlet-name>WebInitParamExample</servlet-name>
<servlet-class>roseindia.net.WebInitParamExample</servlet-class>

<init-param>
<param-name>Site :</param-name>
<param-value>http://roseindia.net</param-value>
</init-param>

</servlet>

<servlet-mapping>
<servlet-name>WebInitParamExample</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>

</web-app>

Output :

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 14, 2011

Related Tutorials

Discuss: @WebInitParam Annotation Servlet Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com