RSS

Monthly Archives: January 2016

Implement Servlet using Servlet Interface

UsingServlet.java

import java.io.*;
import javax.servlet.*;
public class UsingServlet implements Servlet 
{
	ServletConfig config=null;
	public void init(ServletConfig con) throws ServletException
	{
			config=con;
	}
	public void service(ServletRequest req,ServletResponse res) 
	throws IOException,ServletException
	{
			res.setContentType("text/html");
			PrintWriter out=res.getWriter();
			out.println("Welcome to servlet using servlet interface ");
			out.println(getServletInfo());
			//get the value of initialization parameter
			int i=Integer.parseInt(config.getInitParameter("count"));
			out.println("i="+i);
	}
	public void destroy()
	{
	}
	public String getServletInfo()
	{
			return "copyright inofrmation " ;
	}
	public ServletConfig getServletConfig()
	{
			return config;
	}
}

web.xml

<servlet>
		<servlet-name>UsingServlet</servlet-name>
		<servlet-class>UsingServlet</servlet-class>
		<init-param>
			<param-name>count</param-name>
			<param-value>10</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>UsingServlet</servlet-name>
		<url-pattern>/UsingServlet</url-pattern>
	</servlet-mapping>
 
Leave a comment

Posted by on January 30, 2016 in Example