Aim: Write a simple servelet prog which maintain counter for the no of times it has been accessed since its loading , initialize counter using deployment descriptor.
File Name:Counter.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Counter extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{ response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Counter</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>"+"No. Of Visitors:");
out.println(RequestHandler.getreqcnt()+"</h1>");
out.println("<h1>"+"No. Of activated Sessoin:");
out.println(RequestHandler.getreqsc() +"</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
File Name: RequestHandler.java
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class RequestHandler implements ServletRequestListener,HttpSessionListener{
static int cnt ,sc;
public void requestDestroyed(ServletRequestEvent sre) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void requestInitialized(ServletRequestEvent sre) {
cnt++;
}
public void sessionCreated(HttpSessionEvent se) {
sc++;
}
public void sessionDestroyed(HttpSessionEvent se) {
sc--;
}
public static int getreqcnt() {
return cnt;
}
public static int getreqsc() {
return sc;
}
}
Web.XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<listener>
<listener-class>RequestHandler</listener-class>
</listener>
<servlet>
<servlet-name>Counter</ servlet-name>
<servlet-class>Counter</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Counter</servlet-name>
<url-pattern>/Copunter</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
File Name:Counter.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Counter extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{ response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Counter</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>"+"No. Of Visitors:");
out.println(RequestHandler.getreqcnt()+"</h1>");
out.println("<h1>"+"No. Of activated Sessoin:");
out.println(RequestHandler.getreqsc() +"</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
File Name: RequestHandler.java
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class RequestHandler implements ServletRequestListener,HttpSessionListener{
static int cnt ,sc;
public void requestDestroyed(ServletRequestEvent sre) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void requestInitialized(ServletRequestEvent sre) {
cnt++;
}
public void sessionCreated(HttpSessionEvent se) {
sc++;
}
public void sessionDestroyed(HttpSessionEvent se) {
sc--;
}
public static int getreqcnt() {
return cnt;
}
public static int getreqsc() {
return sc;
}
}
Web.XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<listener>
<listener-class>RequestHandler</listener-class>
</listener>
<servlet>
<servlet-name>Counter</ servlet-name>
<servlet-class>Counter</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Counter</servlet-name>
<url-pattern>/Copunter</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
No comments:
Post a Comment