SlideShare a Scribd company logo
Advance Java 
Presented by 
Prince Soni
Company Profile
Advance Java 
• Java Core 
▫ Genesis of Java 
• Lesson 3: Name 
▫ Provide brief description, if desired.
Lesson 1: Genesis Of Java 
• Java’s Creator – James Gosling 
• Why Java is so important to Internet ? 
• Java’s Magic : The Bytecode 
• The Java Buzzwords
Quick Walkthrough of Java Core 
• Data Types, Variables, Arrays, 
Operators, Classes, Methods, 
Inheritence 
• Packages, Interfaces, Exception 
Handling, I/O Applets and Networking 
Basics, Collection Framework 
• Event Handling, AWT and Swing
Java Core : Wrap-up 
Few miscellaneous features of Java’s Core to be noted are :- 
• Java is a strongly typed language 
• Java’s Primitive data types – Default Values and size 
• Java’s Automatic Conversion 
• The this keyword 
• Garbage Collection ( Java’s Destructor !! ) 
• Argument passing – Call By Value and Call By Reference 
• Using super to access hidden data 
• Object Class 
• Packages it’s importance and where does Java looks for user defined packages. Role 
of Access Specifier in Packages 
• Java’s extend keyword only support single inheritance. Other forms of inheritance 
could be achieved through interfaces
Java Core : Wrap-up 
• Interfaces – dynamic lookup of a method. 
• Exception handling – Try, Catch, throw, throws, 
finally. Super class – Throwable. It’s sub class – 
Exception and Error 
• Thread Synchronization 
• Collection Framework – D/f Interfaces 
• Swing Using Net Beans IDE.
Lesson 2: JEEE – What is it ???? 
• What Is the Java 2 Enterprise Edition (J2EE)? 
Depending upon whom you ask, Java 2 Enterprise Edition 
(J2EE) is oneof many things :- 
• Web components such as JSPs, Servlets, or Web 
Archives. 
• EJB components, which are Java Archives containing EJB 
code and the deployment descriptor. 
• Client applications, which are stand-alone Java 
executable.
Archives, Descriptor and manifest 
• Descriptors provide configuration information ,environment 
settings, role-based security, and vendor-specific information. 
• The manifest is a packing slip that is automatically generated by 
the archive process. 
• 1. Java Archives (JAR)—A JAR file encapsulates one or more Java 
• classes, a manifest, and a descriptor. JAR files are the lowest 
• level of archive. JAR files are used in J2EE for packaging EJBs 
• and client-side Java Applications. 
• 2. Web Archives (WAR)—WAR files are similar to JAR files, except 
• that they are specifically for web applications made from 
• Servlets, JSPs, and supporting classes. 
• 3. Enterprise Archives (EAR)—An EAR file contains all of the 
• components that make up a particular J2EE application.
Introduction to J2EE Enterprise Architecture 
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M 
The Web Tier 
Servlets
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M 
J2EE Architecture 
• Multi-tiered, distributed application model 
• Component based 
• Unified security model 
• Flexible transaction control 
• Web services support 
• Open systems development - not tied into one vendor
Application Servers Copyright 2002 © Paulo Merson 
• In the 90’s, systems should be client-server
Application Servers Copyright 2002 © Paulo Merson 
• Today, enterprise applications use the 
multi-tier model
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M 
J2EE Components 
• A component is a self-contained functional software unit that is 
assembled into a J2EE application with its related classes and files and 
that communicates with other components. 
• Application clients and applets are components that run on the client. 
• Java Servlets and JavaServer Pages™ (JSP™) technology components are 
Web components that run on the server. 
• Enterprise JavaBeans™ (EJB™) components (enterprise beans) are 
business components that run on the server.
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M 
J2EE Components 
• Application client 
▫ Web client 
Consists of two parts: (1) dynamic web pages containing various types of markup language 
(HTML, XML, and so on), which are generated by web components running in the server’s web 
tier, and (2) a web browser, which renders the pages received from the server 
▫ Applets 
A web page received from the web tier can include an embedded applet. An applet is a small 
client application written in the Java programming language that executes in the Java virtual 
machine (JVM) installed in the web browser 
▫ Application client 
An application client runs on a client machine and provides a way for users to handle tasks that 
require a richer user interface than can be provided by a mark up language. It typically has a 
graphical user interface (GUI) created from Java’s Swing or AWT APIs, but a command-line 
interface is also possible
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M 
J2EE Components 
• JavaBeans Components 
▫ Application may include JavaBeans 
▫ Have properties, get- and set- methods 
▫ Not considered J2EE components by the J2EE specification 
• J2EE Web Components 
▫ Either Servlets or JSP pages 
Servlets are Java programming language classes that dynamically process requests and 
construct responses. 
JavaServer Pages are text-based documents that execute as servlets but allow a more natural 
approach to creating static content. 
• Business Components (Enterprise JavaBeans) 
▫ Business code, which is logic that solves or meets the needs of a particular business 
domain such as banking is handled by enterprise beans running in the business tier.
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M 
The Different Components 
Web browser 
Dynamic web pages 
Applet & optional 
JavaBeans 
components 
Application Client 
JavaServer 
Pages 
Servlets 
JavaBeans 
components 
(optional) 
Entity Beans 
Session Beans 
Message-driven 
Beans 
Database 
and Legacy 
Systems 
Web Tier Business Tier 
Client Tier 
J2EE Server (Container)
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M The Different Containers 
Applet Container 
Web browser 
Java Plug-in 
J2EE Server (Container) 
JavaServer 
Pages 
Servlets 
Entity Beans 
Session Beans 
Message-driven 
Beans 
Database 
and Legacy 
Systems 
Web Container 
EJB Container 
Application client 
Application client 
Container
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M 
Containers and Server-side Technologies 
• A container is a runtime service provides the interface between components and 
the low-level functionality that supports the component 
• A container manages components that have been developed according to the API 
specifications 
• We shall use Sun’s Java System Application Server as our J2EE container. It provides 
both a web container (for servlets and JSPs) and an EJB container (for enterprise 
beans) 
• Servlets and JSPs are server-side technologies that accept HTTP requests from an 
HTML form and return responses in the form of HTML, XHTML or XML documents 
• At runtime, servlets and JSPs are equivalent: You could write an application just 
using servlets or just using JSPs or you could mix them up; we’ll have a look later at 
a suitable policy for choosing when to use servlets or JSPs
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M 
Servlet interface 
• Architecturally all servlets must implement the servlet interface 
• The methods of the Servlet interface are invoked automatically by the servlet container 
• The interface has five methods 
• void init(ServletConfig config) 
▫ This method is called once during the servlet’s execution cycle to initialise the servlet (the argument is supplied by the 
container) 
• ServletConfig getServletConfig() 
▫ Returns an object reference to an object that implements interface ServletConfig (provides access to servlet configuration 
information as well as information about it’s environment (the container) 
• String getServiceInfo() 
▫ Returns information such as the servlet’s author and version 
• void service(ServletRequest request, ServletResponse response) 
▫ This method is called as a result of a client request 
• void destroy() 
▫ Clean up method called when the servlet is destroyed by it’s container
Servlet’s lifecycle - UML state diagram 
Instantiation based on 
a request or at 
container start up 
Does not exist Instantiated 
Initialisation 
Initialisation failed 
Unavailable Initialised and/or 
Ready for requests 
Back to service 
In case of temporary 
unavailability 
HTTP request(s) 
From client(s) 
Destroyed Service 
End of 
Service 
Temporary or permanent thread 
failure 
Time out or container shutdown
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M 
HttpServlet class 
• Web-based servlets typically extend the HttpServlet class 
• HttpServlet is an abstract class that must be extended to create an HTTP 
servlet suitable for a Web site. A subclass of HttpServlet must override at 
least one method, usually one of these: 
▫ doGet() – if the servlet is to support HTTP GET requests 
▫ doPost() – for HTTP POST requests 
▫ doPut() – for HTTP PUT requests 
▫ doDelete() – for HTTP DELETE requests 
▫ init() and destroy() – to manage resources that are held for the life of 
the servlet 
▫ getServletInfo() – if the servlet is to provide information about itself 
• There's almost no reason to override the service method, which handles 
standard HTTP requests by dispatching them to the handler methods for each 
HTTP request type (the doXXX methods listed above).
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M 
doGet() and doPost() methods 
• doGet(HttpServletRequest req, HttpServletResponse resp) 
▫ Processes an HTTP GET request; e.g. to retrieve the contents of a specified URL (an HTML 
or XHTML or XML page) 
▫ The response content can be generated dynamically by the servlet 
▫ URL can be typed into a browser’s address or Location field and can be bookmarked 
• doPost(HttpServletRequest req, HttpServletResponse resp) 
▫ Processes an HTTP POST request; typically used to post data from an HTML form to a 
server-side form handler that processes the data (save it in a database or retrieve data 
from the database.) 
▫ Cannot be typed into a browser’s address or Location field and cannot be bookmarked 
▫ an HTTP POST request does not have a restriction on the amount of data transmitted 
• Safer to use doPost() method! 
▫ Except if you’re using a servlet to display information without an associated html page – 
then use doGet() method
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M 
Simple example using doGet() method 
• On the Client side we need: 
▫ An HTML client; e.g. a web browser 
• On the server side we need: 
▫ A servlet with a doGet() method 
▫ A web.xml file that relates the servlet’s URL to the servlet’s Java class 
Usually this is generated for you by the IDE (NetBeans in this case)
The web client 
<html> 
<head> 
<title>HelloServlet</title> 
</head> 
<body> 
<form action="hello" method="get"> 
<p>press Submit to invoke servlet HelloServlet</p> 
<p><input type="submit" name="Submit" value="Submit"></p> 
</form> 
</body> 
</html> 
• This file is called index.html 
• Note the form tags 
▫ The form’s action attribute specifies the servlet’s URL, i.e. hello 
J2EE Enterprise Architecture Enterprise Applications CE00465-M
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M 
<web-app> 
web.xml 
<display-name>HelloWorld</display-name> 
<servlet> 
<servlet-name>helloservlet</servlet-name> 
<servlet-class>entapps.HelloServlet</servlet-class> 
</servlet> 
<servlet-mapping> 
<servlet-name>helloservlet</servlet-name> 
<url-pattern>/hello</url-pattern> 
</servlet-mapping> 
</web-app> 
• Note for each servlet you need two tags 
▫ <servlet> 
▫ <servlet-mapping>
J2EE Enterprise 
Architecture 
Enterprise HelloServlet.java Applications CE00465-M 
package entapps; 
import javax.servlet.*; 
import javax.servlet.http.*; 
import java.io.*; 
public class HelloServlet extends HttpServlet 
{ 
public void doGet(HttpServletRequest request, 
HttpServletResponse response) 
throws ServletException, IOException 
{ 
response.setContentType("text/html"); 
PrintWriter out = response.getWriter(); 
out.println("<html>"); 
out.println("<head><title>Hello Servlet</title></head>"); 
out.println("<body bgcolor="lightblue">"); 
out.println("<p>Hello World.</p>"); 
out.println("</body></html>"); 
} 
}
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M 
Running in NetBeans 
• In NetBeans you need to build the project, then deploy the project and finally run 
the project. If you choose the run option then NetBeans will automatically build, 
deploy and run the project. 
• Once the application is deployed and run you see the following URL in a web 
browser (assuming you called the project SimpleServlet) 
▫ http://localhost:8080/SimpleServlet/ 
▫ When you hit the submit button Hello World is displayed and the URL becomes 
▫ http://localhost:8080/SimpleServlet/hello?Submit=Submit 
• If you changed the doGet() method to a doPost() method and submit the form the 
URL becomes 
▫ http://localhost:8080/SimpleServlet/hello 
▫ You can’t see the input parameters 
▫ If you bookmark it then you can’t retrieve the URL
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M Adding in request parameters 
• If you wanted to add in the name of the user in the html page 
and display it in the servlets, we would add in 
UserID:<input type ="text" name = "requsername" > 
in the html file 
and 
out.println("<p>from"+request.getParameter("requsername")); 
in the servlet 
• Sometimes you may want to hide the request parameter from 
the user (but still visible when you choose view source from 
within the browser) 
▫ Set the type attribute to “hidden” 
<input type = "hidden" name = "action" value 
="entryform"> 
▫ This will be hidden from the user, you can use it in the servlet by 
String action = 
request.getParameter("action");
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M 
Initialising Servlets 
• Common in real-life servlets 
▫ E.g. initialising database connection pools 
• Use SevletConfig.getInitParameters() to read initialisation parameters 
▫ Call getServletConfig() to obtain the ServletConfig object 
• Set initialisation parameters in web.xml 
• It is common to use the init() method even when you don’t read 
initialisation parameters 
▫ e.g. to set up data structures that don’t change during the life cycle of the 
servlet, to load information from disk, etc.
J2EE Enterprise 
Architecture 
Enterprise Example of Initialising parameters Applications CE00465-M 
<web-app> 
<display-name>HelloWorld</display-name> 
<servlet> 
<init-param> 
<param-name>defaultusername</param-name> 
<param-value>Harry</param-value> 
</init-param> 
<servlet-name>helloservlet</servlet-name> 
<servlet-class>web.HelloServlet</servlet-class> 
</servlet> 
<servlet-mapping> 
<servlet-name>helloservlet</servlet-name> 
<url-pattern>/helloservlet</url-pattern> 
</servlet-mapping> 
</web-app>
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M 
Example of Initialising parameters 
public void doPost(HttpServletRequest request, 
HttpServletResponse response) 
throws ServletException, IOException 
{ 
response.setContentType("text/html"); 
PrintWriter out = response.getWriter(); 
ServletConfig config = getServletConfig(); 
out.println("<html>"); 
out.println("<head><title>HelloServlet</title></head>"); 
out.println("<body bgcolor="lightblue">"); 
out.println("<p>Hello World.</p>"); 
out.println("<p>from " + request.getParameter("requsername")); 
out.println("<p>and from " + 
config.getInitParameter("defaultusername") + 
"the default user"); 
out.println("</body></html>"); 
}
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M 
Request Headers 
• Every request has header information associated with it e.g. whether 
the request is a post or get request, the protocol associated with the 
request, etc. The request object has methods associated to display 
these headers . Below is a servlet that prints these headers out for a 
particular request 
public class ShowRequestHeaders extends HttpServlet 
{ 
public void doGet(HttpServletRequest request, 
HttpServletResponse response) 
throws ServletException, IOException 
{ 
response.setContentType("text/html"); 
PrintWriter out = response.getWriter(); 
String title = "Servlet Example: Showing Request Headers";
J2EE Enterprise 
Architecture 
Enterprise Request Headers Applications CE00465-M 
out.println( 
"<head><title>Servlet Example: " + 
"Showing Request Headers</title></head>" + 
"<body bgcolor="#B0C4DE ">n" + 
"<h1 align=center>" + title + "</h1>n" + 
"<b>Request Method: </b>" + request.getMethod() + "<br>n" + 
"<b>Request URI: </b>" + 
request.getRequestURI() + "<br>n" + 
"<b>Request Protocol: </b>" + 
request.getProtocol() + "<br><br>n" + 
"<table border=1 align=center>n" + 
"<tr bgcolor=#6495ed">n" + 
"<th>Header Name<th>Header Value"); 
Enumeration headerNames = request.getHeaderNames(); 
while (headerNames.hasMoreElements()) 
{ 
String headerName = (String)headerNames.nextElement(); 
out.println("<tr><td>" + headerName + "</td><td>" ); 
out.println(request.getHeader(headerName) + "</td><td>" ); 
} 
out.println("</table>n</body></html>"); 
}
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M Request Headers
J2EE Enterprise 
Architecture 
Enterprise Statelessness and Sessions Applications CE00465-M 
• HTTP is a stateless protocol 
▫ Each request from a client is treated in isolation from all 
other requests from the same client i.e. there is no 
relationship between the different requests 
▫ A protocol is stateful if the response to a given request may 
depend not only on the current request but also on the 
outcome of previous requests 
• Stateful is important 
▫ With on-line banking you only need to register once 
e.g. looking at your balances, or transferring money from 
one account to another doesn’t require you to re-register for 
every request 
▫ Internet shopping with a shopping cart 
• Session 
▫ The server should be able to identify that a series of requests 
from a single client form a single working session 
• State 
▫ The server should be able to remember information related 
to previous requests in a single session
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M 
Session Tracking with the Java Servlet API 
• The javax.servlet.http.HttpSession interface encapsulates the notion of a 
session 
▫ The request object provides the getSession() method which gives you access to 
the HttpSession object associated with the client making the request 
▫ The HttpSession object is an implicit object and is created for each client by the 
web container 
▫ The web container maintains this object for the duration of the client session 
▫ Each session consumes memory on the server-side, so it’s unwise to keep 
sessions open forever 
Most e-commerce sites limit this interval to less than 30 minutes 
You can specify the session limit time in the web.xml file using a deployment 
descriptor element <session-config>
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M 
Session Methods for Managing State 
• setAttribute() method 
▫ public setAttribute(String name, Object attribute) 
 Binds an object to this session, using the name specified 
• getAttribute() method 
▫ public Object getAttribute(String name) 
This method returns the attribute bound with the specified name in this session, or null if no 
object is bound under the name 
• getAttributeNames() method 
▫ public Enumeration getAttributeNames(String name) 
Returns an Enumeration of String objects containing the names of all the objects bound to this 
session. 
• removeAttribute() method 
▫ public void removeAttribute(String name) 
Removes the object bound with the specified name from this session.
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M 
Using the state management methods 
String un = request.getParameter("username"); 
request.getSession().setAttribute("user", un); 
String un=(String)request.getSession().getAttribute("user");
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M 
Request Dispatching 
• Allows a servlet or a JSP page to dispatch a request to an HTML page, or another 
servlet or JSP page, which will then be responsible for any further processing and for 
generating the response. 
• The request object has an interface for this purpose: javax.servlet.RequestDispatcher 
• The request dispatcher object has two methods which allow delegation of the 
request-response processing to another resource after the dispatching servlet has 
finished any preliminary processing 
▫ public void forward(ServletRequest req, ServletResponse resp) throws ServletException, 
java.io.IOException) 
Allows the request to be forwarded to another servlet or JSP page which then assumes all 
responsibility for producing the response 
▫ public void include(ServletRequest req, ServletResponse resp) throws ServletException, 
java.io.IOException) 
Allows the inclusion of the content produced by another resource in the dispatching servlet’s 
response
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M 
Examples of Request Dispatching 
RequestDispatcher rd = 
request.getRequestDispatcher("oracleconnection"); 
rd.include(request, response); 
String status = 
(String)request.getSession().getAttribute("status"); 
if (status.equalsIgnoreCase("OK")) 
{ 
response.sendRedirect("connection_established.html"); 
} 
else 
{ 
rd = request.getRequestDispatcher("viewerror"); 
rd.forward(request, response); 
}
J2EE Enterprise 
Architecture 
Enterprise Applications CE00465-M 
Examples of Request Dispatching 
• oracleconnection is the name of a servlet, 
OracleConnection.java 
▫ Specified in web.xml 
<servlet> 
<servlet-name>oracleconnection</servlet-name> 
<servlet-class>entapps.OracleConnection</servlet-class> 
</servlet> 
<servlet-mapping> 
<servlet-name>oracleconnection</servlet-name> 
<url-pattern>/oracleconnection</url-pattern> 
</servlet-mapping>
Examples of Request Dispatching 
• Status is the name of an attribute that OracleConnection sets to either “OK” or “fail” 
• public void sendRedirect(java.lang.String location) throws java.io.IOException 
▫ Sends a temporary redirect response to the client using the specified redirect 
location URL. This method can accept relative URLs; the servlet container must 
convert the relative URL to an absolute URL before sending the response to the 
client. If the location is relative without a leading '/' the container interprets it as 
relative to the current request URI. If the location is relative with a leading '/' the 
container interprets it as relative to the servlet container root. (source 
java.sun.com) 
• viewerror is the name of a Servlet specified in web.xml.

