CGS – 4854 Summer 2012 Web Site Construction and Management Instructor: Francisco R. Ortega Chapter 2.

Slides:



Advertisements
Similar presentations
4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
Advertisements

Java Server Pages (JSP)
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
Introduction to Servlets Based on: Hall, Brown, Core Servlets and JavaServer Pages.
J.Sant Servlets Joseph Sant Sheridan Institute of Technology.
An introduction to Java Servlet Programming
J2EE Servlets and JSP Advanced topics Presented by Bartosz Sakowicz.
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
JSP Java Server Pages Reference:
27-Jun-15 Directories and DDs. 2 Web apps A web application is basically a web site that: “Knows who you are”--it doesn’t just give you static pages,
Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET.
A Servlet’s Job Read explicit data sent by client (form data) Read implicit data sent by client (request headers) Generate the results Send the explicit.
Java Servlets and JSP.
Java Servlets. What Are Servlets? Basically, a java program that runs on the server Basically, a java program that runs on the server Creates dynamic.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Struts 2.0 an Overview ( )
JSP and Servlets Lecture notes by Theodoros Anagnostopoulos.
CGS – 4854 Summer 2012 Web Site Construction and Management Instructor: Francisco R. Ortega Date: May 5 th,2012.
Servlets Pranav Maydeo. What is a Servlet ? Servlets are modules of Java code that run in a server application to answer client requests. Servlets are.
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
AN OVERVIEW OF SERVLET TECHNOLOGY SERVER SETUP AND CONFIGURATION WEB APPLICATION STRUCTURE BASIC SERVLET EXAMPLE Java Servlets - Compiled By Nitin Pai.
Objectives Java Servlet Web Components
JSP Java Server Pages Softsmith Infotech.
CSC 2720 Building Web Applications
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
SKT-SSU IT Training Center Servlet and JSP. Chapter Three: Servlet Basics.
111 Java Servlets Dynamic Web Pages (Program Files) Servlets versus Java Server Pages Implementing Servlets Example: F15 Warranty Registration Tomcat Configuration.
JSP Architecture Outline  Model 1 Architecture  Model 2 Architecture.
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time.
CGS – 4854 Summer 2012 Web Site Construction and Management Instructor: Francisco R. Ortega Chapter 3 Part 1.
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
Java Servlets. Servlets When we run small Java programs within a browser these are referred to as Applets... And when we run small Java programs within.
Mark Dixon 1 09 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
Java Servlets & Java Server Pages Lecture July 2013.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
CGS – 4854 Summer 2012 Web Site Construction and Management Instructor: Francisco R. Ortega Chapter 3 Part 1.
Topic Java EE installation (Eclipse, glassfish, etc.) Eclipse configuration for EE Creating a Java Web Dynamic Project Creating your first servlet.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
S ERVLETS Hits Counter 21-Nov-15. S ERVLETS - H ITS C OUNTER Many times you would be interested in knowing total number of hits on a particular page of.
CSC 2720 Building Web Applications Frameworks for Building Web Applications.
A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, Responds oriented other.
Mark Dixon 1 11 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
©SoftMoore ConsultingSlide 1 Filters. Filters can be used in a web application to intercept, examine, and possibly transform requests or responses associated.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
Configuration Web Server Tomcat - Install JDK Install Tom cat Configure Tom cat for running Servlet C:\Program Files\Apache Software Foundation\Tomcat.
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
Introduction To HTML Dr. Magdi AMER. HTML elements.
 Java Server Pages (JSP) By Offir Golan. What is JSP?  A technology that allows for the creation of dynamically generated web pages based on HTML, XML,
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
S ERVLETS Form Data 19-Mar-16. F ORM P ROCESSING You must have come across many situations when you need to pass some information from your browser to.
1 Web Programming with Servlets & JSPs WEB APPLICATIONS – AN OVERVIEW.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
CS3220 Web and Internet Programming Introduction to Java Servlets
Java Servlets By: Tejashri Udavant..
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Chapter 26 Servlets.
Servlets and JSP 20-Nov-18 servletsJSP.ppt.
In Class Assg 2 - solution
COP 4610L: Applications in the Enterprise Spring 2005
© 2014, Mike Murach & Associates, Inc.
Java Servlets and JSP.
CS3220 Web and Internet Programming Introduction to Java Servlets
Introduction to Java Servlets
Directories and DDs 25-Apr-19.
Directories and DDs 21-Jul-19.
Directories and DDs 14-Sep-19.
Presentation transcript:

