First Java Servlet Application

First Java Servlet Application

In this article, I am going to discuss the First Java Servlet Application. Please read our previous article where we discussed steps involved to develop a java web application. At the end of this article, you will understand the following pointers.

  1. First Java Servlet Application
  2. What is response.setContentType(“text/html”) method call?
  3. What happens if I placed the main() in our servlet program?
  4. How the servlet program is executing without the main(-) method?
  5. What happens if the programmer calls destroy() method explicitly from the service(-,-) 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 about the specification
  8. What is the difference between web applications and web site?
  9. Explain about static web resource programs and dynamic web resource programs
  10. How 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.

Please have a look at the following diagram to understand what we are going to develop.

First Java Servlet Application

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>
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 ();
    }
}
What is response.setContentType(“text/html”) method call?

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
Explain the following Java Statement in the Service method:

PrintWriter out = response. getWriter();

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();

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.

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, 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 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 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. 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?

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?

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

Explain about the 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
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.

Explain about 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.

How 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 in 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 in 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 about 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. Vender: Apache foundation
4. Open source software (the source code will be exposed)
5. Default port number: 8080 (changeable)
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 as <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 the First Java Servlet Application. I hope you enjoy the First Java Servlet Application article.

Leave a Reply

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