0% found this document useful (0 votes)
4 views

How to Create Java Servlet Application

The document provides a comprehensive guide on creating a Java Servlet application, covering essential topics such as servlet basics, HTTP protocols, and the servlet life cycle. It includes practical examples, such as developing a simple addition application, and explains key concepts like servlet communication, session tracking, and the use of the Tomcat server. Additionally, it addresses common questions regarding servlet execution and specifications in web development.

Uploaded by

smtmaherbanusmc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

How to Create Java Servlet Application

The document provides a comprehensive guide on creating a Java Servlet application, covering essential topics such as servlet basics, HTTP protocols, and the servlet life cycle. It includes practical examples, such as developing a simple addition application, and explains key concepts like servlet communication, session tracking, and the use of the Tomcat server. Additionally, it addresses common questions regarding servlet execution and specifications in web development.

Uploaded by

smtmaherbanusmc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

3/26/24, 12:00 PM How to Create Java Servlet Application - Dot Net Tutorials

Java Servlets – Basics


How to Create Java Servlet Application
 Introduction to Web
Application and Web
Back to: Java Servlets Tutorials
Terminology
 Introduction to
Servlets
 HTTP Protocol and
HTTP Methods
 Java Servlet API
 Java Servlet Interface
 Generic Servlet
 HTTP Servlet in Java
 Servlet Life Cycle
 Steps to Create
Servlet Application
 How to Create Java
Servlet Application
 How Servlet Works
C# MVC Web API Design Patterns .NET Core  Dotnet  Data Bases  Java  C/C++/DSA  More… 

https://dotnettutorials.net/lesson/first-java-servlet-application/ 1/16
3/26/24, 12:00 PM How to Create Java Servlet Application - Dot Net Tutorials

 User Interface Form


Designs
 WAR File in Java
 Welcome File List in
Servlet
 Creating Servlet using
Eclipse IDE
 Creating Servlet using
MyEclipse IDE
Replay
 Creating Servlet using
NetBeans IDE
 Servlet
Communication in
Java
 ServletRequest
Interface
 RequestDispatcher in
Servlet How to Create Java Servlet Application
 Servlet Chaining in
In this article, I am going to discuss How to Create Java Servlet Application. Please read our previous
Java
article where we discussed the steps involved to develop a java web application. At the end of this
 Request Redirection
in Servlet article, you will understand the following pointers.

 ServletConfig
Interface 1. How to Create Java Servlet Application

 ServletContext 2. What is response.setContentType(“text/html”) method call?


Interface 3. What happens if I placed the main() in our servlet program?
4. How the servlet program is executing without the main(-) method?

https://dotnettutorials.net/lesson/first-java-servlet-application/ 2/16
3/26/24, 12:00 PM How to Create Java Servlet Application - Dot Net Tutorials

 Java Servlet 5. What happens if the programmer calls destroy() method explicitly from the service(-,-)
Attributes method of the servlet program?
6. What happens if the programmer calls the init(-) method explicitly from the service(-,-)

method of the servlet program?


7. Explain Specification

8. What is the difference between web applications and web site?


9. Explain static web resource programs and dynamic web resource programs

10. How do we identify whether a web resource program is client-side or server-side?

11. Explain about TOMCAT server

Develop a Java Web Application to Implement the use case of the Addition of two
numbers.
Java Servlets –
Advanced Please have a look at the following diagram to understand what we are going to develop.

 Servlet Scopes in Java


 Session Tracking in
Servlet
 Cookies Session
Tracking Mechanism
 Hidden Form Fields
Session Tracking
Mechanism
 URL Rewriting
Session Tracking
web.xml
Mechanism

https://dotnettutorials.net/lesson/first-java-servlet-application/ 3/16
3/26/24, 12:00 PM How to Create Java Servlet Application - Dot Net Tutorials

 HttpSession Session
Tracking Mechanism <web-app>

 Servlet Events and <servlet>


Listeners <servlet-name>Two</servlet-name>
 Servlet Wrappers <servlet-class>AdditionServlet</servlet-class>
 Servlet Filters </servlet>
 CRUD in Servlet <servlet-mapping>
 Exception Handling in <servlet-name>Two</servlet-name>
Servlet <url-pattern>/add</url-pattern>
 Servlet Annotations </servlet-mapping>
 Servlet Input Output </web-app>
Stream Classes
 Single Thread Model
Interface
Numbers.html
 Server Side Include
(SSI) in Servlet
 Servlet Debugging <html>
 Servlet <body>
Internationalization <center>
<h1> number entry screen</h1>
<form action="./add"> ENTER NUMBER1
<input type="text" name="T1">
<br>
<br> ENTER NUMBER1
<input type="text" name="T2">
<br>

https://dotnettutorials.net/lesson/first-java-servlet-application/ 4/16
3/26/24, 12:00 PM How to Create Java Servlet Application - Dot Net Tutorials

<input type="submit" value="send"> </form>


</center>
</body>
</html>

AdditionServlet.java

importjavax.servlet.*;
import java.io.*;
public class AdditionServlet extends GenericServlet
Real-time Application {
Development Examples public void service (ServletRequest request,ServletResponse response) th
 Registration Form in {
Servlet
String a = request.getParameter ("T1");
 Fetch Data from String b = request.getParameter ("T2");
Database using
int n1 = Integer.parseInt (a);
Servlet
int n2 = Integer.parseInt (b);
 Improving Servlet
int sum = n1 + n2;
performance to fetch
response.setContentType ("text/html");
records from
PrintWriter out = response.getWriter ();
database
out.println ("<!DOCTYPE html>");
 Uploading and
Downloading Files in out.println ("<html>");

Servlet out.println ("<body bgcolor = yellow>");


out.println ("the sum is:" + sum);

https://dotnettutorials.net/lesson/first-java-servlet-application/ 5/16
3/26/24, 12:00 PM How to Create Java Servlet Application - Dot Net Tutorials

 Sending Email out.println ("</body>");


through JavaMail API out.println ("</html>");
in Servlet out.close ();
 Write data into PDF }
using Servlet }
 Login Form in Servlet
 Display Images using
Servlet
 Auto Refresh Page What is response.setContentType(“text/html”) method call?
using Servlet
This method is used to specify the MIME (Multipurpose Internet Mail Extensions) type. It represents that
Java Servlet – Interview passes instructions to the browser window through a web server that this servlet program generates
 Servlet Interview HTML code base response. Some MIME types are
Questions and
Answers 1. Text/html
2. Text/plain
Popular Servlet Books
3. Image/gif
 Most Recommended
4. Image/jpeg
Servlet Books

Explain the following Java Statement in the Service method:

PrintWriter out = response. getWriter();

https://dotnettutorials.net/lesson/first-java-servlet-application/ 6/16
3/26/24, 12:00 PM How to Create Java Servlet Application - Dot Net Tutorials

Java.io.PrintWriter is a character-oriented output stream. In a servlet, it is called a browser stream.


Whatever is written into this stream is transferred to the browser and hence the name. This stream can’t
be used to send binary data from the sender to the browser. For example, images can’t be sent by using
this stream. ServletOutputStream sos = response.getOutputStream();

Advertisements

out.println(” “); This method makes the stream object i.e. out for writing the data to the destination object
response. This response object passes that data (argument values of println() method) to the webserver
and the webserver writes data to the browser window as an HTTP response in the form of a web page.

https://dotnettutorials.net/lesson/first-java-servlet-application/ 7/16
3/26/24, 12:00 PM How to Create Java Servlet Application - Dot Net Tutorials

out.close(); This method is used to close the stream connection with the response object.

What happens if I placed the main() in our servlet program?

1. Servlet program execution is taken care of by servlet containers through life cycle methods.
2. Since the main(-) method is not the life cycle method of the servlet program so it is not called by the
servlet container.
3. Only in the stand-alone application, do we need the main(-) method to begin the application
execution.
4. The container software managed the programs or components that will be executed through life
cycle methods. So the main(-) method is not required in that components or programs.

How the servlet program is executing without the main(-) method?

1. The stand-alone application that will be executed by JVM directly needs to have the main method
to begin the execution. Since the servlet program is not a standalone application and it is a web

https://dotnettutorials.net/lesson/first-java-servlet-application/ 8/16
3/26/24, 12:00 PM How to Create Java Servlet Application - Dot Net Tutorials

resource program that will be executed by servlet containers through life cycle methods so there is
no need for the main method in the servlet program.
2. If an already running java application wants to create certain other java class objects and wants to
call a method of that class then that other class needs not to have a main(-) method.
3. A Servlet container is a continuously running java application/software which creates our own
servlet class object and calls life cycle methods on that object for executing the servlet program. So
our servlet program needs not to have a main(-) method.
4. Since we never give our servlet program to JVM directly for execution so there is no need of
placing the main(-) method in our servlet program.

What happens if the programmer calls destroy() method explicitly from the
service(-,-) method of the servlet program?

The Servlet container will not destroy our servlet class object but the logic of the destroy method execute
along with the service method. When the life cycle event is raised the servlet container calls the life cycle
method. Since the programmer has called the life cycle method, automatically the servlet container never
raises the life cycle event.

What happens if the programmer calls the init(-) method explicitly from the
service(-,-) method of the servlet program?

The Servlet container never creates a new object of our servlet class for the above call but the logic of the
init(-) method executes along with the service method.

What is Specification?

https://dotnettutorials.net/lesson/first-java-servlet-application/ 9/16
3/26/24, 12:00 PM How to Create Java Servlet Application - Dot Net Tutorials

A specification is a document or reference that contains a set of rules and guidelines to develop the
software. There are two types of specifications

