How to Create Java Servlet Application 1
How to Create Java Servlet Application 1
Ads by
Stop seeing this ad Why this ad?
5. What happens if the programmer calls destroy() method explicitly from the service(-,-)
https://dotnettutorials.net/lesson/first-java-servlet-application/ 1/17
3/27/24, 11:10 AM How to Create Java Servlet Application - Dot Net Tutorials
9. Explain static web resource programs and dynamic web resource programs
Develop a Java Web Application to Implement the use case of the Addition of two
numbers.
Please have a look at the following diagram to understand what we are going to develop.
web.xml
<web-app>
<servlet>
<servlet-name>Two</servlet-name>
<servlet-class>AdditionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Two</servlet-name>
<url-pattern>/add</url-pattern>
</servlet-mapping>
</web-app>
Numbers.html
<html>
<body>
<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>
<input type="submit" value="send"> </form>
</center>
</body>
</html>
https://dotnettutorials.net/lesson/first-java-servlet-application/ 2/17
3/27/24, 11:10 AM How to Create Java Servlet Application - Dot Net Tutorials
AdditionServlet.java
importjavax.servlet.*;
import java.io.*;
public class AdditionServlet extends GenericServlet
{
public void service (ServletRequest request,ServletResponse response)
throwsServletException, IOException
{
String a = request.getParameter ("T1");
String b = request.getParameter ("T2");
int n1 = Integer.parseInt (a);
int n2 = Integer.parseInt (b);
int sum = n1 + n2;
response.setContentType ("text/html");
PrintWriter out = response.getWriter ();
out.println ("<!DOCTYPE html>");
out.println ("<html>");
out.println ("<body bgcolor = yellow>");
out.println ("the sum is:" + sum);
out.println ("</body>");
out.println ("</html>");
out.close ();
}
}
This method is used to specify the MIME (Multipurpose Internet Mail Extensions) type. It represents that passes
instructions to the browser window through a web server that this servlet program generates HTML code base
response. Some MIME types are
1. Text/html
2. Text/plain
3. Image/gif
4. Image/jpeg
https://dotnettutorials.net/lesson/first-java-servlet-application/ 3/17
3/27/24, 11:10 AM How to Create Java Servlet Application - Dot Net Tutorials
Ads by
Stop seeing this ad Why this ad?
Advertisements
Ads by
Stop seeing this ad
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/ 4/17
3/27/24, 11:10 AM How to Create Java Servlet Application - Dot Net Tutorials
Ads by
Stop seeing this ad
out.close(); This method is used to close the stream connection with the response object.
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.
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 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.
https://dotnettutorials.net/lesson/first-java-servlet-application/ 5/17
3/27/24, 11:10 AM How to Create Java Servlet Application - Dot Net Tutorials
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?
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
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.
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.
https://dotnettutorials.net/lesson/first-java-servlet-application/ 6/17
3/27/24, 11:10 AM How to Create Java Servlet Application - Dot Net Tutorials
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.
https://dotnettutorials.net/lesson/first-java-servlet-application/ 7/17