Chapter 8 Script-free pages. Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java.

Slides:



Advertisements
Similar presentations
8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
Advertisements

JSP and Servelets.
Apache Struts Technology
Chapter 81 JavaBeans JavaServer Pages By Xue Bai.
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai.
Model-View-Controller ("MVC") This is a architectural design pattern for interactive applications. This design pattern organizes an interactive application.
JSP: JavaServer Pages Juan Cruz Kevin Hessels Ian Moon.
CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.
Java Server Pages B.Ramamurthy. Java Server Pages Servlets are pure Java programs. They introduce dynamism into web pages by using programmatic content.
Comp2513 Java Server Pages Daniel L. Silver, Ph.D.
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
Struts 2.0 an Overview ( )
UNIT-V The MVC architecture and Struts Framework.
Java Server Pages (JSP) Presented by: Ananth Prasad & Alex Ivanov May 10, 2001.
CSC 2720 Building Web Applications Using Java Beans, Custom Tags and Tag Libraries in JSP pages.
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.
JDeveloper 10g and JavaServer Faces: High-Performance UIs on the Web Avrom Roy-Faderman Senior Programmer May, 2006.
Design Patterns Phil Smith 28 th November Design Patterns There are many ways to produce content via Servlets and JSPs Understanding the good, the.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
Introduction to JavaServer Pages (JSP) Slides from Dr. Mark Llewellyn.
Java Server Pages Lecture July Java Server Pages Java Server Pages (JSPs) provide a way to separate the generation of dynamic content (java)
Jordan Anastasiade. All rights reserved.
JSP Architecture Outline  Model 1 Architecture  Model 2 Architecture.
 Embeds Java code  In HTML tags  When used well  Simple way to generate dynamic web-pages  When misused (complex embedded Java)  Terribly messy.
Mark Dixon 1 12 – Java Beans. Mark Dixon 2 Session Aims & Objectives Aims –To cover the use of Java Beans Objectives, by end of this week’s sessions,
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Java Server Pages™ A technology for serving dynamic web content Stanisław Osiński,
Case Study + MVC Lec Error Pages By means of the page directive, a JSP can be given the responsibility of an Error page An Error JSP will be called.
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
Slides © Marty Hall, book © Sun Microsystems Press 1 JSP Scripting Elements Core Servlets & JSP book:
CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.
SE-2840 Dr. Mark L. Hornick 1 Java Server Pages. HTML/JSPs are intended to be used as the views in an MVC- based web application Model – represents an.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
10-Nov-15 Java Object Oriented Programming What is it?
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
Java Server Pages (JSP)
Java Server Pages An introduction to JSP. Containers and Components Several clients – one system.
Model View Controller Architecture of Java Web Applications Dr. M V S Peri Sastry, Ph.D.[BITS-Pilani]
CS-4220 Dr. Mark L. Hornick 1 Java Server Pages. HTML/JSPs are intended to be used as the views in an MVC- based web application Model – represents an.
Chính phủ điện tử TS. Phạm Văn Tính Khoa CNTT, ĐH Nông Lâm TP.HCM
CourseOutline Example & JavaBeans Lec Start with Example Displaying Course Outlines User will select either course “web design & development” or.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
Web Technologies Java Beans & JSP By Praveen Kumar G.
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
CS562 Advanced Java and Internet Application Introduction to the Computer Warehouse Web Application. Java Server Pages (JSP) Technology. By Team Alpha.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
Chapter 3 JSP Overview. The Problem with Servlets processing the request and generating the response are both handled by a single servlet class Java programming.
CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
Chapter 6 Chapter 6 Server Side Programming (JSP) Part 1 1 (IS 203) WebProgramming (IS 203) Web Programming.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
Chapter 14 Using JavaBeans Components in JSP Documents.
JSP Java Server Pages. Hello, !
Apache Struts Technology A MVC Framework for Java Web Applications.
CS320 Web and Internet Programming Web Application and MVC Chengyu Sun California State University, Los Angeles.
A Presentation Presentation On JSP On JSP & Online Shopping Cart Online Shopping Cart.
CS 562 Advanced Java and Internet Application Computer Warehouse Web Application By Team Alpha :-  Puja Mehta (102163)  Mona Nagpure (102147)
Structure of a web application
Scripted Page Web App Development (Java Server Pages)
Scope and State Handling in JSPs
Design and Maintenance of Web Applications in J2EE
MSIS 655 Advanced Business Applications Programming
Java Server Pages B.Ramamurthy.
CS320 Web and Internet Programming Java Beans
CS3220 Web and Internet Programming Expression Language (EL)
CS3220 Web and Internet Programming Expression Language (EL)
Scripted Page Web Application Development (Java Server Pages)
Presentation transcript:

Chapter 8 Script-free pages

Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java code in your JSP files – This can become a maintenance nightmare Page designer does not know Java and programmer is not be able to design prettier pages

Example Suppose we have a small web program that asks user the name and age, and then calculate the monthly fee of a sports club. Here is the HTML code for this page test What is your name? What is your age?

Using scripting to generate the dynamic content Here is the HTML code for this page test What is your name? What is your age? Here is the JSP code to process the user form data process Thanks, ! Your monthly payment for the sports club is <% int age=Integer.parseInt(request.getParameter("age")); int payment = 0; if (age <6) payment =0; else if (age < 18) payment = 10; else if (age < 60) payment = 30; else payment = 10; %> $.

