You are on page 1of 45

A

Industrial Training Report


On

Java Programming Language

Submitted

in partial fulfillment
for the award of the Degree of
Bachelor of Technology
In
Computer Engineering

Submitted To: Submitted By:

Mr. Sayar Singh Shekhawat RAVI RAJA

Head of Department V Sem CSE


Roll No.: -17EAICS122
Department of Computer Science & Engineering

Arya Institute of Engineering & Technology, Jaipur

Rajasthan Technical University, Kota


ARYA INSTITUTE OF ENGINEERING & TECHNOLOGY
SP-40, RIICO Industrial Area, Jaipur (Raj)-302022

Department of Computer Science & Engineering

Certificate

This is to certify that the work, which is being presented in the Industrial
Training report for training taken at < Dreamtech IT Solution Pvt. Ltd. >
entitled “ Java Programming Language ” submitted by Mr. Ravi Raja ,
a student of third year(VSem) B.Tech. in Computer Engineering as a
partial fulfilment for the award of degree of bachelor of technology is a
record of student’s work carried out and found satisfactory for
submission.

Mr. Manish Choubisa Mr. Sayar Singh Shekhawat


Training Coordinator Head of Department
Candidate’s Declaration

I hereby declare that the work, which is being presented in the Industrial
Training report, entitled “ Java Programming Language ”in partial fulfillment
for the award of Degree of “Bachelor of Technology” in Department of Computer
Science &Engineering with Specialization in Computer Engineering and submitted
to the Department of Computer Science & Engineering, Arya Institute of
Engineering & Technology, is a record of my own investigations carried under the
Guidance of Mr. Manish Choubisa, Associate Professor, Department of Computer
Science & Engineering.

Ravi Raja

RAVI RAJA
Roll No:- 17EAICS122.
Acknowledgement

I gratefully acknowledge for the assistance, co-operation, guidance


and clarifications provided by Dreamtech IT Solution Pvt. Ltd during
the development of the project on Java Programming Language
during my training period. My extreme gratitude towards Mr. S P Singh
(Head & Director of Dreamtech IT Solution Pvt. Ltd) for guiding me
throughout the Training Period.Without his willing disposition, spirit of
accommodation, timely clarification and above all faith in me, the
project could not have been completed in due time.

His readiness to discuss all important matters at work deserves


special attention. I would also like to thank all the trainer of the
Company for their co-operation and friendly support.
ABSTRACT
The DITSPT goal of engaging in world-class research in the interrelated
technologies has resulted in a need to invent new solutions to address the
challenges of a web economy – from secure e-commerce, high
availability systems to electronic content publishing. The Advance Java
training done by the Publishing Systems and Solutions Unit in DITSPT,
Jaipur , sets to create and develop Software or other web technology to
enable desktop access to Information Technology sector.
The students’ project, which contributed to part of the Project,
allowed the students to acquire the knowledge of Object Oriented
Programming and Software Engineering.
The Text Editor Application, the initial part of the E-service problem
set, served as an introduction to object oriented programming in Java.
The Text Editor Applet faces an applet security that severely restricts
functionality, particularly in file access and printing. The project
introduced the use of servlet to handle these functions remotely instead.
The Text Editing Service then introduced a new electronic service,
which goes beyond the simple client/server model. The former employs
many small service components and make sure of the e-speak
framework to define the text editor service and interface. From the
results of the successful project, it is recommended that the Extensible
Markup Language, XML, be used in place of the HTML, which has
limited internal structure. XSL, a transformation language for
transforming a XML document into an HTML document, can be used to
convert the Text Editing applet to talk to the e-speak service.
Table of content
CHAPTER 1 HISTORY OF JAVA.…………………….…….… [1 - 2]

CHAPTER 2 JDK & JRE………………………………………... [4 - 5]

CHAPTER 3 INSTALLATION OF JAVA ……………………... [6-11]

CHAPTER 4 JAVA BASIC TERM……...…………………....... [12-13]

CHAPTER 5 JAVA NETWORKING.……...….......................... [13-14]

CHAPTER 6 AWT…………………………………………….... [14-15]

CHAPTER 7 SWING…………………………………………… [16-17]

CHAPTER 8 JAVA DATABASE CONNECTIVITY……….... [18 - 22]

CHAPTER 9 JAVA SERVER PAGE…..……………...……… [23 - 29]

CHAPTER 10 JAVA SERVER PAGE…..…………………… [30 - 33]

CHAPTER 11 APPLET………………...…………………….. [34 - 35]

CHAPTER 12 STRING………………...…………………….. [36 - 37]

CONCLUSION

