Showing posts with label apache tomcat server. Show all posts
Showing posts with label apache tomcat server. Show all posts

Hit Counter


Simple program to demonstrate accessing a servlet in Apache Tomcat Server

Hit counter program

 

import javax.servlet.http.*; 

import javax.servlet.*; 

import java.io.*; 

 

public class HitCounter extends HttpServlet

{ 

 

     private int counter = 0;

     public void doGet(HttpServletRequest req,HttpServletResponse res)

throws ServletException, IOException 

     { 

          //setting the content type

          res.setContentType("text/html");

 

          //create the stream to write the data 

          PrintWriter pw=res.getWriter();

            

          counter++;

         

          //writing html in the stream 

          pw.println("<html><body>"); 

          pw.println("<h2>Hit Count = "+counter+"</h2>"); 

          pw.println("</body></html>"); 

         

          //closing the stream

          pw.close(); 

     }

}   

 

 

 

Session Management using HttpSession


Program to demonstrate session management using HttpSession

 

// Session management

 

import javax.servlet.http.*; 

import javax.servlet.*; 

import java.io.*; 

import java.util.*;

 

public class SessionMgt extends HttpServlet

{ 

 

     private int counter = 0;

    

     public void doGet(HttpServletRequest req,HttpServletResponse res)

throws ServletException, IOException 

     { 

          //setting the content type

          res.setContentType("text/html");

          //create the stream to write the data 

          PrintWriter pw=res.getWriter();

            

          counter++;

         

          //writing html in the stream 

          pw.println("<html>");

          pw.println("<head>");

          pw.println("<title>Session</title>");           

          pw.println("</head>");

          pw.println("<body><h3>"); 

          pw.println("<center>");

          pw.println("<h2>Servlet Session Management</h2><hr/><br/>");

         

          HttpSession hs = req.getSession(true);

                  

          // Display date/time of last access.

          Date dt = (Date)hs.getAttribute("date");

 

          if(dt != null)

          {

              pw.println("Last access: " + dt + "<br/><br/>");

          }

 

          // Display current date/time

          dt = new Date();

          hs.setAttribute("date", dt);

          pw.println("Current date: " + dt+ "<br/><br/>");

         

          pw.println("<hr/>Hit Count = "+counter); 

          pw.println("</center>"); 

          pw.println("</h3></body></html>"); 

         

          //closing the stream

          pw.close(); 

     }

}   

 

 

 

Simple Welcome Servlet


Simple program to access a servlet and display a welcome message

 

import javax.servlet.http.*; 

import javax.servlet.*; 

import java.io.*; 

 

public class WelcomeServlet extends HttpServlet

{ 

     public void doGet(HttpServletRequest req,HttpServletResponse res)

throws ServletException, IOException 

     { 

          //setting the content type

          res.setContentType("text/html");

 

          //create the stream to write the data 

          PrintWriter pw=res.getWriter();

           

          //writing html in the stream 

          pw.println("<html><body>"); 

          pw.println("<center><h2>Welcome to servlet</h2></center>"); 

          pw.println("</body></html>"); 

         

          //closing the stream

          pw.close(); 

     }

} 

Deploying a Servlet in Apache Tomacat Server

Deploying a Servlet in Apache Tomcat Server

 

 

Steps to Deploy a Servlet in Apache Tomcat Server

 

§  Create a directory structure under Tomcat for your application.

o   Inside the webapps folder of apache tomcat server create a folder (Example WT).

o   Inside this new folder (WT) create a folder called WEB-INF

o   Inside WEB-INF create a folder called classes.

o   Create a web.xml file in WEB-INF

o   The html and jsp files should be saved in WT folder.

 

Directory Structure

 

§  Copy the servlet-api.jar file from …\apache-tomcat-9.0.24\lib to …\jre1.8.0_221\lib\ext

 

§  Set path  variable to Java compiler and classpath variable to the  servlet-api.jar file

(path=C:\Program Files\Java\jdk1.8.0_221\bin)

(classpath=C:\Program Files\Java\jre1.8.0_221\lib\ext\servlet-api.jar)




§  Write the servlet source code and Compile. Copy the .class file to the classes folder created in tomcat server. (Example – WelcomeServlet.java)

 

§  Create a deployment descriptor for the servlet in web.xml.

Example:

                   <servlet>

                             <servlet-name>WelcomeServlet</servlet-name>

                             <servlet-class>WelcomeServlet</servlet-class>

                   </servlet>

 

                   <servlet-mapping>

                             <servlet-name>WelcomeServlet</servlet-name>

                             <url-pattern>/WelcomeServlet</url-pattern>

          </servlet-mapping>

 

§  Start the Tomcat server and call the servlet from a web browser.

 

web.xml file:

 

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app version="3.0"

          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_3_0.xsd">

 

          <servlet>

                   <servlet-name>WelcomeServlet</servlet-name>

                   <servlet-class>WelcomeServlet</servlet-class>

          </servlet>

 

          <servlet-mapping>

                   <servlet-name>WelcomeServlet</servlet-name>

                   <url-pattern>/WelcomeServlet</url-pattern>

          </servlet-mapping>

  

</web-app>