More Related Content

PDF
Lecture 1: Introduction to JEE
PDF
jsf2 Notes
PDF
Lecture 2: Servlets
ODP
Hibernate Developer Reference
PPT
Hybernat and structs, spring classes in mumbai
PPT
Chap3 3 12
PPT
EJB 3.0 Java Persistence with Oracle TopLink
PDF
Hibernate 3
Lecture 1: Introduction to JEE
jsf2 Notes
Lecture 2: Servlets
Hibernate Developer Reference
Hybernat and structs, spring classes in mumbai
Chap3 3 12
EJB 3.0 Java Persistence with Oracle TopLink
Hibernate 3

What's hot (19)

PPTX
Hibernate example1
PPT
Java database connectivity
PDF
PDF
Java EE 8 Recipes
PPTX
2 jdbc drivers
PPTX
3 jdbc api
PPS
Jdbc architecture and driver types ppt
PPT
Hibernate architecture
PPSX
JSP - Part 1
PDF
EJB 3.0 - Yet Another Introduction
PPTX
4 jdbc step1
PPTX
Dao example
PDF
PPT
Basic Java Database Connectivity(JDBC)
PDF
Java Web Programming [4/9] : JSP Basic
PPTX
Java.sql package
PDF
Hibernate Presentation
PPT
J2EE - JSP-Servlet- Container - Components
PPTX
Hibernate in Action
Hibernate example1
Java database connectivity
Java EE 8 Recipes
2 jdbc drivers
3 jdbc api
Jdbc architecture and driver types ppt
Hibernate architecture
JSP - Part 1
EJB 3.0 - Yet Another Introduction
4 jdbc step1
Dao example
Basic Java Database Connectivity(JDBC)
Java Web Programming [4/9] : JSP Basic
Java.sql package
Hibernate Presentation
J2EE - JSP-Servlet- Container - Components
Hibernate in Action
Ad

