Tuesday, 19 May 2015

Advance Java

Java : Servlet Tutorials
Java Servlets it Java technology for creating the dynamic web applications. Java Servlets are server side components in Java that runs on Servlet enabled web server such as Tomcat, Jetty, Websphere etc.. Java Servlet is much faster then CGI and Perl since it runs in the same JVM. In case of CGI and Perl separate memory space is allocated for execution of the program which reduces it's performance.
For running the Java Servlet you can download the latest version of Tomcat server and install on your computer. It's much easier to use Tomcat for learning Java Servlet. These step-by-step tutorials will teach you Java Servlet and impart the knowledge necessary for developing and testing Java Servlet. After learning these tutorials you will be able to start using Java Servlet in your web projects.
In this section you will find several tutorials of Java Servlet.
First let's understand about web and web applications.
  1. History of  Web application
    Earlier in client- server computing, each application had its own client program and it worked as a user interface and need to be installed on each user's personal computer.
       
  2. Introduction to Web Server
    Web Server
     is a computer on the World Wide Web that stores Html documents that can be retrieved via a Web browser. It is a computer that delivers web pages.
      
  3. What is a Container
    Container are nothing but a Java application which controls servlets. Tomcat is one of the example of a container. It is the container which calls the servlet's methods like doPost() and doGet().
       

After understanding the basics of web and web application development we will learn about the Servlet technology of Java. The Servlet technology helps the developer to write dynamic web applications. JSP is also compiled into Servlet before it is executed on the container.Servlet technology is used to create web application (resides at server side and generates dynamic web page).
Servlet technology is robust and scalable because of java language. Before Servlet, CGI (Common Gateway Interface) scripting language was popular as a server-side programming language. But there was many disadvantages of this technology. We have discussed these disadvantages below.
There are many interfaces and classes in the servlet API such as Servlet, GenericServlet, HttpServlet, ServletRequest, ServletResponse etc.

What is a Servlet?

Servlet can be described in many ways, depending on the context.
  • Servlet is a technology i.e. used to create web application.
  • Servlet is an API that provides many interfaces and classes including documentations.
  • Servlet is an interface that must be implemented for creating any servlet.
  • Servlet is a class that extend the capabilities of the servers and respond to the incoming request. It can respond to any type of requests.
  • Servlet is a web component that is deployed on the server to create dynamic web page.

 What is web application?

A web application is an application accessible from the web. A web application is composed of web components like Servlet, JSP, Filter etc. and other components such as HTML. The web components typically execute in Web Server and respond to HTTP request.

CGI(Commmon Gateway Interface)

CGI technology enables the web server to call an external program and pass HTTP request information to the external program to process the request. For each request, it starts a new process.

Disadvantages of CGI

There are many problems in CGI technology:
  1. If number of clients increases, it takes more time for sending response.
  2. For each request, it starts a process and Web server is limited to start processes.
  3. It uses platform dependent language e.g. C, C++, perl.


Advantage of Servlet


There are many advantages of servlet over CGI. The web container creates threads for handling the multiple requests to the servlet. Threads have a lot of benefits over the Processes such as they share a common memory area, lightweight, cost of communication between the threads are low. The basic benefits of servlet are as follows:
  1. better performance: because it creates a thread for each request not process.
  2. Portability: because it uses java language.
  3. Robust: Servlets are managed by JVM so no need to worry about momory leak, garbage collection etc.
  4. Secure: because it uses java language..

History of web application

Earlier in client- server computing, each application had its own client program and it worked as a user interface and need to be installed on each user's personal computer. Most web applications use HTML/XHTML that are mostly supported by all the browsers and web pages are displayed to the client as static documents.  A web page can merely displays static content and it also lets the user navigate through the content, but a web application provides a more interactive experience. 
Any computer running Servlets or JSP needs to have a container. A container is nothing but a piece of software responsible for loading, executing and unloading the Servlets and JSP. While servlets can be used to extend the functionality of any Java- enabled server. They are mostly used to extend web servers, and are efficient replacement for CGI scripts. CGI was one of the earliest and most prominent server side dynamic content solutions, so before going forward it is very important to know the difference betweenCGI and the Servlets.
Common Gateway Interface (CGI)
The Common Gateway Interface, which is normally referred as CGI, was one of the practical technique developed for creating dynamic content. By using the CGI, a web server passes requests to an external program and after executing the program the content is sent to the client as the output. In CGI when a server receives a request it creates a new process to run the CGI program, so creating a process for each request requires significant server resources and time, which limits the number of requests that can be processed concurrently. CGI applications are platform dependent. There is no doubt that CGI played a major role in the explosion of the Internet but its performance, scalability issues make it less than optimal solutions.
Java Servlets
Java Servlet is a generic server extension that means a java class can be loaded dynamically to expand the functionality of a server. Servlets are used with web servers and run inside a Java Virtual Machine (JVM) on the server so these are safe and portable. Unlike applets they do not require support for java in the web browser. Unlike CGI, servlets don't use multiple processes to handle separate request. Servets can be handled by separate threads within the same process. Servlets are also portable and platform independent.

Servlet Terminology

1.    Basics of Servlet
2.    HTTP
3.    Http Request Methods
4.    Difference between Get and Post
5.    Anatomy of Get Request
6.    Anatomy of Post Request
7.    Content Type
There are some key points that must be known by the servlet programmer like server, container, get request, post request etc. Let's first discuss these points before starting the servlet technology.
The basic terminology used in servlet are given below:
1.    HTTP
2.    HTTP Request Types
3.    Difference between Get and Post method
4.    Container
5.    Server and Difference between web server and application server
6.    Content Type
7.    Introduction of XML
8.    Deployment

HTTP (Hyper Text Transfer Protocol)

1.    Http is the protocol that allows web servers and browsers to exchange data over the web.
2.    It is a request response protocol.
3.    Http uses reliable TCP connections bydefault on TCP port 80.
4.    It is stateless means each request is considered as the new request. In other words, server doesn't recognize the user bydefault.

Http Request Methods

Every request has a header that tells the status of the client. There are many request methods. Get and Post requests are mostly used.
The http request methods are:
·         GET
·         POST
·         HEAD
·         PUT
·         DELETE
·         OPTIONS
·         TRACE

          
   HTTP Request
         Description
GET
Asks to get the resource at the requested URL.
POST
Asks the server to accept the body info attached. It is like GET request with extra info sent with the request.
HEAD
Asks for only the header part of whatever a GET would return. Just like GET but with no body.
TRACE
Asks for the loopback of the request message, for testing or troubleshooting.
PUT
Says to put the enclosed info (the body) at the requested URL.
DELETE
Says to delete the resource at the requested URL.
OPTIONS
Asks for a list of the HTTP methods to which the thing at the request URL can respond

What is the difference between Get and Post?

There are many differences between the Get and Post request. Let's see these differences:
GET
POST
1) In case of Get request, only limited amount of data can be sent because data is sent in header.
In case of post request, large amount of data can be sent because data is sent in body.
2) Get request is not secured because data is exposed in URL bar.
Post request is secured because data is not exposed in URL bar.
3) Get request can be bookmarked
Post request cannot be bookmarked
4) Get request is idempotent. It means second request will be ignored until response of first request is delivered.
Post request is non-idempotent
5) Get request is more efficient and used more than Post
Post request is less efficient and used less than get.

Anatomy of Get Request

As we know that data is sent in request header in case of get request. It is the default request type. Let's see what informations are sent to the server. 

Anatomy of Post Request

As we know, in case of post request original data is sent in message body. Let's see how informations are passed to the server in case of post request. 

No comments:

Post a Comment