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



Similar documents
Creating Java EE Applications and Servlets with IntelliJ IDEA

Web Development in Java Part I

Developing an EJB3 Application. on WebSphere 6.1. using RAD 7.5

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

Web Anwendungen Entwickeln mit JSF, Spring und Tomcat

Web Container Components Servlet JSP Tag Libraries

Java EE 6 Development with NetBeans 7

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

Managed Beans II Advanced Features

Controlling Web Application Behavior

Getting Started Guide. Version: 4.0.0

An introduction to web programming with Java

Java EE 6 New features in practice Part 3

Introduction to J2EE Web Technologies

Managing Data on the World Wide-Web

Java Servlet 3.0. Rajiv Mordani Spec Lead

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

This presentation will provide a brief introduction to Rational Application Developer V7.5.

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

Recommended JSF Enhancements

Principles and Techniques of DBMS 5 Servlet

INTRODUCTION TO WEB TECHNOLOGY

Supporting Multi-tenancy Applications with Java EE

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

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

WebSphere and Message Driven Beans

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

ACM Crossroads Student Magazine The ACM's First Electronic Publication

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

Java Web Programming. Student Workbook

Hello World RESTful web service tutorial

Glassfish, JAVA EE, Servlets, JSP, EJB

Hello World Portlet Rendered with JSP for WebSphere Portal Version 4.1

PA165 - Lab session - Web Presentation Layer

J2EE-Application Server

JBoss SOAP Web Services User Guide. Version: M5

Implementing the Shop with EJB

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

JSP Java Server Pages

Agenda. Web Development in Java. More information. Warning

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

Building and Using Web Services With JDeveloper 11g

JBoss Portlet Container. User Guide. Release 2.0

WA 2. GWT Martin Klíma

Web Application Programmer's Guide

Handling the Client Request: Form Data

Java Servlet Tutorial. Java Servlet Tutorial

ShoreTel Enterprise Contact Center 8 Installing and Implementing Chat

CREATE A CUSTOM THEME WEBSPHERE PORTAL

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

Java EE 6 development with Eclipse, Netbeans, IntelliJ and GlassFish. Ludovic Champenois Oracle Corporation

Tutorial on Building a web Application with Jdeveloper using EJB, JPA and Java Server Faces By Phaninder Surapaneni

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

Developing Web Applications using JavaServer Pages and Servlets

JAVA PROGRAMMING PATTERN FOR THE PREFERENCES USABILITY MECHANISM

Getting Started Guide. Version 1.8

PowerTier Web Development Tools 4

Piotr Nowicki's Homepage. Java EE 6 SCWCD Mock Exam. "Simplicity is the ultimate sophistication." Important!

Google Web Toolkit. Progetto di Applicazioni Software a.a. 2011/12. Massimo Mecella

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

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

Introduction to Web Technologies

Agenda. 1. ZAPms Konzept. 2. Benutzer-Kontroller. 3. Laout-Aufbau. 4. Template-Aufbau. 6. Konfiguration. 7. Module.

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

Servers, Dynamic Web Projects, and Servlets

.NET Best Practices Part 1 Master Pages Setup. Version 2.0

Enterprise Java Web Application Frameworks & Sample Stack Implementation

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

CHAPTER 9: SERVLET AND JSP FILTERS

Virtual Open-Source Labs for Web Security Education

Java EE 6 Ce qui vous attends

CONFIGURING A WEB SERVER AND TESTING WEBSPEED

Athena Framework Java Developer's Guide

Wicket Application Development

JSF Melih Sakarya. Java Server Faces PrimeFaces

Chapter 1 Introduction to web development and PHP

NetBeans IDE Field Guide

DTS Web Developers Guide

Aspects of using Hibernate with CaptainCasa Enterprise Client

Web Application Architecture (based J2EE 1.4 Tutorial)

Application Security

Catalog Web service and catalog commerce management center customization

PRIMEFACES MOBILE USER S GUIDE

CONTROLLING WEB APPLICATION BEHAVIOR WITH

White Paper. JavaServer Faces, Graphical Components from Theory to Practice

Struts 2 - Practical examples

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

JavaServer Faces 2.0. Bernd Bohmann. Matthias Weßendorf. Agenda. Agenda. IRIAN Deutschland Apache MyFaces Tobago CORE. Mehr als nur ein Web-Framework