MVC When program becomes more complex, it is hard for both web designer and program to implement and maintain it By applying the Model-View-Controller (MVC) architecture to a J2EE application, you separate core business model functionality from the presentation and control logic that uses this functionality Such separation make it easier to implement, test, and maintain the application

MVC

Model - The model represents enterprise data and the business rules that govern access to and updates of this data. View -The view renders the contents of a model. It accesses enterprise data through the model and specifies how that data should be presented. It is the view's responsibility to maintain consistency in its presentation when the model changes. Controller - The controller translates interactions with the view into actions to be performed by the model. Very often in a J2EE application, controller is responsible for dispatching request to appropriate modules

MVC In J2EE application – Model can be Java Beans, Enterprise Java Beans (EJB), or other services – View normally is JSP pages that is responsible for rendering the HTML page – Controller normally is a servlet that is responsible for dispatching user request to appropriate components such as other servlet or JSP pages

JavaBeans It is a java class that follow the following rules: – You must have a public, no-arg constructor – You must name your public getter and setter methods starting with get (or is, for a boolean) and set, followed by the name of the instance variable (also called bean property) with the first letter of the word capitalized – The setter argument type and the getter return type MUST be identical. This defines the property type. int getFoo() void setFoo(int foo)

Example JavaBean public class UserInfo { private String name; public void setName(String sname){ name = sname; } public String getName(){ return name; } Pay attention that we have a capitalized N in the setter and getter method

JavaBeans By the way, you need to package your class for its use in Tomcat – That is, you need to write somehthing like package edu.nku.cs package foo at the beginning of your Java Beans, otherwise, tomcat cannot find your class Also, sometimes, you do not have to have both getter and setter for a property. See the following example for detail.

JavaBeans You can use Java Beans to automatically extract the HTTP GET/POST parameters and set these parameters in corresponding instance variables for you – Thus you do not need to use request.getParameter() or request.getParameterValues() in your JSP file To do this, you need to define instance variables in Java Beans that have the exact names (variable names) as the name of the parameters passed by HTTP GET/POST method – Of course, you also need to follow the rules of Java Beans such as no-arg constructor, rules for getter and setter method

Example of using JavaBeans test What is your name? What is your age? package foo; public class UserInfo { private String name; private int age; public void setName(String sname){ name = sname; } public String getName(){ return name; } public void setAge(int sage){ age = sage; } public int getAge(){ return age; } public int getMonthlyFee(){ int payment; if (age <6) payment =0; else if (age < 18) payment = 10; else if (age < 60) payment = 30; else payment = 10; return payment; }

Example of using Java Beans In your JSP file, you need to declare and initalize a bean attribute with Identifier for the bean object. Think it as a variable name for one instance of the bean class Class name

Example of using Java Beans Then you can set a bean attribute’s property value with Identifies the actual bean object. This will match the “id” value from the tag Identifies the property name (in other words, the thing with the getter and setter in the bean class)

Example of using Java Beans This is your jsp file process.jsp process Thanks, ! Your monthly payment for the sports club is $. package foo; public class UserInfo { private String name; private int age; public void setName(String sname){ name = sname; } public String getName(){ return name; } public void setAge(int sage){ age = sage; } public int getAge(){ return age; } public int getMonthlyFee(){ int payment; if (age <6) payment =0; else if (age < 18) payment = 10; else if (age < 60) payment = 30; else payment = 10; return payment; } The property name in JSP and the variable name in bean have to match

Example of using JavaBeans There is a even better approach – You can set multiple property values with Identifies the actual bean object. This will match the “id” value from the tag Using * to set the values of all properties

Example of using Java Beans process Thanks, ! Your monthly payment for the sports club is $. package foo; public class UserInfo { private String name; private int age; public void setName(String sname){ name = sname; } public String getName(){ return name; } public void setAge(int sage){ age = sage; } public int getAge(){ return age; } public int getMonthlyFee(){ int payment; if (age <6) payment =0; else if (age < 18) payment = 10; else if (age < 60) payment = 30; else payment = 10; return payment; } here will call both setName(…) and setAge(…) to set property name and age

Example of using Java Beans Please be noted that one nice feature of using Java Beans is that if Java Bean properties are Strings or primitives, action will do the coercing for you – For example, we have age as an int type, action takes the String request parameter age, converts it to an int, and passes that int to the bean’s setter method setAge(…)

Example of using Java Beans After you extract the HTTP GET/POST request parameters and set them in Java Beans, you can get these info out of Beans by using Pay attention to which only has one getter method without setter method (which we do not need) – We put our programming logic in getMonthlyFee() method.

Example of using Java Beans process Thanks, ! Your monthly payment for the sports club is $. package foo; public class UserInfo { private String name; private int age; public void setName(String sname){ name = sname; } public String getName(){ return name; } public void setAge(int sage){ age = sage; } public int getAge(){ return age; } public int getMonthlyFee(){ int payment; if (age <6) payment =0; else if (age < 18) payment = 10; else if (age < 60) payment = 30; else payment = 10; return payment; } calls corresponding getter method

Scriptless JSPs using Expression Language (EL) EL stands for Expression Language It has some implicit objects – Such as param – Use this when you have only one parameter for that particular parameter name paramValues – Use paramValues when you might have more than one parameter value for a given parameter name

Example of using EL

You can use EL for Java Bean also process Thanks, ${userData.name} ! Your monthly payment for the sports club is $ ${userData.monthlyFee}. We can use ${Name_of_Bean.name_of_property} to access Java Bean’s property easily without resorting to