Web Container Components Servlet JSP Tag Libraries



Similar documents
Controlling Web Application Behavior

JEE Web Applications Jeff Zhuk

An introduction to web programming with Java

ACM Crossroads Student Magazine The ACM's First Electronic Publication

Introduction to J2EE Web Technologies

How To Understand The Architecture Of Java 2Ee, J2Ee, And J2E (Java) In A Wordpress Blog Post

Portals, Portlets & Liferay Platform

Java EE Introduction, Content. Component Architecture: Why and How Java EE: Enterprise Java

Web Applications. For live Java training, please see training courses at

Web Application Architecture (based J2EE 1.4 Tutorial)

Creating Java EE Applications and Servlets with IntelliJ IDEA

CONTROLLING WEB APPLICATION BEHAVIOR WITH

Supplement IV.E: Tutorial for Tomcat. For Introduction to Java Programming By Y. Daniel Liang

Glassfish, JAVA EE, Servlets, JSP, EJB

DTS Web Developers Guide

INTRODUCTION TO WEB TECHNOLOGY

Hello World Portlet Rendered with JSP for WebSphere Portal Version 4.1

2.8. Session management

AN OVERVIEW OF SERVLET AND JSP TECHNOLOGY

Course Name: Course in JSP Course Code: P5

In this chapter, we lay the foundation for all our further discussions. We start

Java Server Pages combined with servlets in action. Generals. Java Servlets

2. Follow the installation directions and install the server on ccc

Agenda. Summary of Previous Session. Application Servers G Session 3 - Main Theme Page-Based Application Servers (Part II)

Java Servlet Tutorial. Java Servlet Tutorial

CHAPTER 9: SERVLET AND JSP FILTERS

Java and Web. WebWork

Database Applications Recitation 10. Project 3: CMUQFlix CMUQ s Movies Recommendation System

Tutorial: Building a Web Application with Struts

Class Focus: Web Applications that provide Dynamic Content

Pure server-side Web Applications with Java, JSP. Application Servers: the Essential Tool of Server-Side Programming. Install and Check Tomcat

Development. with NetBeans 5.0. A Quick Start in Basic Web and Struts Applications. Geertjan Wielenga

JSP Java Server Pages

Web Applications. Originals of Slides and Source Code for Examples:

SSC - Web development Model-View-Controller for Java web application development

SSC - Web applications and development Introduction and Java Servlet (II)

Web Service Development Using CXF. - Praveen Kumar Jayaram

Workshop for WebLogic introduces new tools in support of Java EE 5.0 standards. The support for Java EE5 includes the following technologies:

Server-Side Web Development JSP. Today. Web Servers. Static HTML Directives. Actions Comments Tag Libraries Implicit Objects. Apache.

JBoss Portlet Container. User Guide. Release 2.0

Java Web Programming. Student Workbook

Penetration from application down to OS

Complete Java Web Development

7 Web Databases. Access to Web Databases: Servlets, Applets. Java Server Pages PHP, PEAR. Languages: Java, PHP, Python,...

Java Servlet 3.0. Rajiv Mordani Spec Lead

PowerTier Web Development Tools 4

Design Patterns. Design patterns are known solutions for common problems. Design patterns give us a system of names and ideas for common problems.

Managing Data on the World Wide-Web

Java Servlet and JSP Programming. Structure and Deployment China Jiliang University

An introduction to creating JSF applications in Rational Application Developer Version 8.0

Web Programming II JSP (Java Server Pages) ASP request processing. The Problem. The Problem. Enterprise Application Development using J2EE

Web Development in Java Live Demonstrations (Live demonstrations done using Eclipse for Java EE 4.3 and WildFly 8)

Outline. CS 112 Introduction to Programming. Recap: HTML/CSS/Javascript. Admin. Outline

Case Studies of Running the Platform. NetBeans UML Servlet JSP GlassFish EJB

Servlet 3.0. Alexis Moussine-Pouchkine. mercredi 13 avril 2011

Web Application Programmer's Guide

PA165 - Lab session - Web Presentation Layer

Model-View-Controller. and. Struts 2

Developing Web Applications using JavaServer Pages and Servlets

Principles and Techniques of DBMS 5 Servlet

Web Frameworks and WebWork

JBoss SOAP Web Services User Guide. Version: M5

Building Web Applications, Servlets, JSP and JDBC

Servlets. Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun

NetBeans IDE Field Guide

Arjun V. Bala Page 20

Mastering Tomcat Development

Getting Started with Web Applications

Programming on the Web(CSC309F) Tutorial: Servlets && Tomcat TA:Wael Aboelsaadat

Web Application Developer s Guide

Ch-03 Web Applications

Application Security

Virtual Open-Source Labs for Web Security Education

Services. Custom Tag Libraries. Today. Web Development. Role-Based. Development. Code Reuse. Tag Libraries Custom Tags. Tag Lifecycle.

Web Applications and Struts 2

JSP. Common patterns

Design Approaches of Web Application with Efficient Performance in JAVA

Client-server 3-tier N-tier

Simplify Your Web App Development Using the Spring MVC Framework

In this chapter the concept of Servlets, not the entire Servlet specification, is

Oracle WebLogic Foundation of Oracle Fusion Middleware. Lawrence Manickam Toyork Systems Inc

Enterprise Application Development In Java with AJAX and ORM

MVC pattern in java web programming

Crawl Proxy Installation and Configuration Guide

Exam Prep. Sun Certified Web Component Developer (SCWCD) for J2EE Platform

Configure a SOAScheduler for a composite in SOA Suite 11g. By Robert Baumgartner, Senior Solution Architect ORACLE

Oracle WebLogic Server

WIRIS quizzes web services Getting started with PHP and Java

CrownPeak Java Web Hosting. Version 0.20

Developing Java Web Applications with Jakarta Struts Framework

Web Development on the SOEN 6011 Server

Web Application Development Using Borland JBuilder 8 WebLogic Edition

SSO Plugin. HP Service Request Catalog. J System Solutions. Version 3.6

Customer Bank Account Management System Technical Specification Document

Web Presentation Layer Architecture

Web-JISIS Reference Manual

Transcription:

Web Application Development, Best Practices by Jeff Zhuk, JavaSchool.com ITS, Inc. dean@javaschool.com Web Container Components Servlet JSP Tag Libraries Servlet Standard Java class to handle an HTTP request and response Mixes Java and HTML code JSP Allows some separation of Java and HTML code Using JavaBeans as backend helpers JSP scriplets mix a lot of Java and HTML code Tag libraries Define custom tags to remove Java code from JSP pages

Java Servlet and JSPs Supported by all current application servers, JSPs are automatically translated into Java servlet classes. Java servlets and JSPs are collaborative mechanisms; they can be used in together or in isolation. In JSP, HTML code looks exactly as it would appear in a.html file. The JSP interpreter automatically translates into servlet compatible form. JSPs introduced the notion of tags ; The custom or user-defined tags is also supported by the JSP spec. Java Standard Tag Library, Struts, BEA WebLogic, and more tag library frameworks are available.

Java Servlets serve HTTP The Hypertext Transfer Protocol (HTTP), like HTML was developed by Tim Berners-Lee and CERN in1989 with the main purpose to transfer arbitrary data, and therefore is not restricted to just text, as its name might suggest. HTTP is a stateless, connectionless protocol HTTP 1.1 requests GET, POST, HEAD, PUT, TRACE, DELETE, OPTIONS, CONNECT Although the HTTP 1.1 spec. contains 7 types of requests, many simple HTTP servers only know the commands GET, POST, and HEAD HTML forms have (hidden or visible) input fields and the ACTION field Default request method is GET <FORM ACTION="/directoryName/ServletClassName METHOD= POST > Name: <INPUT TYPE="TEXT" NAME= name"/> Password: <INPUT TYPE= PASSWORD" /> <CENTER><INPUT TYPE= SUBMIT"/>GO!</CENTER> </FORM>

What is Servlet? A servlet extends functionality of a web server similar to an applet in a browser. Servlet is a piece of Java code Servlet is loaded by a Web server to handle client requests Servlet s code stays in memory when the request terminates Servlet can retain its connection across requests Servlet can chain a client request to another Servlet (servlet chaining) Servlets usually: 1) Accept input data 2) Run or delegate business logics 3) Dynamically generate or just transfer HTML response page For example, when a browser sends a request to the web server, the server may forward the request to a servlet that can process the request and construct a response, usually an HTML page, that is returned to the browser.

