Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Ex.

No: 5a VISITOR COUNTER


Date:

Aim:

To write a java program using Servlet for VisitorCounter.

Algorithm:

Step1: Import all the necessary java and javax class files.

step2: Declare the class name and extend HttpServlet class.

step3: Implement doGet() in java servlet.

step4: Run the HTML form and click the submit button.

Step5: See the output generated by Servlet according to the no.of visitor.
PROGRAMS:
VisitorCounter.html:
<html>
<head>
<title>VisitorCounter</title>
</head>
<body>
<form action="VisitorCounter" method="get">
<input type="submit" value="Enter Into Site">
</form>
</body>
</html>
VisitorCounter.java:
import java.io.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import javax.servlet.*;
@WebServlet(name="VisitorCounter", urlPatterns={"/VisitorCounter"})
public class HelloCounter1 extends HttpServlet {
private int visit=0;
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
visit++;
out.println("Hello! You are the visitor:"+visit);
}
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
processRequest(request, response);
}
}
OUTPUT:

RESULT:

Thus the java program using Servlet for VisitorCounter has been created
and executed successfully.

You might also like