Server-Side Java Programming - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Server-Side Java Programming

Description:

We would like to show you a description here but the site won t allow us. – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 25
Provided by: Kan80
Category:

less

Transcript and Presenter's Notes

Title: Server-Side Java Programming


1
Server-Side Java Programming
2
Java
  • Java started originally as OAK, for remote
    control of peripherals.
  • Applets, small portable web-page-embeddable
    programs made Java look appealing at first.
  • Today, applets are not very significant to Java's
    popularity or success.
  • Server-side Java has been most important driving
    force behind Java's direction.

3
Java Language
  • Java was designed by James Gosling from scratch
    to be a language that is
  • Highly object-oriented
  • Garbage-collected
  • Multithreaded to the core
  • Secure/safe/managed
  • Dynamic and reflective
  • Interpreted (today, Just-In-Time compiled)?
  • The approach can be summarized as first, do it
    right, then, improve efficiency.

4
Packages and Classes
  • Directory structure parallels package hierarchy.
  • One .java file contains one public Java class by
    the same name.
  • org/apache/logging/Main.java
  • package org.apache.logging
  • public class Main ...
  • Other non-public classes, nested classes and
    anonymous classes may also be defined in that
    same file.
  • Java compiles to intermediate bytecode each
    class creates one .class file (may be multiple
    per .java file).

5
Bytecode
  • Unlike platform-specific executables, same
    bytecode can run on any platform that supports
    Java.
  • Bytecode is often smaller than source code (which
    is often much smaller than the corresponding
    compiled executable file). This speeds up Java
    code download on the network.
  • All external class references, methods,
    externally visible internal methods and fields
    will have names recorded in bytecode.

6
Symbols in Bytecode
  • Bytecode contains many symbols.
  • This allows classes with known interfaces to be
    dynamically loaded and used with ease.
  • Often, reverse engineering is quite possible.
  • Obfuscators can be used to make reverse
    engineering harder, but they require all classes
    to be gathered and obfuscated together (because
    public APIs will be jumbled up).
  • This eliminates the ability to dynamically load
    and use classes.

7
Standard Libraries
  • A very large standard library exists, is
    well-documented, and can be assumed available
    anywhere Java is available.
  • This helps reduce application code size.
  • Java motto is Write once, run anywhere.
  • You should quickly learn to read and navigate the
    API documentation.
  • We will use both Java SDK and Java EE libraries
    and APIs.

8
Java Syntax
  • There are no structs or properties in Java.
  • Objects are always passed-by-reference.
  • Strings and arrays are special types of objects
    other objects can not do operator overloading (as
    in C).
  • Generics and assertions were added later without
    changing the bytecode.
  • Annotations were added later and they changed
    Java significantly.

9
Server-Side Java
  • Lighter-weight three-tier Java Web Applications
    use Servlets and JSPs (JavaServer Pages).
  • Heavier-weight four-tier Java Enterprise
    Applications use EJBs.
  • In Java EE 5, annotation-based entities that
    contain database-persisted data can be used by
    both of these types of applications.
  • Web services and application client containers
    are also supported.

10
(No Transcript)
11
(No Transcript)
12
JSP
  • JSP is much like PHP.
  • It allows Java code to be embedded within HTML.
  • Java code runs on the server side and produces
    HTML.
  • Client only sees the HTML.
  • JSPs are compiled automatically to Servlets when
    a page is requested by a client.

13
JSTL
  • JSP pages used to mix presentation with business
    logic.
  • To separate code from the design, JSTL (JSP Tag
    Library) was created.
  • JSTL tags look like HTML tags, but are backed by
    Java code for each tag.
  • This allows JSP pages to stay smaller and use
    less Java code, making accidental change of code
    by designers less likely.

14
Servlets
  • Servlets and JavaBeans are often used to have
    persistent logic, and page flow control.
  • JSPs are best used for presentation (more HTML),
    Servlets can be used for business logic (more
    Java code).
  • Client sessions can be created and used in both
    Servlets and JSPs.