Servlet Code Planning import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class AbstractServlet extends HttpServlet { public void doget(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException { //Get HTTP headers, cookies, session and form data from the request //Apply business logic, e.g., user authorization //Fill outgoing "response" to specify the HTTP response //including status code, headers, cookies, session data, // send output HTML page back to a client } }

Servlet Code Example import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class HelloServlet extends javax.servlet.http.httpservlet { public void doget( HttpServletRequest request, HttpServletResponse response) throws javax.servlet.servletexception, java.io.ioexception { String html = <HTML>\n" + "<HEAD><TITLE>HTML Title</TITLE></HEAD>\n" + "<BODY>\n" + "<H1> This is just an example</h1>\n" + "</BODY></HTML>"; } } response.getwriter().println(html); ITS, Inc. dean@javaschool.com

Servlet Best Practices New services can be added run time as new JSPs/ASPs or Java /.NET classes //servicename and servicedetails are to be populated // by servlet dopost(), doget() or service() methods String servicename = request.getparameter( service ); Hashtable servicedetails = getservicedetails(); Service service = // known or new service (Service) Class.forName(serviceName).newInstance(); String content = service.run(servicedetails); response.setcontenttype( text/html"); // application/xsl and etc. response.getwriter().println(content); XML based Service API allows us to describe any existing and future service <ServiceRequest service= Mail action= get > <Param><paramName1= ></Param> </ServiceRequest> We can find both Dispatcher and Factory patterns in this example. This approach makes it possible to create a unified API for client server communications. Any service (including new, unknown design time services) can be requested by a client without code change. ITS, Inc. dean@javaschool.com

Servlet s Initialization and web.xml <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app id="webapp"> <display-name>javaschoolapp</display-name> <context-param> <param-name>sqllocation</param-name> <param-value>sql</param-value> <description>location of SQL statements relative to WEB-INF</description> </context-param> <!-- Initialization code is in the ITSServletContextListener.java -- > <listener> <listener-class>com.its.util.itsservletcontextlistener</listener-class> </listener> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.actionservlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>web-inf/struts-config.xml</param-value> </init-param> </servlet> ITS, Inc. dean@javaschool.com

Servlets Summary Servlet-Centric Architecture Web Tier Client(s) Web Browser Http Post/Get Submit Form Request Display Page Response Servlets execute business logic, provide data access, and Create HTML pages Other Enterprise Applications Database Java code in servlets mix together business and presentation layers

Servlet Summary 1. A servlet extends functionality of a web server similar to an applet in a browser. 2. Servlets usually: 1. 2. 3.

Servlet Summary 1. A servlet extends functionality of a web server similar to an applet in a browser. 2. Servlets usually: 1) Accept input data 2) Run or delegate business logics 3) Dynamically generate HTML response page 3. In Servlet-based architecture there is a mix

JSP Concepts JSP, similar to Microsoft ASP Technology, simplifies web design. Web Designers who understand HTML can create most of the page using HTML tags and leave the rest for programmers As compared to ASP, JSP is better language for dynamic part Portable to multiple servers and operating systems As compared to pure servlets, JSP is more convenient to create HTML Can use standard tools (e. g., HomeSite, DreamWeaver MX) Model / view / controller paradigm JSP programmers still need to know servlet programming <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD><TITLE>Simple date with JSP</TITLE></HEAD> <BODY> Today s date is <%= new java.util.date( ) %>. <P> Regular HTML for rest of on-line store s Web page </BODY> </HTML> ITS, Inc. dean@javaschool.com

MVC Model 1: JSP-to-JSP Page-Centric Architecture Web Tier Client(s) Web Browser Http Post/Get Submit Form Request Display Page Response JSPs with Embedded Java Beans to execute business logic and provide data access Other Enterprise Applications Database Interrelated JSP pages provide presentation, control, and business processing with scriplets and embedded Java beans encouraging spaghetti code in JSP. ITS, Inc. dean@javaschool.com

JSP Custom Tag Library What is Tag Library? Standard actions In JSP, actions are elements that can create and access programming language objects and affect output JSP spec 1.1 defines standard actions that can be used to interact with any Bean usebean, setproperty, getproperty, include, forward, param, plugin JSP 1.1 and above supports development of reusable modules called custom actions A custom action is invoked by using a custom tag in a JSP page A tag library is a collection of custom tags

Client(s) Web Browser Http Post/Get MVC Model 2 - Better Separation of Business and Presentation Layers Submit Form Request Display Page Response Web Tier Servlet Controller Web Tier JSPs are forming The View Model Java Beans Service Layer Other Enterprise Applications Database Servlet and JSP work together. Servlet and related classes, like Struts Action, control application logic and interact with services while JSP forms the presentation ITS, Inc. dean@javaschool.com

Custom Tag Library Examples JSTL Struts WebLogic Displaytag Your own custom tag library ITS, Inc. dean@javaschool.com

JSP/Tags Summary/Repetition JSP provides better. Custom Tags.

Web Application Development, Best Practices by Jeff Zhuk, JavaSchool.com ITS, Inc. dean@javaschool.com Web Container Components Servlet JSP Practice Open Dynamic Web Application Project in Eclipse Create JSP - index.jsp Remember what is JSP? Allows some separation of Java and HTML code Using JavaBeans as backend helpers JSP scriplets provide access to Java in HTML Create Servlet train.servletexample Remember what is servlet? Standard Java class to handle an HTTP request and response Often mixes Java and HTML code

MVC Model 1: JSP-to-JSP Page-Centric Architecture Web Tier Client(s) Web Browser Http Post/Get Submit Form Request Display Page Response JSPs with Embedded Java Beans to execute business logic and provide data access Other Enterprise Applications Database Interrelated JSP pages provide presentation, control, and business processing with scriplets and embedded Java beans encouraging spaghetti code in JSP. ITS, Inc. dean@javaschool.com

JSP/Tags Summary/Repetition JSP provides better. Custom Tags. Practice...

Open NEW Dynamic Web Application Project in Eclipse NEW Project - Web Dynamic Web Project Project name: webtrain

Create index.jsp WebContent right mouse click NEW JSP file index.jsp <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD><TITLE>Simple date with JSP</TITLE></HEAD> <BODY> Today s date is <%= new java.util.date( ) %>. <P> Regular HTML for rest of on-line store s Web page </BODY> </HTML> ITS, Inc. dean@javaschool.com

Deploy Your Application Find the webtrain directory in the Eclipse workspace Copy the WebContent directory to c:/uop/apps Zip the content of the folder (without the folder itself) and rename the compressed file into yourname.war Email this file to me and I will Copy the file yourname.war to the deployment directory, for example, Tomcat/Webapps Point the Browser to the following URLs to test your application: http://yourserver:8080/yourname/index.jsp

Servlet Code Planning import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class AbstractServlet extends HttpServlet { public void doget(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException { //Get HTTP headers, cookies, session and form data from the request //Apply business logic, e.g., user authorization //Fill outgoing "response" to specify the HTTP response //including status code, headers, cookies, session data, // send output HTML page back to a client } }

Prepare Eclipse to Servlets 1.Provide the Servlet Library Place servlet-api.jar in the Eclipse - WEB-INF/lib Right mouse click on the jar Add to Build Path 2. Create the train package under the src and create the ServletExample class in this package

Servlet s Initialization and web.xml <?xml version="1.0" encoding="utf-8"?>... Keep existing code generated by Ecllipse... and add after the <web-app... <web-app id="webapp"> <display-name>yourname</display-name> <context-param> <param-name>dsname</param-name> <param-value>jeffcotrain</param-value> </context-param> <servlet> <servlet-name>action</servlet-name> <servlet-class>train.servletexample</servlet-class> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <session-config> <session-timeout>30</session-timeout> </session-config> </web-app> ITS, Inc. dean@javaschool.com

Servlets Summary Servlet-Centric Architecture Web Tier Client(s) Web Browser Http Post/Get Submit Form Request Display Page Response Servlets execute business logic, provide data access, and Create HTML pages Other Enterprise Applications Database Java code in servlets mix together business and presentation layers

Servlet Summary 1. A servlet extends functionality of a web server similar to an applet in a browser. 2. Servlets usually: 1. 2. 3.

Servlet Summary 1. A servlet extends functionality of a web server similar to an applet in a browser. 2. Servlets usually: 1) Accept input data 2) Run or delegate business logics 3) Dynamically generate HTML response page 3. In Servlet-based architecture there is a mix

Deploy Your Application Make sure that your code was compiled with no read spots Find the webtrain directory in the Eclipse workspace Copy build/classes into WebContent/WEB-INFO Copy the WebContent directory to c:/uop/apps Zip the content of the folder (without the folder itself) and rename the compressed file into yourname.war Deploy the file yourname.war to the deployment directory, for example, Tomcat/Webapps Point the Browser to the following URLs to test your application: http://yourserver:8080/yourname/index.jsp http://yourserver:8080/yourname/servletexample.do ITS, Inc. dean@javaschool.com