REFERENCES
HISTORY OR INTRODUCTION OF JAVA
History of Java
Java is a General Purpose, Object Oriented Programming Language
developed by Sun Microsystems of USA I 1991. Originally called Oak by Games
Gosling, one of the invention of the language, java was designed for the
development of software for consumer electronic devices like TVs, VCRs, toasters
and such other electronic Machines.This goal had a strong impact on the
development team to make the language simple, portable and highly reliable.

The java team which included Patrick Naughton discovered that the existing
languages like C and C++ had limitations in terms of both reliability and
portability. However, they modeled their new language Java on C and C++ but
removed a number of features of of C and C++ that were considered as source of
problems and thus made java really simple, reliable,portable and powerful
language.

1990-> Sun Microsystems decided to develop special software that could be used
to manipulate consumer electronic devices. A team of Sun Microsystems
programmers headed by James Gosling was formed to undertake this task.

1991-> After exploring the possibility of most Object Oriented Programming


Language C++, the team announced a new language named “Oak”.

1992-> The team, known as a Green Project team by Sun, demonstrated the
application of their new language to control a list of home appliance using a hand-
held device with a tiny touch sensitive screen.

1993-> The World Wide Web(WWW) appeared on the internet and transformed
the text-based Internet into a Graphical-rich environment. The green Project team
came up with the idea of developing Web Applets(tiny programs) using the new
language that could run on all types of computers connected to Internet.
1994-> The team developed a web browser called “Hot Java” to locate and run
applet programs on Internet. Hot Java demonstrated the power of the new
language, thus making it instantly popular among the Internet users.

1995-> Oak was named “Java”, due to some legal snags. Java is just a name
and is not an acronym. Many popular companies including Netscape and
Microsoft announce to their support to Java.

1996->Java established itself not only a leader for Internet Programming but also as
a general-purpose, object oriented programming language. Java found its
home.

The most striking feature of the language is that it is a platform-neutral


language. Java is a first programming language that is not tied to any particular
hardware or operating system.
INTRODUCTION TO JAVA
Java is a programming language initially developed by Sun Microsystems by
James Gosling and released as a principal component of Sun Microsystems' Java
platform in 1995. Although the language gets much of its syntax from C and C++
it has a less complicated object model and lesser low-level services. Java
applications are typically compiled to byte code (class file) that can run on any
Java Virtual Machine (JVM) regardless of computer architecture .Java is a general-
purpose, object-oriented language that is specifically designed to have as few
implementation dependencies as possible. It is intended to let application
developers "write once, run anywhere". Java is currently one of the most standard
programming languages in use, and is extensively used from application software
to web applications.

There were five primary goals in the creation of the Java language.

 It should be "simple, object-oriented and familiar".

 It should be "robust and secure".

 It should be "architecture-neutral and portable".

 It should execute with "high performance".

 It should be "interpreted, threaded, and dynamic".


JDK & JRE
Java Environment includes a large number of development tools and
hundreds of classes and methods. The development tools are part of the system
known as Java Development Kit (JDK) and the classes and methods are part of the
Java Standard Library (JSL), also known as the Application Programming
Interface (API).

JDK : Java Development Kit comes with a collection of tools that are used for
developing and running Java Programs. They include :

appletviewer-> Enables us to run Java Applets (Without using java compatible


browser)

Java-> Java Interpreter, which runs applets and applications by reading and
interpreting bytecode files.

Javac-> The Java compiler, which translates Java source code to byte code files
that the interpreter understad.

Javadoc-> Creates HTML-format documentation from Java source code files.

Javah-> Produces header files for use with native methods.

Javap-> Java disassembler, which enables us to convert bytecode files into a


program description.

Jdb-> Java debugger, which helps us to find errors in our programs.


The Java Platform
One thing that distinguished Java from some other languages is its ability to
run the same compiled code across multiple operating systems.In other languages,
the source code (code that is written by the programmer), is compiled by a
compiler into an executable file. This file is in machine language, and is intended
for a single operating system/processor combination, so the programmer would
have to re-compile the program seperately for each new operating
system/processor combination.Java is different in that it does not compile the code
directly into machine language code. Compilation creates bytecode out of the
source code. Bytecode generally looks something like this:

a7 f4 73 5a 1b 92 7d

When the code is run by the user, it is processed by something called the Java
Virtual Machine (JVM). The JVM is essentially an interpreter for the bytecode. It
goes through the bytecode and runs it. There are different versions of the JVM that
are compatible with each OS and can run the same code. There is virtually no
difference for the end-user, but this makes it a lot easier for programmers doing
software development.

Java and Open Source:-


In 2006 Sun started to make Java available under the GNU General Public
License (GPL). Oracle continues this project called OpenJDK.

Java Virtual machine :-


