CounterServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CounterServlet extends HttpServlet{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(true);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Integer count = new Integer(0);
String head;
if (session.isNew()) {
head = "This is the New Session";
} else {
head = "This is the old Session";
Integer oldcount =(Integer)session.getValue("count");
if (oldcount != null) {
count = new Integer(oldcount.intValue() + 1);
}
}
session.putValue("count", count);
out.println("<HTML><BODY BGCOLOR="#FDF5E6">n" +
"<H2 ALIGN="CENTER">" + head + "</H2>n" +
"<TABLE BORDER=1 ALIGN=CENTER>n"
+ "<TR BGCOLOR="#FFAD00">n"
+" <TH>Information Type<TH>Session Countn"
+"<TR>n" +" <TD>Total Session Accessesn" +
"<TD>" + count + "n" +
"</TABLE>n"
+"</BODY></HTML>" );
}
}
Mapping of Servlet (“CounterServlet.java”) in web.xml file
<servlet> <servlet-name>CounterServlet</servlet-name> <servlet-class>CounterServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CounterServlet</servlet-name> <url-pattern>/CounterServlet</url-pattern> </servlet-mapping>
Running the servlet by this url:
http://localhost:8080/CounterServlet
displays the figure below:
When servlet is hit six times by the user the counter value will be increased by six as shown in figure below:
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…