Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 8 Script-free pages. Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java."— Presentation transcript:

1 Chapter 8 Script-free pages

2 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

3 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?

4 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; %> $.

5 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

6 MVC

7 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

8 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

9 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)

10 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

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

12 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

13 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; }

14 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

15 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)

16 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

17 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

18 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

19 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(…)

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

21 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

22 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

23 Example of using EL

24

25

26

27 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


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

Similar presentations


Ads by Google