- The Java virtual machine (JVM) is a software implementation of a
computer that executes programs like a real machine.
- The Java virtual machine is written specifically for a specific operating
system, e.g. for Linux a special implementation is required as well as for
Windows.
- Java programs are compiled by the Java compiler into bytecode. The
Java virtual machine interprets this bytecode and executes the Java
program.

Java Runtime Environment vs. Java Development Kit


- A Java distribution comes typically in two flavors, the Java Runtime
Environment (JRE) and the Java Development Kit (JDK).

- The Java runtime environment (JRE) consists of the JVM and the Java class
libraries and contains the necessary functionality to start Java programs.

- The JDK contains in addition the development tools necessary to create Java
programs. The JDK consists therefore of a Java compiler, the Java virtual
machine, and the Java class libraries.
INSTALLATION OF JAVA
Check Installation
- Java might already be installed on your machine. You can test this by
opening a console (if you are using Windows: Win+R, enter cmd and
press Enter) and by typing in the following command:
- java –version.
- If Java is correctly installed, you should see some information about
your Java installation. If the command line returns the information that
the program could not be found, you have to install Java.

Installing the Java Development Kit


- Before installing the Java Development Kit (JDK), you should probably
know what it is. It is distributed by Oracle. It contains the core libraries
and compiler required to develop Java. The JDK should not be confused
with the JRE (Java Runtime Environment). The JRE is a JVM for
running, as opposed to compiling, Java programs.

Downloading and Installing


- To download the JDK, go to
http://www.oracle.com/technetwork/java/javase/downloads/index.html.
Click on "JDK with NetBeans Bundle". Follow the instructions for
downloading the JDK installation file.
- Windows: If you are running Windows, simply run the executable file
and follow the installation instructions.
- Unix, Solaris, or Linux: For Linux and Unix, download the "jdk1 6.0"
for Linux systems. Save the downloaded file in any drive. Once you have
saved the file, extract it to a place that you can remember, by using
Terminal or by double clicking on the file. When you have finished
extracting the file, copy the JDK 1.6.0 folder and paste it in the
usr/local(To paste to the usr/local directory, you have to be in root) so
that every user can use the java files. You can delete the downloaded zip
file so that it doesn't take up space on your drive.
- Macintosh: The latest available JDK is automatically installed by the
operating system. Because Java for Macintosh is developed and
maintained by Apple, in coordination with Sun, the current version on the
Macintosh may not be the current version that is available

Note on Editions
- The JDK comes in three editions.
- Java Standard Edition (JSE) – This version is the basic platform for Java.
The course will focus on this edition.
- Java Enterprise Edition (JEE) – This edition is mainly for developing and
running distributed multitier architecture Java applications, based
largely on modular software components running on an application
server. We will not be covering this version in the course.
- Java Micro Edition (JME) – This edition is primarily for developing
programs to run on consumer applicances, such as PDAs and cell
phones.

Configuring Variables
- Before writing code, it is recommended that you set the Path variable on
your system so you can compile your code more easily.

For Windows Users


- From the Control Panel, double click "System" (System and Maintenance
in Vista)
- For Windows 7 or Vista, click on "System," "Advanced System Settings"
on the left, and then on "Environment Variables."
- For XP and 2000, click on the "Advanced" tab and click on
"Environment Variables" For NT, click on the "Environment" tab.
- Select the Path variable and click "Edit".
- Add the path to the bin directory of where Java is installed on your hard
drive. It should probably be: C:\Program Files\Java\jdk1.6.0_20\bin
unless you changed it during installation.
- Click OK
For Linux and UNIX
- One way to set your path in Linux/Unix is to add a path export to your
bash profile.
- In order to do this, first open your bash profile in a text editor. For
example,
- pico ~/.bash_profile
- Then add this line:
- export PATH=$PATH:/usr/local/jdk/bin
- Note that the path to the java directory "/usr/local/jdk/bin" may be
different on your machine.
- Restart your shell.

For Macintosh
- Apple sets everything up for you. Sit back and relax.
- The only drawback is that because Apple handles development and
maintenance of Java on the Mac, there is usually a delay from the time
that a new version is released by Sun and the time that the new version is
released on the Mac. Also, getting the latest version sometimes requires
an operating system upgrade.
- Oh well, you can't have everything.

Validate installation
- Switch again to the command line and run the following command.
- java -version
- The output should be similar to the following output.
- java version "1.7.0_25"
- OpenJDK Runtime Environment (IcedTea 2.3.10) (7u25-2.3.10-
1ubuntu0.13.04.2)
- OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode)
Development Process with Java
- Java source files are written as plain text documents. The programmer
typically writes Java source code in an Integrated Development
Environment (IDE) for programming. An IDE supports the programmer
in the task of writing code, e.g. it provides auto-formating of the source
code, highlighting of the important keywords, etc.
- At some point the programmer (or the IDE) calls the Java compiler
(javac). The Java compiler creates the bytecode instructions. These
instructions are stored in .class files and can be executed by the Java
Virtual Machine.