15
EJBs and Entities
  • In four-tier enterprise applications, EJBs
    usually handle the business logic.
  • In this case, JSPs and Servlets are used for
    presentation issues.
  • In Java EE 5, entity beans are now replaced
    with entity objects, which are part of the new
    JPA (Java Persistence API).
  • Entity objects are automatically saved and
    retrieved from database.
  • Enterprise Application Container handles EJB
    issues and persistence.

16
Types of EJBs
  • Stateless Session beans don't hold any client
    state. They are often pooled and shared amongst
    clients.
  • Stateful Session beans hold client state. They
    are created per-client.
  • Message-Driven beans allow asynchronous
    messaging caller does not wait for a reply.

17
Enterprise Application Server
  • We will use Sun JSAS PE 9.0 (Sun Java System
    Application Server Platform Edition 9.0).
  • Sun JSAS is based on Glassfish open-source
    application server.
  • Java EE 5 greatly simplifies EJB development.
  • This is mostly due to object-relational mapping
    that automates object persistence by using
    annotations.
  • The standardized mapping was created by Oracle,
    in its open-source product Toplink Essentials.

18
JSP Code Examples
19
helloworld.jsp
  • lt_at_page contentType"text/html gt
  • lt_at_page pageEncoding"UTF-8 gt
  • lthtmlgt
  • ltheadgtlttitlegtJSP Hello World!lt/titlegtlt/headgt
  • ltbodygt
  • lth1gtJSP Sayslt/h1gt
  • lt
  • out.println("Hello World!")
  • gt
  • lt/bodygt
  • lt/htmlgt

20
helloform.jsp 1/2 JSP Processing
  • lt_at_page contentType"text/html gt
  • lt_at_page pageEncoding"UTF-8 gt
  • lthtmlgt
  • ltheadgtlttitlegtJSP Hello Xyzlt/titlegtlt/headgt
  • ltbodygt
  • lt
  • String user request.getParameter("user")
  • if (user null)
  • user "Stranger"
  • gt
  • lth1gtHello lt user gtlt/h1gt

21
helloform.jsp 2/2 HTML Form
  • (JSP processing goes here)
  • ltform method"POST" action"helloform.jsp"gt
  • Your Name
  • ltinput type"text" name"user" size"40"gt
  • ltinput type"submit" name"submit" value"OK"gt
  • lt/formgt
  • lt/bodygt
  • lt/htmlgt

22
colorbox.jsp Structure
  • Colorbox uses nested for loops to create a
    colorful table.
  • colorbox.jsp structure
  • Page directives at the top
  • HTML on the outside
  • Initialize formatter to print hexadecimal later.
  • Outer for loop for red color
  • Nested HTML lttrgt
  • Inner loop prints dynamic HTML

23
colorbox.jsp 1/2
  • lt_at_page contentType"text/html gt
  • lt_at_page pageEncoding"UTF-8 gt
  • lt_at_page import"java.util.Formatter" gt
  • lthtmlgt
  • ltheadgtlttitlegtJSP Color Boxlt/titlegtlt/headgt
  • ltbodygt
  • lttable border"0" cellspacing"0"gt
  • lt
  • StringBuilder sb new StringBuilder()
  • Formatter formatter new Formatter(sb)
  • int red, green

24
colorbox.jsp 2/2
  • for (red 0 red lt 255 red 17)
  • // instead of lttrgt below, we can use
    'out.println("lttrgt")' here
  • gt
  • lttrgt
  • lt
  • for(green 0 green lt 255 green 17)
  • out.println(formatter.format(
  • "lttd bgcolor'02x02xff'gtnbspnbspnbsp
    ",
  • red, green))
  • sb.setLength(0) // avoid appending next
    string
  • gt
  • lt/bodygtlt/htmlgt
Write a Comment
User Comments (0)
About PowerShow.com