Practical-14 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.
index.html:
<!DOCTYPE html>
<html>
<head>
<title>Practical-14</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="HitCounter" method="GET">
<input type="submit" value="OK">
</form>
</body>
</html>
<html>
<head>
<title>Practical-14</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="HitCounter" method="GET">
<input type="submit" value="OK">
</form>
</body>
</html>
HitCounter.java:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HitCounter extends HttpServlet {
int c;
public void init(ServletConfig s) throws ServletException
{
c=Integer.parseInt(s.getInitParameter("HitCounter"));
}
public ServletConfig getServletConfig()
{
return null;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
c++;
PrintWriter out=response.getWriter();
out.println("Total Hit: "+c);
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="4.0" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
<servlet>
<servlet-name>HitCounter</servlet-name>
<servlet-class>HitCounter</servlet-class>
<init-param>
<param-name>HitCounter</param-name>
<param-value>0</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>HitCounter</servlet-name>
<url-pattern>/HitCounter</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
Output:
Comments
Post a Comment