Garbage collector
- The JVM automatically re-collects the memory which is not referred to
by other objects. The java garbage collector checks all object references
and find the objects which can be automatically released.
- While the garbage collector releases the programmer from the need to
explicitly manage memory the programmer still need to ensure that he
does not keep unneeded object references otherwise the garbage collector
cannot release the associated memory. Keeping unneeded object
references are typically called memory leaks.

Classpath
- The classpath defines where the Java compiler and Java runtime look for
.class files to load. This instructions can be used in the Java program.
- For example if you want to use an external Java library you have to add
this library to your classpath to use it in your program.
Java basic terms
Basics: Package, Class and Object
- It is important to understand the base terminology of Java in terms of
packages, classes and objects. This section gives an overview of these
terms.

Package
- Java groups classes into functional packages.
- Packages are typically used to group classes into logical units. For
example all graphical views of an application might be placed in the same
package called com.vogella.webapplication.views.
- It is common practice to use the reverse domain name of the company as
top level package. For example the company might own the domain,
vogella.com and in this example the Java packages of this company starts
with com.vogella.
- Other main reason for the usage of packages is to avoid name collisions
of classes. A name collision occurs if two programmers give the same
fully qualified name to a class. The fully qualified name of a class in Java
consists out of the package name followed by a dot (.) and the class name.
- Without packages, a programmer may create a Java class called Test.
Another programmer may create a class with the same name. With the
usage of packages you can tell the system which class to call. For
example if the first programmer puts the Test class into package report
and the second programmer puts his class into package xmlreader you can
distinguish between these classes by using the fully qualified name, e.g.
xmlreader.Test or report.Test.

Class
- Def.: Template that describes the data and behavior associated with an
instance of that class.
- In Java source code a class is defined by the class keyword and must start
with a capital letter. The body of a class is surrounded by {}.
package test;
class MyClass {
}
- The data associated with a class is stored in variables ; the behavior
associated to a class or object is implemented with methods.
- A class is contained in a Java source file with the same name as the class
plus the .java extension.

Object
- Def.: An object is an instance of a class.
- The object is the real element which has data and can perform actions.
Each object is created based on the class definition

Inheritance
- A class can be derived from another class. In this case this class is called
a subclass. Another common phrase is that a class extends another class.
- The class from which the subclass is derived is called a superclass.
- Inheritance allows a class to inherit the behavior and data definitions of
another class.
- The following codes demonstrates how a class can extend another class.
In Java a class can extend a maximum of one class.
package com.vogella.javaintro.base;
class MyBaseClass {
void hello(){
System.out.println("Hello from MyBaseClass");
}
}
package com.vogella.javaintro.base;
class MyExtensionClass extends MyBaseClass {
}
Override methods and the @override annotation
- If a class extends another class it inherits the methods from its superclass.
If it wants to change these methods it can override these methods. To
override a method you use the same method signature in the source code
of the subclass.
- To indicate to the reader of the source code and the Java compiler that
you have the intention to override a method you can use the @override
annotation.
- The following code demonstrates how you can override a method from a
superclass.
package com.vogella.javaintro.base;
class MyBaseClass {
void hello(){
System.out.println("Hello from MyBaseClass");
}
}
package com.vogella.javaintro.base;
class MyExtensionClass2 extends MyBaseClass {
}

Object has superclass


- Every object in Java implicitly extends the Object class. The class defines
the following methods for every Java object:
- equals(o1) allows to check if the current object is equal to o1
- getClass() returns the class of the object .
- hashCode() returns an identifier of the current object
- toString() Give a string representation of the current object

Variables and methods


Variable
- Variables allow the Java program to store values during the runtime of
the program.
- A variable can either be a primitive variable or a reference variable. A
primitive variable contains value while the reference variable contains a
reference (pointer) to the object. Hence if you compare two reference
variables, you compare if both point to the same object. To compare
objects use the object1.equals(object2) method call.

Instance variable

- Instance variable is associated with an instance of the class (also called


object). Access works over these objects.
- Instance variables can have any access control and can be marked final or
transient. Instance variables marked as final can not be changed after
assigned to a value.

Local variable

- Local (stack) variable declarations cannot have access modifiers.


- final is the only modifier available to local variables. This modifier
defines that the variable can not be changed after first assignment.
- Local variables do not get default values, so they must be initialized
before use.

