Lecture 19 - Dynamic Web - JAVA - Part 1
Lecture 19 - Dynamic Web - JAVA - Part 1
Java platform
• A Java platform comprises the JVM together with supporting
class libraries.
libraries
http://www.oracle.com/technetwork/java/javaee/javaee-faq-jsp-135209.html#diff
Java EE Containers
When a request comes in:
• a Servlet needs to be instantiated and create a new thread to
handle the request.
• call the Servlet’s doPost()or
doPost() doGet() method and pass the
HTTP request and HTTP response objects
• get the request and the response to the Servlet
• manage the life, death and resources of the Servlet
* All of the above are the tasks of the web container.
container
Java EE Containers
Java EE SERVER
Web
server My codes
PHP
HTTP HTML MySQL
interpreter
Client
Operating System
Web
TCP/IP
browser
Operating
System
Internet
Historically (Java Web App)
Server: response
• Webserver supports
HTTP. Server
<html>
<head>
GET... GET...
</head>
<body>
...
Web server
<body>
</html>
Web
Container Servlet
Application (Java code)
HTTP <html> (Java code)
Client <head>
</head>
<body>
...
<body>
</html>
Operating System
Web
TCP/IP
browser
Operating
System
It’s the Container that gives
the Servlet the HTTP request
and response, and it’s the
Internet Container that calls the Servlet’s
methods (e.g. doPost() or doGet())
Historically (Java Web App)
Server: response
• Webserver supports
HTTP. Server
<html>
<head>
GET... GET...
</head>
<body>
...
Web server
<body>
</html>
Servlet
HTTP (Java code)
<html>
Client <head>
</head>
<body>
...
<body>
</html>
Operating System
Web
TCP/IP
browser
Operating
System
It’s the Container that gives
the Servlet the HTTP request
and response, and it’s the
Internet Container that calls the Servlet’s
methods (e.g. doPost() or doGet())
(Java Web App) Server: response
• Webserver supports
HTTP. Server
GET...
Grizzly is now the Web server <html>
<head>
</head>
the application
server
Servlet
(Java code)
HTTP
Client
Operating System
Web
TCP/IP
browser
Operating
System
It’s the Container that gives
the Servlet the HTTP request
and response, and it’s the
Internet Container that calls the Servlet’s
methods (e.g. doPost() or doGet())
Java Servlets
Java Servlets simplify web development by providing
infrastructure for component, communication, and
session management in a web container that is
integrated with a web server.
server
• Writing Servlets is like writing Java codes that place an
HTML page inside a Java class (this is the worst part of
Servlets!)
Client/Database Server
application client or applet components running on the
Java EE server
NameHandler.java
Class NameHandler
containing user data
Web profile
Java Application Server: Glassfish
GlassFish
is an open source application server project led
by Sun Microsystems for the Java EE platform. The
proprietary version is called Oracle GlassFish
Enterprise Server. GlassFish is free software
Sun is the original creator
of Tomcat
It uses a derivative of Apache Tomcat as the servlet
container for serving Web content, with an added
component called Grizzly which uses Java NIO for
scalability and speed.
https://grizzly.dev.java.net/ http://java.dzone.com/articles/glassfish-and-tomcat-whats-the
http://www.jsptut.com/Getfamiliar.jsp
Commercial Deployment
Oracle provides software support only
Sample Run
Project: HelloWeb
HelloWeb: Directories and Files
NameHandler.java
Java Package
Right-click Source Packages
http://en.wikipedia.org/wiki/GlassFish
Java Package
Add a Java Class, specify Package name
Java Package
• a mechanism for organizing Java classes into namespaces
• can be stored in compressed files called JAR files, allowing classes to
download faster as a group rather than one at a time.
http://en.wikipedia.org/wiki/GlassFish
Java Package
Add a Java Class
http://en.wikipedia.org/wiki/GlassFish
Java Package
Edit the Java Class
http://en.wikipedia.org/wiki/GlassFish
Generating Getter and Setter Methods
Right-click name field in the Source editor
expand HTML Forms and drag a Form item to a point after the
<h1> tags in the Source Editor.
Click OK.
Source Generated
Drag a Text Input item to a point just before the </form> tag, then
specify the following values:
• Name: name
• Type: text
Source Generated
Drag a Button item to a point just before the </form> tag. Specify the
following values:
• Label: OK
• Type: submit
Type Enter your name: just before the first <input> tag, then
change the default Hello World! text between the <h1> tags
to Entry Form.
Form
We would like to
pass this to our
server
response.jsp
Adding a JSP File
In the Projects window, right-click the HelloWeb project node and
choose New > JSP.
JSP The New JSP File wizard opens.
Notice that the <jsp:useBean> tag is added beneath the <body> tag.
Adding a Set Bean property item
Drag a Set Bean Property item from the Palette to a point just before
the <h1> tag and click OK.
User input
Response from
the JSP file
Sample Run
NameHandler.java
Class NameHandler
containing user data,
get and set methods
http://java.sun.com/blueprints/code/projectconventions.html
http://java.sun.com/blueprints/code/projectconventions.html
NetBeans
http://netbeans.org/kb/docs/web/quickstart-webapps.html
http://www.oracle.com/technetwork/java/javaee/documentation/index.html
Simple Database Example
http://netbeans.org/kb/docs/web/mysql-webapp.html
E-Commerce Example
http://netbeans.org/kb/docs/javaee/ecommerce/design.html
http://netbeans.org/kb/docs/javaee/ecommerce/data-model.html#createERDiagram
http://dot.netbeans.org:8080/AffableBean/
Model-View-Controller Paradigm