4. Servlet
4.1 Develop a simple servlet program which maintains a counter for the number of times it has been accessed since its loading, initialize the counter using deployment descriptor.
HitCounter.java
import java.io.IOException;import java.io.PrintWriter;import javax.servlet.*;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class HitCounter extends HttpServlet{int c;public void init(){ServletConfig s=getServletConfig();c=Integer.parseInt(s.getInitParameter("counter"));}public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException{c++;PrintWriter out = res.getWriter();out.println("Total Hit: " + c);}}
<servlet><servlet-name>pagecounter</servlet-name><servlet-class>HitCounter</servlet-class><init-param><param-name>counter</param-name><param-value>0</param-value></init-param></servlet><servlet-mapping><servlet-name>pagecounter</servlet-name><url-pattern>/counter</url-pattern></servlet-mapping>
0 Comments
Post a Comment