Methods
- A method is a block of code with parameters and a return value. It can be
called on the object.
package com.vogella.javaintro.base;
public class MyMethodExample {
void tester(String s) {
System.out.println("Hello World");
}
}
- Method can be declared with var-args. In this case the method declares a
parameter which accepts from zero to many arguments (syntax: type ..
name;) A method can only have one var-args parameter and this must be
the last parameter in the method.
- Overwrite of a superclass method: A method must be of the exact same
return parameter and the same arguments. Also the return parameter must
be the same. Overload methods: An overloaded method is a method with
the same name, but different arguments. The return type can not be used
to overload a method.

Main method
- A public static method with the following signature can be used to start a
Java application. Such a method is typically called main method.
public static void main(String[] args){

Constructor
- A class contains constructors that are invoked to create objects based on
the class definition. Constructor declarations look like method
declarations except that they use the name of the class and have no return
type. A class can have several constructors with different parameters.
Each class must define at least one constructor.
- In the following example the constructor of the class expects a parameter.
package com.vogella.javaintro.base;
public class MyConstructorExample2 {

String s;

public MyConstructorExample2(String s) {
this.s = s;
}
}
- If no explicit constructor is defined the compiler adds implicitly a
constructor. If the class is sub-classed then the constructor of the super
class is always implicitly called in this case.
- In the following example the definition of the constructor without
parameters (also known as the empty constructor) is unnecessary. If not
specified the compiler would create one.
package com.vogella.javaintro.base;
public class MyConstructorExample {
// Unnecessary, would be created by the compiler if left out
public MyConstructorExample() {
}
}
- The naming conversion for creating a constructor is the following:
classname (Parameter p1, ..) {} .
- Every object is created based on a constructor. This constructor method is
the first statement called before anything else can be done with the object.
JAVA NETWORKING
The term Network programming refers to writing programs that
execute across multiple devices (Computers), in which the devices are all
connected to each other using networks. The Java net package of the J2se
API contains a collection of classes and interfaces that provide the low-
level communication details, allowing you to write programs that focus
on solving problem at hand. The java net package provides support for
the two common network protocols.

1. The java.net package provides support for the two common network
protocols -
- TCP- TCP stands for transmission control protocol, which allows for
reliable communication between two applications. TCP is typically used
over the Internet Protocol, which is referred to as TCP/IP.
- UDP (User Datagram Protocol)- UDP stands for User Datagram
Protocol, a connection-less protocol that allows for packets of data to be
transmitters between applications.

2. This chapter gives a good understanding on the following two


subjects –
- URL Processing- This would be covered separately. Click here to learn
about URL Processing in java language.
- Socket Programming- Sockets provide the communication mechanism
between two computers using TCP. A client program creates a socket on
its end of the communication and attempts to connect that socket to a
server. When the connection is made, the server creates a socket object on
its end of the communication. The client and the server can now
communicate by writing to and reading from the socket.
AWT (ABSTRACT WINDOWING TOOLKIT)
Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or
window-based application in java. AWT components are platform-dependent.

- AWT components are heavyweight.


- AWT doesn't support pluggable look and feel.
- AWT provides fewer components than Swing.
- AWT doesn't follow MVC (Model View Controller) where model
represents data, view represents presentation and controller acts as an
interface between model and view.

1. Java AWT Hierarchy

The hierarchy of Java AWT classes are given below

Java AWT Hierarchy


Example-

import java.awt.*;

class First extends Frame {

First() {

Button b=new Button("click me");

b.setBounds(30,100,80,30); // setting button position

add(b); //adding button into frame

setSize(300,300); //frame size 300 width and 300 height

setLayout(null); //no layout manager

setVisible(true); //now frame will be visible, by default not visible


} public static void main(String args[]){

First f=new First();

}}
SWING
Work Flow Java Swing is a part of Java Foundation Classes (JFC) that is
used to create window-based applications. It is built on the top of AWT (Abstract
Windowing Toolkit) API and entirely written in java.

- Java swing components are platform-independent.


- Swing components are lightweight.
- Swing supports pluggable look and feel.
- Swing provides more powerful components such as tables, lists, scroll
panes, color chooser, tabbed pane etc.
- Swing follows MVC.
1. Hierarchy of Java Swing classes

The hierarchy of java swing API is given below


FirstSwingExample.java

import javax.swing.*;

public class FirstSwingExample {

public static void main(String[] args) {

JFrame f=new JFrame(); //creating instance of JFrame

JButton b=new JButton("click"); //creating instance of JButton

b.setBounds(130,100,100, 40); //x axis, y axis, width, height

f.add(b); //adding button in JFrame

f.setSize(400,500); //400 width and 500 height

f.setLayout(null); //using no layout managers

f.setVisible(true); //making the frame visible

} }

Output-
JAVA DATABASE CONNECTIVITY
The Java Database Connectivity (JDBC) API is the industry standard for
database-independent connectivity between the Java programming language and a
wide range of databases – SQL databases and other tabular data sources, such as
spreadsheets or flat files. The JDBC API provides a call-level API for SQL-based
database access.

JDBC technology allows you to use the Java programming language to


exploit "Write Once, Run Anywhere" capabilities for applications that require
access to enterprise data. With a JDBC technology-enabled driver, you can connect
all corporate data even in a heterogeneous environment.

1. JDBC drivers
There are commercial and free drivers available for most relational
database servers. These drivers fall into one of the following types:
- Type 1 That calls native code of the locally available ODBC driver.
- Type 2 That calls database vendor native library on a client side. This
code then talks to database over network.
- Type 3, the pure-java driver that talks with the server-side middleware
those then talks to database.
- Type 4, the pure-java driver that uses database native protocol.
2. Functionality and implementation
JDBC allows multiple implementations to exist and b e used by the
same application. The API provides a mechanism for dynamically loading
the correct Java packages and registering them with the JDBC Driver
Manager. The Driver Manager is used as a connection factory for creating
JDBC connections. JDBC connections support Creating and executing
statements. These may be update statements such as SQL's CREATE,
INSERT, UPDATE and DELETE, or they may be query statements such as
SELECT. Additionally, stored procedures may be invoked through a JDBC
connection.

JDBC represents statements using one of the following classes-


- Statement - the statement is sent to the database server each and every
time.
- Prepared Statement - the statement is cached and then the execution path
is predetermined on the database server allowing it to be executed
multiple times in efficient manner.
- Callable Statement- used for executing stored procedure son the

database. Update statements such as INSERT, UPDATE and DELETE


return an update count that indicates how many rows were affected in the database.

These statements do not return any other information. Query statements


return a JDBC row result set. The row result set is used to walk over the result set.
Individual columns in a row are retrieved either by name or by column number.
There may be any number of rows in the result set. The row result set has metadata
that describes the names of the columns and their types. There is an extension to
the basic JDBC API in the javax.sql. The method Class.forName(String) is used to
load the JDBC driver class.
JAVA SERVER PAGES
Java Server Pages (JSP) is a technology that helps software developers
create dynamically generated web pages based on HTML, XML, or other
document types. Released in 1999 by Sun Microsystems[1], JSP is similar to PHP,
but it uses the Java programming language. To deploy and run Java Server Pages, a
compatible web server with a servlet container, such as Apache Tomcat or Jetty, is
required.

Java Server Pages is a technology which permits software developers to create


dynamic request like HTML, XML in order to answer to client request in the net.
This technology lets Java code and definite pre-defined procedures to be implanted
into static content. The syntax in Java Server Pages includes a supplementary XML
tag which is known as JSP actions.

It is made use to evoke the utility of the built-in functions. Moreover JSP permits
to establish and form the JSP tag libraries which operate as an extension to the
standard XML or HTML tags. These JSP tag libraries give a good technique to
widen the potentiality of the Web server by providing an independent platform.JSP
compiler compiles the JSPs into Java Servlets.
A JSP compiler may possibly create a servlet in Java code and it is later
compiled by the Java compiler. It might even directly produce the byte code for the
servlet. Java Server Pages can be examined as a high level abstraction of servlets
which is practiced as an extension of the Servlet2.1API.

Java Server Pages are HTML pages embedded with snippets of Java code.It is
an inverse of a Java Servlet

JSPs run in two phases

1. Translation Phase- In translation phase JSP page is compiled into a servlet


called JSP Page Implementation class

2. Execution Phase- In execution phase the compliled JSP is processed

JSP Compilation
The Java Server Pages and the Servlets were initially developed at Sun
Microsystems. Opening with version 1.2 of the Java Server Page specification the
JSPs have been built under the Java Community Process. There are quite a few JSP
implicit objects that are represented by the JSP container and it could be mentioned
and indicated by the programmers

- Config - It gives the data of the servlet configuration .


- Application - Data's are shared by the servlets and Java Server Pages in
theapplication.
- Exception - Exceptions are not trapped by the codes in the application.
- Out - The data's are written with the help of JSP Writer to the response
stream.
- Request - Here the Hypertext Transfer Protocol request the object.
- Response - Here the Hypertext Transfer Protocol response the object
- Session - It is helpful to trace the data's and information about a user from
one request to another request

There are several actions that are performed in JSP actions. A JSP action is nothing
but a XML tags that invokes functionality of the built-in web server.

Some of the JSP action is given as follows.

- Jsp:param - It indicates a parameter which will be added in addition to


the request of the existing parameters. It is used inside the jsp:params or
jsp:include, jsp:forwardblocks
- Jsp:include - Java Servlet provisionally gives the request and response
off to the specific Java Server Page. The Control will later come again to
the existing JSP as soon as theother JSP has completed. With the help of
this the JSP code will be distributed among several other JSPs rather than
replica.
- Jsp:forward - This JSP action is used to give off the request/response to
the other servlet or JSP. The control will not come back to the existing
JSP.
- Jsp:plugin - The ancient version of web browsers like Internet Explorer
and Netscape Navigator use various tags to embed an applet. This action
creates the definite tags that are required for a browser to include an
applet.
- Jsp:fallback - This action is used to confirm that if the browser never
gives support to applets.
- Jsp:getProperty - It obtains a property from the specific JavaBean.
- Jsp:setProperty - It sets a property in the specific JavaBean
SERVLET
Servlets are protocol and platform independent server-side software
components, written in Java. They run inside a Java enabled server or
application server, such as the WebSphere Application Server. Servlets are
loaded and executed within the Java Virtual Machine (JVM) of the Web
server or application server, in much the same way that applets are loaded
and executed within the JVM of the Web client.

Since servlets run inside the servers, however, they do not need a
graphical user interface (GUI). In this sense, servlets are also faceless
objects.

Servlets more closely resemble Common Gateway Interface (CGI)


scripts or programs than applets in terms of functionality. As in CGI
programs, servlets can respond to user events from an HTML request, and
then dynamically construct an HTML response that is sent back to the client.

a) The Java Servlet API

