Starter Tutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Home » Programming » Advanced Java » Servlets » Java program to demonstrate two-tier client-server model using servlets
Suryateja Pericherla Categories: Servlets. No Comments on Java program to demonstrate two-tier client-server model using servlets
Join Our Newsletter - Tips, Contests and Other Updates
Email
Name

Aim

Write a java program to demonstrate two-tier client-server model.

 

Theory

A two-tier client-server architecture consists of a client and a server. The client sends requests to the server and the server takes the request, processes the request and sends a response back. The two-tier client server architecture can be visualized as shown in the figure below:

Two Tier Client Server Model

 

The client generally sends the request through a web browser like Google Chrome or Mozilla Firefox or Opera and views the response (generally a webpage built using HTML/CSS and/or JavaScript) again in the web browser. On the server tier, a server-side programming technology like Servlets can be used to process the requests and send back responses to the client.

 

Servlets

Servlets is a Java based technology for server-side processing. Other languages or technologies for server-side processing are PHP, JSP, node.js, Perl etc. A servlet is a special Java class which is dynamically loaded on the server and used to generate dynamic content.

 

Following are the general steps that happen when a client requests a servlet:

  1. Client sends a HTTP request from web browser containing a URL with servlet.
  2. Web server receives the request and forwards the request to application server.
  3. Using information available in xml (deployment descriptor) the servlet container loads the object appropriate servlet class.
  4. If required, the servlet retrieves data from database, processes it and sends the response back to web server.
  5. Web server forwards the response back to the client who sent the request.

 

Above steps can be illustrated as follows:

Servlet Request and Response Steps

Servlet API is a set of classes and interfaces that specify a contract between a servlet class and a servlet container. Some examples of servers that provide servlet containers are: Apache Tomcat, Oracle’s WebLogic, IBM’s Websphere, JBoss etc. All the API classes and interfaces are grouped into following packages:

  • servlet
  • servlet.http

 

Program

index.html (contains client-side code)

<html>
	<head>
		<title>Hello Servlet</title>
	</head>
	<body>
		<form  action="HelloServlet" method="get">
			<input type="submit" value="Invoke Servlet!" />
		</form>
	</body>
</html>

 

HelloServlet.java (contains server side code)

import java.io.*;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
public class HelloServlet extends HttpServlet 
{
	public void doGet(HttpServletRequest request, 
	HttpServletResponse response) throws ServletException, IOException 
	{
		response.getWriter().write("Hello World!");
	}
}

 

Input and Output

index.html

Client Side HTML Output

HelloServlet.java

Server Side Servlet Output

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory