Download presentation
Presentation is loading. Please wait.
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.
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 <%@page contentType="text/html“ %>
pageEncoding="UTF-8“ %> <html> <head><title>JSP: Hello World!</title></head> <body> <h1>JSP Says:</h1> <% out.println("Hello World!"); %> </body> </html>
20
helloform.jsp 1/2: JSP Processing
contentType="text/html“ %> pageEncoding="UTF-8“ %> <html> <head><title>JSP: Hello Xyz</title></head> <body> <% String user = request.getParameter("user"); if (user == null) user = "Stranger"; %> <h1>Hello <%= user %></h1>
21
helloform.jsp 2/2: HTML Form
… (JSP processing goes here) <form method="POST" action="helloform.jsp"> Your Name: <input type="text" name="user" size="40"> <input type="submit" name="submit" value="OK"> </form> </body> </html>
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 “<tr>” Inner loop prints dynamic HTML
23
colorbox.jsp 1/2 <%@page contentType="text/html“ %>
pageEncoding="UTF-8“ %> import="java.util.Formatter" %> <html> <head><title>JSP: Color Box</title></head> <body> <table border="0" cellspacing="0"> <% StringBuilder sb = new StringBuilder(); Formatter formatter = new Formatter(sb); int red, green;
24
colorbox.jsp 2/2 for (red = 0; red <= 255; red += 17) {
// instead of <tr> below, we can use 'out.println("<tr>")' here %> <tr> <% for(green = 0; green <= 255; green += 17) { out.println(formatter.format( "<td bgcolor='%02x%02xff'> ", red, green)); sb.setLength(0); // avoid appending next string } </body></html>
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.