The Java Servlet API is a set of Java classes which define a standard interface
between a Web client and a Web servlet. Client requests are made to the Web
server, which then invokes the servlet to service the request through this interface.
The API is composed of two packages:

- javax.servlet
- javax.servlet.http

The Servlet interface class is the central abstraction of the Java


Servlet API. This class defines the methods which servlets must implement,
including a service() method for the handling of requests. The
GenericServlet class implements this interface, and defines a generic,
protocol-independent servlet.

To write an HTTP servlet for use on the Web, we will use an even
more specialized class of GenericServlet called HttpServlet.HttpServlet
provides additional methods for the processing of HTTP requests such as
GET (doGet method) and POST (doPost method). Although our servlets may
implement a service method, in most cases we will implement the HTTP
specific request handling methods of doGet and doPost.

b) Servlet Life Cycle

Lifecycle of Servlet
The life cycle of a servlet can be categorized into four parts:

- Loading and Instantiation- The servlet container loads the servlet


during startup or when the first request is made. The loading of the servlet
depends on the attribute <load-onstartup> of web.xml file. If the attribute
<load-on-startup> has a positive value then the servlet is load with
loading of the container otherwise it load when the first request comes for
service. After loading of the servlet, the container creates the instances of
the servlet.
- Initialization- After creating the instances, the servlet container calls the
init() method and passes the servlet initialization parameters to the init()
method. The init() must be called by the servlet container before the
servlet can service any request. The initialization parameters persist untill
the servlet is destroyed. The init() method is called only once throughout
the life cycle of the servlet.The servlet will be available for service if it is
loaded successfully otherwise the servlet container unloads the servlet.
- Servicing the Request: After successfully completing the initialization
process, the servlet will be available for service. Servlet creates seperate
threads for each request. The sevlet container calls the service() method
for servicing any request. The service() method determines the kind of
request and calls the appropriate method (doGet() or doPost()) for
handling the request and sends response to the client using the methods of
the response object.
- Destroying the Servlet- If the servlet is no longer needed for servicing
any request, the servlet container calls the destroy() method . Like the
init() method this method is also called only once throughout the life
cycle of the servlet. Calling the destroy() method indicates to the servlet
container not to sent the any request for service and the servlet releases all
the resources associated with it. Java Virtual Machine claims for the
memory associated with the resources for garbage collection.
A simple servlet example
package itso.servjsp.servletapi;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SimpleHttpServlet extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><TITLE>SimpleHttpServlet</TITLE><BODY >");
out.println("<H2>Servlet API Example -
SimpleHttpServlet</H2><HR>");
out.println("<H4>This is about as simple a servlet as it gets!</H4>");
out.println("</BODY><HTML>");
out.close();
}
}
APPLET