CGS – 4854 Summer 2012 Web Site Construction and Management Instructor: Francisco R. Ortega Chapter 2

Today’s Lecture – 05/16/12 Some Java questions to do at home. Chapter 2 Remember I have office hours Today!

Java Questions Write logic for a controller using JSP that calls confirm.jsp if: – Button was pressed and amount of total users currently requesting the page is less that Hint USE static variable to add or remove users. – Otherwise, send to Busy.jsp

Java Questions Write logic for a controller using JSP that calls sold.jsp if – Button buy was pressed and textbox quantity is greater than 1. – Check that value of textbox is present before asking if > 1

Java Questions Write a controller using a servlet that checks to see if edit,confirm or cancel was pressed – If none pressed, send them to BadQuery.jsp – If any of them pressed send them to their page. – Check that the button was pressed and that value is not an empty string.

Java How do you open a PrintWriter and write something to it in Java? How do you read a file using a scanner? Write the code that will do the following: if a number is less than 0, display 'negative'; if a number is greater than 100, display 'over 100'; for all other numbers, display 'valid grade'. How do you test if a string is empty as opposed to not existing?

Java How do you define a variable for a class named ShoppingCart? How do you create a new object for a class named ShoppingCart? – Assume it takes CartList as parameter

Java What is an abstract class? What is an interface ? Create some classes (and extend them) to show: – Abstract Classes – Use of interface Create a class person and then student. – How do you call the base constructor of person?

