Share ⇓

What is Advance Java?


What is Advance Java?

Most of us already know that normal applications can be easily built using core Java concepts. But, when it comes to developing web applications, advanced Java fundamentals, like JSP, Servlets, JDBC etc.

Introduction to Advanced Java

Advanced Java is everything that goes beyond Core Java – most importantly the APIs defined in Java Enterprise Edition, includes Servlet programming, Web Services, the Persistence API, etc. It is a Web & Enterprise application development platform which basically follows client & server architecture.

Below I have listed down few major advantages of Advance Java:

1. Advance Java i.e. J2EE (Java Enterprise Edition) gives you the library to understand the Client-Server architecture for Web Application Development which Core Java doesn’t support.

2. J2EE is platform Independent, Java Centric environment for developing, building & deploying Web-based applications online. It also consists of a set of services, APIs, and protocols, which provides the functionality that is necessary for developing multi-tiered, web-based applications.

3. You will be able to work with Web and Application Servers like Apache Tomcat, Glassfish etc and understand the communication over HTTP protocol. But, in Core Java, it is not possible.

4. There are a lot of Advance Java frameworks like Spring, JSF, Struts etc. which enable you to develop a secure transaction based web apps for the domains like E-Commerce, Banking, Legal, Financial, Healthcare, Inventory etc.

5. To work and understand the hot technologies like Hadoop and Cloud services, you should be prepared with core and advanced Java concepts.

I hope you understood why Advanced Java is essential. For your better understanding, I have divided this article into three sections. Each of these section deals with one of the most important concepts of advanced Java:

1. JDBC (Java DataBase Connectivity)
2. Java Servlets
3. JSP (Java Servlet Pages)

1. Introduction to JDBC in Advance Java and Core Java

JDBC is a standard Java API for a database-independent connectivity between the Java programming language and a wide range of databases. This application program interface lets you encode the access request statements, in Structured Query Language (SQL). They are then passed to the program that manages the database. It mainly involves opening a connection, creating a SQL Database, executing SQL queries and then arriving at the output.

We can use JDBC API to access tabular data stored in any relational database. By the help of JDBC API, we can save, update, delete and fetch data from the databases. It is similar to the Open Database Connectivity (ODBC) provided by Microsoft.

JDBC Architecture
The JDBC API supports both two-tier and three-tier processing models for database access but in general, JDBC Architecture consists of two layers

JDBC API: This provides the application-to-JDBC Manager connection.
JDBC Driver API: This supports the JDBC Manager-to-Driver Connection.

The JDBC API uses a driver manager and database-specific drivers to provide transparent connectivity to heterogeneous databases. The JDBC driver manager ensures that the correct driver is used to access each data source. The driver manager is capable of supporting multiple concurrent drivers connected to multiple heterogeneous databases.

Common JDBC Components
The JDBC API provides the following interfaces and classes −

1. DriverManager is used to manage a list of database drivers. The first driver that recognizes a certain subprotocol under JDBC will be used to establish a database Connection.
2. Driver is an interface that handles the communications with the database server. It also abstracts the details associated with working with Driver objects.
3. Connection is an interface that consists all the methods required to connect to a database. The connection object represents communication context, i.e., all communication with the database is through connection object only.

Introduction to Servlets in Advance Java
A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers.

1. Servlet is a technology which is used to create a web application.
2. It is an API that provides many interfaces and classes including documentation.
3. Servlet is an interface that must be implemented for creating any Servlet.
4. It is also a class that extends the capabilities of the servers and responds to the incoming requests. It can respond to any requests.

Advanced Java Tutorial: Servlet Life Cycle
The entire life cycle of a Servlet is managed by the Servlet container which uses the javax.servlet.Servlet interface to understand the Servlet object and manage it.

Servlet Life Cycle
The entire life cycle of a Servlet is managed by the Servlet container which uses the javax.servlet.Servlet interface to understand the Servlet object and manage it.

Stages of the Servlet Life Cycle:
The Servlet life cycle mainly goes through four stages,

  1. Loading a Servlet.
  2. Initializing the Servlet.
  3. Request handling
  4. Destroying the Servlet.

Let’s look at each of these stages in details:

1. Loading a Servlet: The first stage of the Servlet life cycle involves loading and initializing the Servlet by the Servlet container.

The Web container or Servlet Container can load the Servlet at either of the following two stages :

i). Initializing the context, on configuring the Servlet with a zero or positive integer value.

ii). If the Servlet is not preceding the stage, it may delay the loading process until the Web container determines that this Servlet is needed to service a request.

2. Initializing a Servlet: After the Servlet is instantiated successfully, the Servlet container initializes the instantiated Servlet object. The container initializes the Servlet object by invoking the init(ServletConfig) method which accepts ServletConfig object reference as a parameter.

3. Handling request: After initialization, the Servlet instance is ready to serve the client requests.

The Servlet container performs the following operations when the Servlet instance is located to service a request :

It creates the ServletRequest and ServletResponse. In this case, if this is an HTTP request then the Web container creates HttpServletRequest and HttpServletResponse objects which are subtypes of the ServletRequest and ServletResponse objects respectively.

 4. Destroying a Servlet: When a Servlet container decides to destroy the Servlet, it performs the following operations,

i. It allows all the threads currently running in the service method of the Servlet instance to complete their jobs and get released.
ii. After currently running threads have completed their jobs, the Servlet container calls the destroy() method on the Servlet instance.
iii. After the destroy() method is executed, the Servlet container releases all the references of this Servlet instance so that it becomes eligible for garbage collection.