Work An applet is a Java program that runs in a Web browser. An


applet can be a fully functional Java application because it has the entire
Java API at its disposal.
There are some important differences between an applet and a
standalone Java application, including the following –
- An applet is a Java class that extends the java.applet.Applet class.
- A main() method is not invoked on an applet, and an applet class will not
define main().
- Applets are designed to be embedded within an HTML page.
- When a user views an HTML page that contains an applet, the code for
the applet is downloaded to the user's machine.
- A JVM is required to view an applet. The JVM can be either a plug-in of
the Web browser or a separate runtime environment.
- The JVM on the user's machine creates an instance of the applet class and
invokes various methods during the applet's lifetime.
- Applets have strict security rules that are enforced by the Web browser.
The security of an applet is often referred to as sandbox security,
comparing the applet to a child playing in a sandbox with various rules
that must be followed.
- Other classes that the applet needs can be downloaded in a single Java
Archive (JAR) file.

Life Cycle of an Applet


Four methods in the Applet class give you the framework on which you
build any serious applet –

a) init − This method is intended for whatever initialization is needed for


your applet. It is called after the param tags inside the applet tag have been
processed.

b) start − This method is automatically called after the browser calls the
init method. It is also called whenever the user returns to the page containing
the applet after having gone off to other pages.

c) stop − This method is automatically called when the user moves off the
page on which the applet sits. It can, therefore, be called repeatedly in the
same applet.

