1 / 24

Server-Side Java Programming

Server-Side Java Programming. 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.

jmoua
Télécharger la présentation

Server-Side Java Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. 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.

  11. 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.

  12. 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.

  13. 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.

  14. 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.

  15. 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”.

  16. JSP Code Examples

  17. helloworld.jsp <%@page contentType="text/html“ %> <%@page pageEncoding="UTF-8“ %> <html> <head><title>JSP: Hello World!</title></head> <body> <h1>JSP Says:</h1> <% out.println("Hello World!"); %> </body> </html>

  18. helloform.jsp 1/2: JSP Processing <%@page contentType="text/html“ %> <%@page 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>

  19. 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>

  20. 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

  21. colorbox.jsp 1/2 <%@page contentType="text/html“ %> <%@page pageEncoding="UTF-8“ %> <%@page 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;

  22. 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'>&nbsp;&nbsp;&nbsp;", red, green)); sb.setLength(0); // avoid appending next string } } %> </body></html>

More Related