Now that you have understood the basics of a servlet, let’s move further and understand what are the steps involved to create a Servlet application.

Steps to create Servlet
  1. Create a directory structure
  2. Create a Servlet
  3. Compile the Servlet
  4. Add mappings to web.xml file
  5. Start the server and deploy the project
  6. Access the servlet
 Now, based on the above steps let’s write a program and understand how servlet works.

Step 1: To run a servlet program, we should have Apache tomcat server installed and configured. Once the server is configured, you can start with your program.  

Step 2: For a servlet program, you need 3 files – index.html file, Java class file, and web.xml file. The very first step is to create Dynamic Web Project and then proceed further

Step 3: Now let’s see how to add 2 numbers using servlets and display the output in the browser.

First,  I will write index.html file

<!DOCTYPE html>
<form action ="add">
  Enter 1st number: <input type="text" name="num1"><br>
  Enter 2nd number: <input type="text" name="num2"><br>
<input type ="submit">    

Above program creates a form to enter the numbers for the addition operation.

Step 4: Now without the Java class file, you can’t perform addition on 2 numbers. So let’s write a class file.

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Add extends HttpServlet{
       public void service(HttpServletRequest req, HttpServletResponse res) throws IOException
              int i = Integer.parseInt(req.getParameter("num1"));
              int j = Integer.parseInt(req.getParameter("num2");
              int k= i j;
              PrintWriter out = res.getWriter();
              out.println("Result is" k);
Step 5: After writing the Java class file, the last step is to add mappings to the web.xml file. Let’s see how to do that.

Step 6: web.xml file will be present in the WEB-INF folder of your web content. If it is not present, then you can click on Deployment Descriptor and click on Generate Deployment Descriptor Stub.

Step 7: After that, you can proceed further and add the mappings to it.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema instance">http://www.w3.org/2001/XMLSchema-instance</a>" xmlns=<"<a href="http://java.sun.com/xml/ns/javaee">http://java.sun.com/xml/ns/javaee</a>" xsi:schemaLocation="<a href="http://java.sun.com/xml/ns/javaee">http://java.sun.com/xml/ns/javaee</a> <a href="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd</a>"</em> version=<em>"3.0"</em>>
Step 8: After all this, you can run the program by starting the server. You will get the desired output on the browser.

Java Server Pages
JSP or Java Server Pages is a technology that is used to create web application just like Servlet technology. It is an extension to Servlet – as it provides more functionality than servlet such as expression language, JSTL, etc. A JSP page consists of HTML tags and JSP tags. The JSP pages are easier to maintain than Servlet because we can separate designing and development. It provides some additional features such as Expression Language, Custom Tags, etc.

Now let’s see various features of JSP.

Portable: JSP tags will process and execute by the server side web container, So that these are browser independent and J2EE server independent.

Powerful: JSP consists of bytecode so that all Java features are applicable in case of JSP like robust, dynamic, secure, platform independent.

Flexible: It allows to define custom tags so that the developer can fill conferrable to use any kind, framework based markup tags in JSP.

Fast Development: If JSP page is modified, we don’t need to recompile and redeploy the project. The Servlet code needs to be updated and recompiled if we have to change the look and feel of the application.

Tracking the User: JSP allows us to track the selections made by the user during user interaction with the website by maintaining the information in the session or cookies

Easy: JSP is easy to learn, easy to understand and easy to develop. JSPs are more convenient to write than Servlets because they allow you to embed Java code directly into your HTML pages.

Now that you understood what is JSP, let’s see how JSP and Servlets differ from each other and why JSP is better than Servlets with the help of below table.

        JSP                                                            Servlets
i. Extension to Servlet                              i.  Not an extension to servlet
ii. Easy to Maintain                                  ii. Bit complicated
iii. No need to recompile or redeploy           iii. The code needs to be recompiled
iv. Less code than a servlet                           iv.  More code compared to JSP

Life Cycle of JSP
The JSP pages follow these phases:

  1. Translation of JSP Page
  2. Compilation of JSP Page
  3. Classloading (the classloader loads class file)
  4. Instantiation (Object of the Generated Servlet is created)
  5. Initialization ( the container invokes jspInit())
  6. Request processing ( the container invokes _jspService())
  7. Destroy ( the container invokes jspDestroy())

A JSP page is translated into Servlet by the help of JSP translator. And then, JSP translator is a part of the web server which is responsible for translating the JSP page into Servlet. After that, Servlet page is compiled by the compiler and gets converted into the class file. Moreover, all the processes that happen in Servlet are performed on JSP later, like initialization, committing response to the browser and destroy.

JSP Scripting Elements:
The scripting elements provide the ability to insert java code inside the JSP. There are three types of scripting elements:

scriptlet tag –  A scriptlet tag is used to execute Java source code in JSP.
Syntax is: <%  java source code %>

expression tag
– The code placed within JSP expression tag is written to the output stream of the response. So you need not write out.print() to write data. It is mainly used to print the values of variable or method.
Syntax : <%=  statement %>

declaration tag
– The JSP declaration tag is used to declare fields and methods. The code written inside the JSP declaration tag is placed outside the service() method of an auto-generated servlet. So it doesn’t get memory at each request.
Syntax: <%!  field or method declaration %>


Our Recent Coment