Viewers also liked (6)

PPT
Summer training java
PPT
J2EE and Servlet
PPT
J2 EEE SIDES
PPT
Java J2EE
PPTX
J2ee seminar
PPT
Pre colonial philippines
Summer training java
J2EE and Servlet
J2 EEE SIDES
Java J2EE
J2ee seminar
Pre colonial philippines
Ad

Similar to Advance java1.1 (20)

PPT
Lecture 19 - Dynamic Web - JAVA - Part 1.ppt
PPT
Lecture 19 dynamic web - java - part 1
PDF
Jeetrainers.com coursejspservlets00
PDF
Coursejspservlets00
PPT
Ppt for Online music store
PPSX
Intorduction to struts
PPT
192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt
PDF
EJ NOV-18 (Sol) (E-next.in).pdf
PDF
Java Introduction
PDF
Java Platform and IDE Netbeans 6.7 for Developing Enterprise and Web Applicat...
PPT
Introduction to java_ee
PPT
Enterprise application developement
PPTX
Java Training in Chennai
PPTX
4. J2EE.pptx
PPTX
Devjyotippt
PDF
PDF
PPTX
presentation on online movie ticket booking
PPTX
Advance Java Topics (J2EE)
PPT
Module 4.pptModule 4.pptModule 4.pptModule 4.ppt
Lecture 19 - Dynamic Web - JAVA - Part 1.ppt
Lecture 19 dynamic web - java - part 1
Jeetrainers.com coursejspservlets00
Coursejspservlets00
Ppt for Online music store
Intorduction to struts
192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt
EJ NOV-18 (Sol) (E-next.in).pdf
Java Introduction
Java Platform and IDE Netbeans 6.7 for Developing Enterprise and Web Applicat...
Introduction to java_ee
Enterprise application developement
Java Training in Chennai
4. J2EE.pptx
Devjyotippt
presentation on online movie ticket booking
Advance Java Topics (J2EE)
Module 4.pptModule 4.pptModule 4.pptModule 4.ppt