d) destroy − This method is only called when the browser shuts down
normally. Because applets are meant to live on an HTML page, you should
not normally leave resources behind after a user leaves the page that contains
the applet.

e) paint − Invoked immediately after the start() method, and also any time
the applet needs to repaint itself in the browser. The paint() method is
actually inherited from the java.awt. A "Hello, World" Applet Following is a
simple applet named HelloWorldApplet.java –

Example- import java.applet.*; import java.awt.*; public class


HelloWorldApplet extends Applet { public void paint (Graphics g) {
g.drawString ("Hello World", 25, 50);

}}
Strings

Strings represent a sequence of characters. The easiest way to represent a sequence


of characters in Java is by using a character array. Example : char
charArray[]=new char[4];

We can declare string using Characters but there is limitation that we can not use
functions related with strings using array like copy of an array is difficult. For that
Java provides String class using objects.

for that two classes are used String and StringBuffer. A Java String
is an instantiated object of the String class.

String stringName;

stringName=new String ("string");

String Array :

String itemArray[]=new String[3];//will create itemArray of size 3 to hold


three string constants.
Input Output in Java
Files in Java
The Path Name

While dealing with the files, a source of confusion for a beginner in Java
programming is the path name. For example, consider the following path
name on a Unix/Linux machine:

/java/scjp/temp

The first forward slash represents the root directory. This path name in
Windows machines may be written as

C:\java\scjp\temp

File(String pathname)

Creates an instance of the File class by converting the path name String to an
abstract path name.

File(String parent, String child)

Creates an instance of the File class by concatenating the child String to the
parent String, and converting the combined String to an abstract path name.

File(File parent, String child)

Creates an instance of the File class by constructing an abstract path name from
the abstract path name of the parent File, and the String path name of
CONCLUSION

This training focused upon increasing our knowledge and interest


in toward the java. Because java is most interesting and most used language
in these days. We learnt how to create a web sites and web pages. It was a
great experience.It increases our practical skills that are the main yhing
which we learnt in the training session.

The Advance Java problem set, a step-by-step approach used by the


student, has ensured that the student acquired enough knowledge required in
various aspects for all the stages, before proceeding on. During the 5 months
attachment in Hewlett-Packard Lab, the student had done the following:

- Designed a business model (represented in XML) that enabled


information in the form of XML documents to be shared between
businesses.
- Implemented a GUI that allowed a user to view and modify the
information in an XML document. XSL was used to transform XML into
HTML for display in a browser.
- Wrote a Java Servlet code to handle the file access application required
by the web client. These are application that cannot be handled by a Java
Applet.
- Implemented the Text Editor Interface (client) using HTML, a
straightforward language used to display certain interface program onto a
web browser.
- Designed a class diagram using the Unified Modeling Language (UML)
that defines the classes and their behavior.
- Created a Text Editor Interface Application using the Java Applet. This
allows greater flexibility than HTML in terms of the layout and
functionality.

Having the knowledge of Java, HTML, XML, XSL, and UML


proved to be immensely useful. This had also built up the student’s
confidence in dealing with feature-rich, user-friendly Windows
applications.
REFERENCE

Books-

- Programming with JAVA (E Balagurusamy)


- Mastering Enterprise JAVA( ED Roman)
- Thinking in Java (Bruce Eckel)
- Complete Reference to JAVA2
- Sun Microsystems and study material

Website-

- www.cmcltd.com
- wikipedia.org/wiki
- www.scribd.com
- Slideshare.net
- www.tutorialspoint.com
- Javatpoint.com

You might also like