File Structure Tutorials/WEB-INF/ classes/*.class lib/*.jar web.xmlindex.jsp edit.jsp confirm.jsp

Web Applications Web application can have many properties in common For the rest of the chapter, we will see – Edit page – Confirm page – Process page For a more smooth operation – Controller class will be needed A servlet page

Sending Data

Sending Data Test <input type=“text" name="hobby" value="${param.hobby}">

Relative and Absolute Reference Same as we sow in HTML Questions – – – What is the absolute and relative reference for Confirm.jsp if called from Edit.jsp ? What is the absolute and relative reference for validate.jsp if called from Confirm.jsp ?

Retrieving Values (Form Element) ${param.field} For example – ${param.hobby} Try Edit.jsp from book (page 38)

Hidden Field Technique Edit.jsp sends data to Confirm.jsp – How to send data back to Edit.jsp? Hidden Fields –

Hidden Fields! Let’s Compare Edit – Confirm –

More in Sending Data! How do we send data to either edit.jsp or process.jsp?

Inefficient Solution <input type="hidden" name="hobby" value="${param.hobby}"> <input type="submit" name="editButton" value="Edit"> <input type="hidden" name="hobby" value="${param.hobby}"> <input type="submit" name="processButton" value="Process">

Better Solution: Controller Using a controller is better solution

Example: Confirm.jsp <input type="hidden" name="hobby" value="${param.hobby}"> <input type="submit" name="editButton" value="Edit"> <input type="submit" name="processButton" value="Process">

Controller It only contains java code Each JSP will send its data to the controller First, it will be shown as a JSP (for simplicity) Objects: – HttpServletRequest – HttpServletResponse Reference Parameters: – Request.getParameter(“hobby”)

Testing Button Button clicked will show in the query string If (request.getParameter(“processButton”) != null)

Controller Logic 1.Each form’s actions is mapped to controller 2.Each submit button has unique name 3.Controller decides which page to call next 4.Controller knows URL of all JSP that controls

Controller Logic if (request.getParameter("processButton") != null) { address = "Process.jsp"; } else if (request.getParameter("confirmButton") != null) { address = "Confirm.jsp"; } else { address = "Edit.jsp"; }

Forward Control! RequestDispatcher dispatcher = request.getRequestDispatcher(address ); dispatcher.forward(request,response)

JSP or Servlet? You can use either one Easier to read in JSP Better performance in Servlet See Controller Code page 50 (or next slide)

Controller Code JSP <% String address; if (request.getParameter("processButton") != null) { address = "Process.jsp"; } else if (request.getParameter("confirmButton") != null) { address = "Confirm.jsp"; } else { address = "Edit.jsp"; } RequestDispatcher dispatcher = request.getRequestDispatcher(address); dispatcher.forward(request, response); %>

Confirm + Controller <input type="hidden" name="hobby" value="${param.hobby}"> <input type="submit" name="editButton" value="Edit"> <input type="submit" name="processButton" value="Process">

JSP vs Java Servlet JSP It will be recreated each time Prefer when lots of HTML and little JSP Easy to write HTML Hard to debug Java Servlet Does not have to recreate Prefer when lots Java and little HTML Java IDE very useful

More JSP/Servlet If you have lots of HTML and JAVA – Redesign your site: Write more code in the controller

JAVA CLASSPATH Windows – setx CLASSPATH=classpath1;classpath2... Linux/Unix/MacOsX – setenv CLASSPATH classpath1:classpath2...

Classpath + Packages ackages.html ackages.html ow-classpath-work-in-java.html ow-classpath-work-in-java.html oldocs/solaris/classpath.html oldocs/solaris/classpath.html oldocs/solaris/classpath.html oldocs/solaris/classpath.html

More about Packages If your class directory is called “classes” – What is the name of the package for classes/store? Store – How about classes/store/hardware ? store.hardware

Controller Servlet Place servlet in a package – Location should not be in the default package Import the following classes: – import java.io.IOException; – import javax.servlet.ServletException; – import javax.servlet.http.HttpServlet; – import javax.servlet.http.HttpServletRequest; – import javax.servlet.http.HttpServletResponse; – import javax.servlet.RequestDispatcher;

Controller Servlet Extend HttpServlet – public class Controller extends HttpServlet { …. } – Override some methods of HttpServlet Class Place controller logic in this protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {... }

Controller Logic Override doGet or doPost protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {... }

Servlet Controller Code (page 55) public class Controller extends HttpServlet { protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { if (request.getParameter("processButton") != null) { address = "../Process.jsp"; } else if (request.getParameter("confirmButton") != null) { address = "../Confirm.jsp"; } else { address = "../Edit.jsp"; } RequestDispatcher dispatcher = request.getRequestDispatcher(address); dispatcher.forward(request, response); }

Jsp + Servlet What’s the difference?

Servlet Location Source file could be anywhere.class file must be in subdirectory classes classes directory must already exist in CLASSPATH Define mapping in web.xml JSP must placed in the folder that is referenced

Servlet Identity FQN Fully Qualified Name (FQN) – is an unambiguous name that specifies which object, function, or variable a call refers to (WIKI) The package and the class name are combined to define the unique name of a class.

Servlet Identity FQN We can use the FQN to define a short name for tomcat to be used internally Ch2Controller ch2.servletController.Controller

Servlet Access Create a short name for servlet in web.xml – See previous slide Create a mapping Ch2Controller /ch2/servletController/Controller

More about Servlet Mapping 1.If the next JSP is in the same directory where the controller is mapped – address=“Confirm.jsp” 2.If is not in the same directory – address=“/ch2/servletController/Confirm.jsp”

Servlet mapping

WAR Files Web application ARchive (WAR) assPage.shtml?CSS assPage.shtml?CSS – If you scroll down to Tomcat Information Deploying a WAR file

Web Servlet Annotation One may skip web.xml mapping It does not work in all cases. If you do the mapping in web.xml – Must remove the annotation from the class

Web Servlet Annotation urlPatterns={"/ch2/servletController/annotated/Controller"}) public class Controller extends HttpServlet { … }

Servlet Engine for Servlets Developer must re-compiled Default behaviors does not update.class – Some systems may be configured to automaticly update

Servlet in IDEs After you build web project – Execute them When WAR file is built – Source packages will be copied to classes To export WAR File – File  Export  Web  War File

Class Files Avoid using default package – Only keep your configuration files If web.xml is not created – File->New-> Standard Deployment Desctiptor

Questions Any Questions Review your java!