More from Prince Soni (7)

DOC
Prince soni btech_fresher_android
PPTX
4 stars
PPTX
Frequent english words
PPTX
Area filling algo
PPTX
Compututer Graphics - Color Modeling And Rendering
PPT
PPTX
Advance java1.2
Prince soni btech_fresher_android
4 stars
Frequent english words
Area filling algo
Compututer Graphics - Color Modeling And Rendering
Advance java1.2

Recently uploaded (20)

PPTX
Geodesy 1.pptx...............................................
PPTX
web development for engineering and engineering
PPTX
additive manufacturing of ss316l using mig welding
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPT
Mechanical Engineering MATERIALS Selection
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Welding lecture in detail for understanding
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
PPT on Performance Review to get promotions
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Construction Project Organization Group 2.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
composite construction of structures.pdf
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Geodesy 1.pptx...............................................
web development for engineering and engineering
additive manufacturing of ss316l using mig welding
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Operating System & Kernel Study Guide-1 - converted.pdf
Mechanical Engineering MATERIALS Selection
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Welding lecture in detail for understanding
Internet of Things (IOT) - A guide to understanding
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPT on Performance Review to get promotions
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
bas. eng. economics group 4 presentation 1.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Construction Project Organization Group 2.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
composite construction of structures.pdf
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx

Advance java1.1

  • 1. Advance Java Presented by Prince Soni
  • 3. Advance Java • Java Core ▫ Genesis of Java • Lesson 3: Name ▫ Provide brief description, if desired.
  • 4. Lesson 1: Genesis Of Java • Java’s Creator – James Gosling • Why Java is so important to Internet ? • Java’s Magic : The Bytecode • The Java Buzzwords
  • 5. Quick Walkthrough of Java Core • Data Types, Variables, Arrays, Operators, Classes, Methods, Inheritence • Packages, Interfaces, Exception Handling, I/O Applets and Networking Basics, Collection Framework • Event Handling, AWT and Swing
  • 6. Java Core : Wrap-up Few miscellaneous features of Java’s Core to be noted are :- • Java is a strongly typed language • Java’s Primitive data types – Default Values and size • Java’s Automatic Conversion • The this keyword • Garbage Collection ( Java’s Destructor !! ) • Argument passing – Call By Value and Call By Reference • Using super to access hidden data • Object Class • Packages it’s importance and where does Java looks for user defined packages. Role of Access Specifier in Packages • Java’s extend keyword only support single inheritance. Other forms of inheritance could be achieved through interfaces
  • 7. Java Core : Wrap-up • Interfaces – dynamic lookup of a method. • Exception handling – Try, Catch, throw, throws, finally. Super class – Throwable. It’s sub class – Exception and Error • Thread Synchronization • Collection Framework – D/f Interfaces • Swing Using Net Beans IDE.
  • 8. Lesson 2: JEEE – What is it ???? • What Is the Java 2 Enterprise Edition (J2EE)? Depending upon whom you ask, Java 2 Enterprise Edition (J2EE) is oneof many things :- • Web components such as JSPs, Servlets, or Web Archives. • EJB components, which are Java Archives containing EJB code and the deployment descriptor. • Client applications, which are stand-alone Java executable.
  • 9. Archives, Descriptor and manifest • Descriptors provide configuration information ,environment settings, role-based security, and vendor-specific information. • The manifest is a packing slip that is automatically generated by the archive process. • 1. Java Archives (JAR)—A JAR file encapsulates one or more Java • classes, a manifest, and a descriptor. JAR files are the lowest • level of archive. JAR files are used in J2EE for packaging EJBs • and client-side Java Applications. • 2. Web Archives (WAR)—WAR files are similar to JAR files, except • that they are specifically for web applications made from • Servlets, JSPs, and supporting classes. • 3. Enterprise Archives (EAR)—An EAR file contains all of the • components that make up a particular J2EE application.
  • 10. Introduction to J2EE Enterprise Architecture J2EE Enterprise Architecture Enterprise Applications CE00465-M The Web Tier Servlets
  • 11. J2EE Enterprise Architecture Enterprise Applications CE00465-M J2EE Architecture • Multi-tiered, distributed application model • Component based • Unified security model • Flexible transaction control • Web services support • Open systems development - not tied into one vendor
  • 12. Application Servers Copyright 2002 © Paulo Merson • In the 90’s, systems should be client-server
  • 13. Application Servers Copyright 2002 © Paulo Merson • Today, enterprise applications use the multi-tier model
  • 14. J2EE Enterprise Architecture Enterprise Applications CE00465-M J2EE Components • A component is a self-contained functional software unit that is assembled into a J2EE application with its related classes and files and that communicates with other components. • Application clients and applets are components that run on the client. • Java Servlets and JavaServer Pages™ (JSP™) technology components are Web components that run on the server. • Enterprise JavaBeans™ (EJB™) components (enterprise beans) are business components that run on the server.
  • 15. J2EE Enterprise Architecture Enterprise Applications CE00465-M J2EE Components • Application client ▫ Web client Consists of two parts: (1) dynamic web pages containing various types of markup language (HTML, XML, and so on), which are generated by web components running in the server’s web tier, and (2) a web browser, which renders the pages received from the server ▫ Applets A web page received from the web tier can include an embedded applet. An applet is a small client application written in the Java programming language that executes in the Java virtual machine (JVM) installed in the web browser ▫ Application client An application client runs on a client machine and provides a way for users to handle tasks that require a richer user interface than can be provided by a mark up language. It typically has a graphical user interface (GUI) created from Java’s Swing or AWT APIs, but a command-line interface is also possible
  • 16. J2EE Enterprise Architecture Enterprise Applications CE00465-M J2EE Components • JavaBeans Components ▫ Application may include JavaBeans ▫ Have properties, get- and set- methods ▫ Not considered J2EE components by the J2EE specification • J2EE Web Components ▫ Either Servlets or JSP pages Servlets are Java programming language classes that dynamically process requests and construct responses. JavaServer Pages are text-based documents that execute as servlets but allow a more natural approach to creating static content. • Business Components (Enterprise JavaBeans) ▫ Business code, which is logic that solves or meets the needs of a particular business domain such as banking is handled by enterprise beans running in the business tier.
  • 17. J2EE Enterprise Architecture Enterprise Applications CE00465-M The Different Components Web browser Dynamic web pages Applet & optional JavaBeans components Application Client JavaServer Pages Servlets JavaBeans components (optional) Entity Beans Session Beans Message-driven Beans Database and Legacy Systems Web Tier Business Tier Client Tier J2EE Server (Container)
  • 18. J2EE Enterprise Architecture Enterprise Applications CE00465-M The Different Containers Applet Container Web browser Java Plug-in J2EE Server (Container) JavaServer Pages Servlets Entity Beans Session Beans Message-driven Beans Database and Legacy Systems Web Container EJB Container Application client Application client Container
  • 19. J2EE Enterprise Architecture Enterprise Applications CE00465-M Containers and Server-side Technologies • A container is a runtime service provides the interface between components and the low-level functionality that supports the component • A container manages components that have been developed according to the API specifications • We shall use Sun’s Java System Application Server as our J2EE container. It provides both a web container (for servlets and JSPs) and an EJB container (for enterprise beans) • Servlets and JSPs are server-side technologies that accept HTTP requests from an HTML form and return responses in the form of HTML, XHTML or XML documents • At runtime, servlets and JSPs are equivalent: You could write an application just using servlets or just using JSPs or you could mix them up; we’ll have a look later at a suitable policy for choosing when to use servlets or JSPs
  • 20. J2EE Enterprise Architecture Enterprise Applications CE00465-M Servlet interface • Architecturally all servlets must implement the servlet interface • The methods of the Servlet interface are invoked automatically by the servlet container • The interface has five methods • void init(ServletConfig config) ▫ This method is called once during the servlet’s execution cycle to initialise the servlet (the argument is supplied by the container) • ServletConfig getServletConfig() ▫ Returns an object reference to an object that implements interface ServletConfig (provides access to servlet configuration information as well as information about it’s environment (the container) • String getServiceInfo() ▫ Returns information such as the servlet’s author and version • void service(ServletRequest request, ServletResponse response) ▫ This method is called as a result of a client request • void destroy() ▫ Clean up method called when the servlet is destroyed by it’s container
  • 21. Servlet’s lifecycle - UML state diagram Instantiation based on a request or at container start up Does not exist Instantiated Initialisation Initialisation failed Unavailable Initialised and/or Ready for requests Back to service In case of temporary unavailability HTTP request(s) From client(s) Destroyed Service End of Service Temporary or permanent thread failure Time out or container shutdown
  • 22. J2EE Enterprise Architecture Enterprise Applications CE00465-M HttpServlet class • Web-based servlets typically extend the HttpServlet class • HttpServlet is an abstract class that must be extended to create an HTTP servlet suitable for a Web site. A subclass of HttpServlet must override at least one method, usually one of these: ▫ doGet() – if the servlet is to support HTTP GET requests ▫ doPost() – for HTTP POST requests ▫ doPut() – for HTTP PUT requests ▫ doDelete() – for HTTP DELETE requests ▫ init() and destroy() – to manage resources that are held for the life of the servlet ▫ getServletInfo() – if the servlet is to provide information about itself • There's almost no reason to override the service method, which handles standard HTTP requests by dispatching them to the handler methods for each HTTP request type (the doXXX methods listed above).
  • 23. J2EE Enterprise Architecture Enterprise Applications CE00465-M doGet() and doPost() methods • doGet(HttpServletRequest req, HttpServletResponse resp) ▫ Processes an HTTP GET request; e.g. to retrieve the contents of a specified URL (an HTML or XHTML or XML page) ▫ The response content can be generated dynamically by the servlet ▫ URL can be typed into a browser’s address or Location field and can be bookmarked • doPost(HttpServletRequest req, HttpServletResponse resp) ▫ Processes an HTTP POST request; typically used to post data from an HTML form to a server-side form handler that processes the data (save it in a database or retrieve data from the database.) ▫ Cannot be typed into a browser’s address or Location field and cannot be bookmarked ▫ an HTTP POST request does not have a restriction on the amount of data transmitted • Safer to use doPost() method! ▫ Except if you’re using a servlet to display information without an associated html page – then use doGet() method
  • 24. J2EE Enterprise Architecture Enterprise Applications CE00465-M Simple example using doGet() method • On the Client side we need: ▫ An HTML client; e.g. a web browser • On the server side we need: ▫ A servlet with a doGet() method ▫ A web.xml file that relates the servlet’s URL to the servlet’s Java class Usually this is generated for you by the IDE (NetBeans in this case)
  • 25. The web client <html> <head> <title>HelloServlet</title> </head> <body> <form action="hello" method="get"> <p>press Submit to invoke servlet HelloServlet</p> <p><input type="submit" name="Submit" value="Submit"></p> </form> </body> </html> • This file is called index.html • Note the form tags ▫ The form’s action attribute specifies the servlet’s URL, i.e. hello J2EE Enterprise Architecture Enterprise Applications CE00465-M
  • 26. J2EE Enterprise Architecture Enterprise Applications CE00465-M <web-app> web.xml <display-name>HelloWorld</display-name> <servlet> <servlet-name>helloservlet</servlet-name> <servlet-class>entapps.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>helloservlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app> • Note for each servlet you need two tags ▫ <servlet> ▫ <servlet-mapping>
  • 27. J2EE Enterprise Architecture Enterprise HelloServlet.java Applications CE00465-M package entapps; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class HelloServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head><title>Hello Servlet</title></head>"); out.println("<body bgcolor="lightblue">"); out.println("<p>Hello World.</p>"); out.println("</body></html>"); } }
  • 28. J2EE Enterprise Architecture Enterprise Applications CE00465-M Running in NetBeans • In NetBeans you need to build the project, then deploy the project and finally run the project. If you choose the run option then NetBeans will automatically build, deploy and run the project. • Once the application is deployed and run you see the following URL in a web browser (assuming you called the project SimpleServlet) ▫ http://localhost:8080/SimpleServlet/ ▫ When you hit the submit button Hello World is displayed and the URL becomes ▫ http://localhost:8080/SimpleServlet/hello?Submit=Submit • If you changed the doGet() method to a doPost() method and submit the form the URL becomes ▫ http://localhost:8080/SimpleServlet/hello ▫ You can’t see the input parameters ▫ If you bookmark it then you can’t retrieve the URL
  • 29. J2EE Enterprise Architecture Enterprise Applications CE00465-M Adding in request parameters • If you wanted to add in the name of the user in the html page and display it in the servlets, we would add in UserID:<input type ="text" name = "requsername" > in the html file and out.println("<p>from"+request.getParameter("requsername")); in the servlet • Sometimes you may want to hide the request parameter from the user (but still visible when you choose view source from within the browser) ▫ Set the type attribute to “hidden” <input type = "hidden" name = "action" value ="entryform"> ▫ This will be hidden from the user, you can use it in the servlet by String action = request.getParameter("action");
  • 30. J2EE Enterprise Architecture Enterprise Applications CE00465-M Initialising Servlets • Common in real-life servlets ▫ E.g. initialising database connection pools • Use SevletConfig.getInitParameters() to read initialisation parameters ▫ Call getServletConfig() to obtain the ServletConfig object • Set initialisation parameters in web.xml • It is common to use the init() method even when you don’t read initialisation parameters ▫ e.g. to set up data structures that don’t change during the life cycle of the servlet, to load information from disk, etc.
  • 31. J2EE Enterprise Architecture Enterprise Example of Initialising parameters Applications CE00465-M <web-app> <display-name>HelloWorld</display-name> <servlet> <init-param> <param-name>defaultusername</param-name> <param-value>Harry</param-value> </init-param> <servlet-name>helloservlet</servlet-name> <servlet-class>web.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>helloservlet</servlet-name> <url-pattern>/helloservlet</url-pattern> </servlet-mapping> </web-app>
  • 32. J2EE Enterprise Architecture Enterprise Applications CE00465-M Example of Initialising parameters public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); ServletConfig config = getServletConfig(); out.println("<html>"); out.println("<head><title>HelloServlet</title></head>"); out.println("<body bgcolor="lightblue">"); out.println("<p>Hello World.</p>"); out.println("<p>from " + request.getParameter("requsername")); out.println("<p>and from " + config.getInitParameter("defaultusername") + "the default user"); out.println("</body></html>"); }
  • 33. J2EE Enterprise Architecture Enterprise Applications CE00465-M Request Headers • Every request has header information associated with it e.g. whether the request is a post or get request, the protocol associated with the request, etc. The request object has methods associated to display these headers . Below is a servlet that prints these headers out for a particular request public class ShowRequestHeaders extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Servlet Example: Showing Request Headers";
  • 34. J2EE Enterprise Architecture Enterprise Request Headers Applications CE00465-M out.println( "<head><title>Servlet Example: " + "Showing Request Headers</title></head>" + "<body bgcolor="#B0C4DE ">n" + "<h1 align=center>" + title + "</h1>n" + "<b>Request Method: </b>" + request.getMethod() + "<br>n" + "<b>Request URI: </b>" + request.getRequestURI() + "<br>n" + "<b>Request Protocol: </b>" + request.getProtocol() + "<br><br>n" + "<table border=1 align=center>n" + "<tr bgcolor=#6495ed">n" + "<th>Header Name<th>Header Value"); Enumeration headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String headerName = (String)headerNames.nextElement(); out.println("<tr><td>" + headerName + "</td><td>" ); out.println(request.getHeader(headerName) + "</td><td>" ); } out.println("</table>n</body></html>"); }
  • 35. J2EE Enterprise Architecture Enterprise Applications CE00465-M Request Headers
  • 36. J2EE Enterprise Architecture Enterprise Statelessness and Sessions Applications CE00465-M • HTTP is a stateless protocol ▫ Each request from a client is treated in isolation from all other requests from the same client i.e. there is no relationship between the different requests ▫ A protocol is stateful if the response to a given request may depend not only on the current request but also on the outcome of previous requests • Stateful is important ▫ With on-line banking you only need to register once e.g. looking at your balances, or transferring money from one account to another doesn’t require you to re-register for every request ▫ Internet shopping with a shopping cart • Session ▫ The server should be able to identify that a series of requests from a single client form a single working session • State ▫ The server should be able to remember information related to previous requests in a single session
  • 37. J2EE Enterprise Architecture Enterprise Applications CE00465-M Session Tracking with the Java Servlet API • The javax.servlet.http.HttpSession interface encapsulates the notion of a session ▫ The request object provides the getSession() method which gives you access to the HttpSession object associated with the client making the request ▫ The HttpSession object is an implicit object and is created for each client by the web container ▫ The web container maintains this object for the duration of the client session ▫ Each session consumes memory on the server-side, so it’s unwise to keep sessions open forever Most e-commerce sites limit this interval to less than 30 minutes You can specify the session limit time in the web.xml file using a deployment descriptor element <session-config>
  • 38. J2EE Enterprise Architecture Enterprise Applications CE00465-M Session Methods for Managing State • setAttribute() method ▫ public setAttribute(String name, Object attribute)  Binds an object to this session, using the name specified • getAttribute() method ▫ public Object getAttribute(String name) This method returns the attribute bound with the specified name in this session, or null if no object is bound under the name • getAttributeNames() method ▫ public Enumeration getAttributeNames(String name) Returns an Enumeration of String objects containing the names of all the objects bound to this session. • removeAttribute() method ▫ public void removeAttribute(String name) Removes the object bound with the specified name from this session.
  • 39. J2EE Enterprise Architecture Enterprise Applications CE00465-M Using the state management methods String un = request.getParameter("username"); request.getSession().setAttribute("user", un); String un=(String)request.getSession().getAttribute("user");
  • 40. J2EE Enterprise Architecture Enterprise Applications CE00465-M Request Dispatching • Allows a servlet or a JSP page to dispatch a request to an HTML page, or another servlet or JSP page, which will then be responsible for any further processing and for generating the response. • The request object has an interface for this purpose: javax.servlet.RequestDispatcher • The request dispatcher object has two methods which allow delegation of the request-response processing to another resource after the dispatching servlet has finished any preliminary processing ▫ public void forward(ServletRequest req, ServletResponse resp) throws ServletException, java.io.IOException) Allows the request to be forwarded to another servlet or JSP page which then assumes all responsibility for producing the response ▫ public void include(ServletRequest req, ServletResponse resp) throws ServletException, java.io.IOException) Allows the inclusion of the content produced by another resource in the dispatching servlet’s response
  • 41. J2EE Enterprise Architecture Enterprise Applications CE00465-M Examples of Request Dispatching RequestDispatcher rd = request.getRequestDispatcher("oracleconnection"); rd.include(request, response); String status = (String)request.getSession().getAttribute("status"); if (status.equalsIgnoreCase("OK")) { response.sendRedirect("connection_established.html"); } else { rd = request.getRequestDispatcher("viewerror"); rd.forward(request, response); }
  • 42. J2EE Enterprise Architecture Enterprise Applications CE00465-M Examples of Request Dispatching • oracleconnection is the name of a servlet, OracleConnection.java ▫ Specified in web.xml <servlet> <servlet-name>oracleconnection</servlet-name> <servlet-class>entapps.OracleConnection</servlet-class> </servlet> <servlet-mapping> <servlet-name>oracleconnection</servlet-name> <url-pattern>/oracleconnection</url-pattern> </servlet-mapping>
  • 43. Examples of Request Dispatching • Status is the name of an attribute that OracleConnection sets to either “OK” or “fail” • public void sendRedirect(java.lang.String location) throws java.io.IOException ▫ Sends a temporary redirect response to the client using the specified redirect location URL. This method can accept relative URLs; the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading '/' the container interprets it as relative to the current request URI. If the location is relative with a leading '/' the container interprets it as relative to the servlet container root. (source java.sun.com) • viewerror is the name of a Servlet specified in web.xml.