Getting Started with JBoss Developer Studio. ISBN: Publication date: April 2008

Building Java Servlets with Oracle JDeveloper

Java Platform, Enterprise Edition (Java EE) From Yes-M Systems LLC Length: Approx 3 weeks/30 hours Audience: Students with experience in Java SE

Software Architecture

Introduction to web development using XHTML and CSS. Lars Larsson. Today. Course introduction and information XHTML. CSS crash course.

Transcription:

Web Development in Java Live Demonstrations (Live demonstrations done using Eclipse for Java EE 4.3 and WildFly 8) Java Servlets: 1. Switch to the Java EE Perspective (if not there already); 2. File > New > Dynamic Web Project; 3. Follow the wizard, give the project a name and choose to generate web.xml; 4. Go to Java Resources/src, right-click > New > Class; 5. Create the livedemo.helloservlet class, extending HttpServlet: public class HelloServlet extends HttpServlet { @Override protected void doget(httpservletrequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setcontenttype("text/html"); try (PrintWriter out = resp.getwriter()) { out.write("<html><head><title>hello, Servlet!</title></head><body>"); out.write("<h1>hello, World!</h1>"); out.write("<p>the time now is " + new Date() + "</p>"); out.write("</body></html>"); 6. Edit web.xml: <servlet> <servlet-name>helloservlet</servlet-name> <servlet-class>livedemo.helloservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>helloservlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> 7. Right-click the project > Run As > Run on Server; 8. Open http://localhost:8080/livedemo/hello Completing the WebApp: 1. Right-click WebContent > New > HTML File > index.html ; 2. Body of the page: <form action="hello" method="get"> <p>please tell me your name: <input type="text" name="visitor" size="20" /></p> <p><input type="submit" value="enter" /></p> </form> 3. Change the Servlet: out.write("<h1>hello, " + req.getparameter("visitor") + "!</h1>");

GET and POST requests: 1. Change method= get to method= post in index.html; 2. Try it, see that it doesn t work; 3. Implement the dopost() method in the servlet, calling doget(). Mention common pattern of both methods calling a third one, doservice() or something; 4. Try again, showing that the query string is gone. Annotations for URL mapping (Servlets 3): 1. Delete the Servlet mapping from web.xml; 2. Add annotation to the Servlet: @WebServlet(name = "HelloServlet", urlpatterns = {"/hello") JavaServer Pages: 1. Right-click WebContent > New > JSP File, File name: hello.jsp, Finish; <%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>hello, JSP!</title> </head> <body> <% String visitor = request.getparameter("visitor"); visitor = ((visitor == null) (visitor.length() == 0))? "visitor" : visitor; %> <h1>hello, <%= visitor %>!</h1> <p>the time now is <%= new java.util.date() %></p> </body> </html> 2. Note the three types of JSP tags (<%@ @>, <% %> and <%= %>); 3. Change action= hello to action= hello.jsp in index.html and test it. JavaServer Faces: 1. Right-click the project > Properties > Project Facets; 2. Select JavaServer Faces and configure it to *.faces; 3. Open web.xml and show the JSF configuration; 4. Also show there s a faces-config.xml, but it s empty for now;

5. Right-click WebContent > New > HTML File > index.html > Next; 6. Select New XHTML File (1.0 strict) > Finish; <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> <f:view contenttype="text/html"> <h:head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>hello, JSF!</title> </h:head> <h:body> <h1>hello, <h:outputtext value="#{hellocontroller.visitor" />!</h1> <p>the time now is <h:outputtext value="#{hellocontroller.time" /></p> <h:form id="helloform"> <p>please tell me your name: <h:inputtext value="#{hellocontroller.visitor" /></p> <p><h:commandbutton value="enter" /></p> </h:form> </h:body> </f:view> </html> 7. Create a new class for the controller: package livedemo; import java.util.date; import javax.faces.bean.managedbean; @ManagedBean public class HelloController { private String visitor = "visitor"; public String getvisitor() { return visitor; public void setvisitor(string visitor) { this.visitor = visitor; public Date gettime() { return new Date(); 8. Try it, then back to Eclipse to AJAXify it: <h:form id="helloform"> <h:panelgroup id="hellopanel"> <h1>hello, <h:outputtext value="#{hellocontroller.visitor" />!</h1> <p>the time now is <h:outputtext value="#{hellocontroller.time" /></p> </h:panelgroup> <p>please tell me your name: <h:inputtext value="#{hellocontroller.visitor"> <f:ajax event="blur" render="hellopanel" execute="@this" /> </h:inputtext> </p> </h:form> </h:body> 9. Note that he panel must be inside the form, or else JSF won t find it; 10. Run Sigme (https://github.com/feees/sigme) and show some screens.

Java Hostel Create the project: 1. File > New > Dynamic Web Project:, Project name: JavaHostel, Target runtime: WildFly 8.0 Runtime, Next > Next; 2. Select Generate web.xml deployment descriptor > Finish. Add facets: 1. Right-click JavaHostel > Properties > Project Facets. Select: a. JavaServer Faces; b. JPA; 2. Open JavaHostel/JPA Content/persistence.xml. Inside <persistence-unit> Try to use the graphical editor: a. General / Persistence provider: org.hibernate.ejb.hibernatepersistence; b. Connection / JTA data source: java:/jboss/datasources/javahostel; c. Properties / Add... / Name: hibernate.hbm2ddl.auto / Value: create; 3. XML result should be: <provider>org.hibernate.ejb.hibernatepersistence</provider> <jta-data-source>java:/jboss/datasources/javahostel</jta-data-source> <properties> <property name="hibernate.hbm2ddl.auto" value="create" /> </properties> Apply web template as decorator: 1. Download the template from http://www.templatemo.com/preview/templatemo_104_hotel; 2. Create new folder: JavaHostel/WebContent/resources; 3. Copy the template's templatemo_style.css file and the images directory there; 4. Right-click JavaHostel/WebContent/resources > New > Other... > Web > HTML File > Next; 5. File name: decorator.xhtml > Next; 6. Templates: select New XHTML File (1.0 transitional) > Finish; 7. Change decorator.xhtml. On <html> add: xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" 8. Replace <body> with <h:body>, <head> with <h:head> 9. Inside <h:head> add:

<link href="#{facescontext.externalcontext.requestcontextpath/resources/templatemo_s tyle.css" rel="stylesheet" type="text/css" /> 10. Inside <title> add: <h:outputtext value="javahostel :: " /><ui:insert name="title" /> 11. Copy from template's index.html everything in <body>, with the following changes: <div id="title">javahostel</div> <div id="salgon">java EE 6 Example WebApp</div> 12. And <div id="templatemo_menu"> <ul> <li><a href="#{facescontext.externalcontext.requestcontextpath/">home</a></li> <li><a href="#">link 01</a></li> <li><a href="#">link 02</a></li> <li><a href="#">link 03</a></li> <li><a href="#">link 04</a></li> <li><a href="#" class="last">link 05</a></li> </ul> </div> <div class="content_title_01">left Column</div> 13. Delete everything in the left column until <div class="cleaner"> </div>; 14. Delete everything from <div class="cleaner_horizontal_divider_01"> </div> until </div> <!-- end of content left -->; 15. In <div class="content_right_section">, delete all contents and add: <ui:insert name="contents">blank page.</ui:insert> 16. Delete everything from <div class="cleaner_h40"> </div> until </div> <!-- end of content right -->; 17. Finally, delete the script tags at the end of the page s body; 18. Right-click JavaHostel/WebContent > New > HTML File, File name: index.xhtml, Template: New XHTML File (1.0 transitional); 19. Change the file, deleting the entire <html> element (all the way until </html>). Replace it with: <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" template="/resources/decorator.xhtml"> <ui:define name="title">welcome</ui:define> <ui:define name="contents"> <h1>welcome to JavaHostel</h1> <p>under development.</p> </ui:define> </ui:composition>

20. Right-click JavaHostel/WebContent > New > File, File name: index.html. Contents of this file: <html> <head><meta http-equiv="refresh" content="0;url=index.faces"></head> </html> 21. Open JavaHostel/WebContent/WEB-INF/web.xml. Edit welcome-file-list to include index.html; 22. Deploy and test. Implement domain classes: 1. Create package br.ufes.inf.nemo.javahostel.domain; 2. Create classes: Guest, Booking, Bed, Room; 3. Add class attributes and JPA mappings. For all classes, add class annotation @Entity and ID attribute: @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; 4. For Guest: private String name; private String email; private String password; @Temporal(TemporalType.DATE) private Date birthdate; 5. For Booking: @ManyToOne private Guest guest; @OneToMany private Set<Bed> beds; @Temporal(TemporalType.DATE) private Date startdate; @Temporal(TemporalType.DATE) private Date enddate; 6. For Bed: @ManyToOne private Room room; private int number; private double pricepernight; 7. For Room: private int number; @OneToMany(cascade = CascadeType.ALL, mappedby = "room") private Set<Bed> beds; 8. Generate getters and setters for all attributes.

Automatic DB schema creation: 1. Check again that in JavaHostel/JPA Content/persistence.xml the hibernate.hbm2ddl.auto property has value create ; 2. Deploy/restart the application, go to MySQL Workbench (admin GUI) and check that tables were created in javahostel DB; 3. Back to JavaHostel/JPA Content/persistence.xml, change the property to update. Guest registration: 1. Open JavaHostel/WebContent/resources/decorator.xhtml, replace Link01: <li><a href="#{facescontext.externalcontext.requestcontextpath/registration/index.fac es ">Registration</a></li> 2. Create folder JavaHostel/WebContent/registration; 3. Copy: JavaHostel/WebContent/index.xhtml to this new folder; 4. Change the title to Registration and use the following contents: <h1>registration</h1> <p>fill in your data to become a guest at Java Hostel.</p> <h:form id="regform"> <p>name: <h:inputtext id="name" value="#{registrationcontroller.guest.name" size="30" /></p> <p>birthdate: <h:inputtext id="birthdate" value="#{registrationcontroller.guest.birthdate" size="10"> <f:convertdatetime pattern="dd/mm/yyyy" /> </h:inputtext></p> <p>e-mail: <h:inputtext id="email" value="#{registrationcontroller.guest.email" size="20" /></p> <p>password: <h:inputsecret id="password" value="#{registrationcontroller.guest.password" size="20" /></p> <p><h:commandbutton action="#{registrationcontroller.register" value="register" /></p> </h:form> 5. Implement the controller class in package br.ufes.inf.nemo.javahostel.control: @Named @SessionScoped public class RegistrationController implements Serializable { @EJB private RegistrationService registrationservice; private Guest guest = new Guest(); private int age; public Guest getguest() { return guest; public int getage() { return age; public String register() { try { registrationservice.register(guest); catch (UnderAgeGuestException e) {

age = e.getage(); return "/registration/underage.xhtml"; return "/registration/success.xhtml"; 6. Quick fix "RegistrationService cannot be resolved to a type" to create the class, in package br.ufes.inf.nemo.javahostel.application (note: for simplicity, JavaHostel is a 2-tier application. The persistence code is in the application layer): @Stateless @LocalBean public class RegistrationService implements Serializable { @PersistenceContext private EntityManager entitymanager; public void register(guest guest) throws UnderAgeGuestException { int age = calculateage(guest.getbirthdate()); if (age < 18) throw new UnderAgeGuestException(age); entitymanager.persist(guest); private static int calculateage(date birthdate) { if (birthdate == null) return 0; Calendar birth = Calendar.getInstance(); birth.settime(birthdate); Calendar today = Calendar.getInstance(); today.settime(new Date(System.currentTimeMillis())); int age = today.get(calendar.year) - birth.get(calendar.year); birth.add(calendar.year, age); if (birth.after(today)) age--; return age; 7. Same thing for UnderAgeGuestException (also in application): public class UnderAgeGuestException extends Exception { private int age; public UnderAgeGuestException(int age) { this. age = age; public int getage() { return age; 8. Finally, the result pages. First, JavaHostel/WebContent/registration/underage.xhtml (as before, copy from index.xhtml): <p>dear <h:outputtext value="#{registrationcontroller.guest.name" />, unfortunately underage people are not allowed to register as guests and, according to your birth date, you have only <h:outputtext value="#{registrationcontroller.age" /> years.</p> 9. Then, JavaHostel/WebContent/registration/success.xhtml : <p>dear <h:outputtext value="#{registrationcontroller.guest.name" />, welcome to JavaHostel.</p> 10. Redeploy and test both cases, show that the guest data ends up in the database.