1. Open specification: (Anyone can develop software based on this specification) example: JDBC
specification, Servlets specification, JSP specification, etc.
2. Proprietary Specification: (The only specific company which has given specification is allowed to
develop the software). Example: .net specification

What is the difference between web applications and web site?

When the website is under development /testing in a software company then it is called a web application.
Once a web application is hosted on the internet by purchasing a Domain name (www.dotnettutorials.net)
and space internet network is called a website.

What are static web resource programs and dynamic web resource programs?

Static web resource programs generate static web pages. For example, HTML Pages. Dynamic web
resource programs generate a dynamic web page. Example Servlet program, JSP Program, etc. The
static web page contains fixed content forever, the content of the dynamic web page changes based on
the input values of the request.

Advertisements

https://dotnettutorials.net/lesson/first-java-servlet-application/ 10/16
3/26/24, 12:00 PM How to Create Java Servlet Application - Dot Net Tutorials

How do we identify whether a web resource program is client-side or server-side?

1. We can identify whether a web resource program is client-side or server-side based on the place
where the web resource programs are executing.
2. If the web resource programs are executing on the server then they are called a server-side web
resource program. For example, the servlet program, JSP program, etc.
3. If the web resource program comes to the browser window from the web application for execution
then it is called a client-side web resource program. Example: HTML programs, javascript
programs.
4. Both client-side and server-side web resource programs are residing on a web server only.
5. The process of keeping the web application in the webserver is technically called deployment and
the reverse process is called un-deployment.

Explain the TOMCAT server.

1. Type: a java-based web server


2. Version: Tomcat 7.0 (compatible with JDK 1.6 /1.7)
Tomcat 6.0 (compatible with JDK 1.6)
Tomcat 5.0 (compatible with JDK 1.5)
2. Vendor: Apache foundation
4. Open source software (the source code will be exposed)
5. Default port number: 8080 (changeable)

https://dotnettutorials.net/lesson/first-java-servlet-application/ 11/16
3/26/24, 12:00 PM How to Create Java Servlet Application - Dot Net Tutorials

6. Default username: admin (changeable)


7. Default password: admin (changeable)
8. To download software: www.apache.org
9. For documents: www.apache.org
10. The installation folder of tomcat is called <tomcat-home>
11. To start tomcat server use <tomcat-home>/bin/tomcat7.exe file
12. To lunch the home page of the tomcat server, the procedure is to open the browser window and type
the following string
http://localhost:8080 in the address bar and press enter
13. Here 8080 is the port number of the tomcat server
14. Procedure to change the port no of tomcat server after installation
Goto <tomcat-home>/conf/server.xml file and modify port attribute value of first <connector> tag and
then restart the server.

In the next article, I am going to discuss How does the Servlets work in detail. Here, in this article, I try to
explain How to Create Java Servlet Application. I hope you enjoy this How to Create Java Servlet
Application article.

Dot Net Tutorials


About the Author: Pranaya Rout

https://dotnettutorials.net/lesson/first-java-servlet-application/ 12/16
3/26/24, 12:00 PM How to Create Java Servlet Application - Dot Net Tutorials

Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has
very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET
Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud
Computing, Microservices, Design Patterns and still learning new technologies.

← Previous Lesson Next Lesson →


Steps to Create Servlet Application How Servlet Works

https://dotnettutorials.net/lesson/first-java-servlet-application/ 13/16
3/26/24, 12:00 PM How to Create Java Servlet Application - Dot Net Tutorials

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

https://dotnettutorials.net/lesson/first-java-servlet-application/ 14/16
3/26/24, 12:00 PM How to Create Java Servlet Application - Dot Net Tutorials

Comment *

Name* Email* Website

Post Comment

https://dotnettutorials.net/lesson/first-java-servlet-application/ 15/16
3/26/24, 12:00 PM How to Create Java Servlet Application - Dot Net Tutorials

About Us Privacy Policy Contact ADO.NET Tutorial Angular Tutorials ASP.NET Core Blazor Tuturials ASP.NET Core Tutorials
ASP.NET MVC Tutorials ASP.NET Web API Tutorials C Tutorials C#.NET Programs Tutorials C#.NET Tutorials Cloud Computing Tutorials
Data Structures and Algorithms Tutorials Design Patterns Tutorials DotNet Interview Questions and Answers Core Java Tutorials
Entity Framework Tutorials JavaScript Tutorials LINQ Tutorials Python Tutorials SOLID Principles Tutorials SQL Server Tutorials Trading Tutorials
JDBC Tutorials Java Servlets Tutorials Java Struts Tutorials C++ Tutorials JSP Tutorials MySQL Tutorials Oracle Tutorials
ASP.NET Core Web API Tutorials HTML Tutorials

© Dot Net Tutorials | Website Design by Sunrise Pixel

https://dotnettutorials.net/lesson/first-java-servlet-application/ 16/16

You might also like