Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CGS – 4854 Summer 2012 Web Site Construction and Management Instructor: Francisco R. Ortega Chapter 3 Part 1."— Presentation transcript:

1

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

3 Today’s Lecture Chapter 3 Power Point + Board – It pays to attend class! Remember I have office hours Today!

4 Member Variables A servlet is a Java class Accessible as long as the object is in memory Accessible for the life of the servlet to any and all requests. – different users/clients.

5 Java Beans Encapsulates other objects (data) into one Makes data safer. Makes it easier to move data around

6 Java Beans Specifications Automatic operations on your objects auto-generation from data stores, or vice versa frameworks such as Hibernate to work with them basically, the tools and frameworks exist that work with JavaBeans and make your life easier

7 A default (no argument) constructor Must be serializable All fields exposed with getters, setters – Default Validation using is

8 JavaBean conventions The properties that are created in a bean are tied closely to the data in the edit page. If the name of the input element is hobby, then the accessor and mutator in the property will be getHobby and setHobby – Notice that the element name is lower case h, but the methods have upper case H.

9 In Class Question Define a Java Bean with – playerName – playerSalary How do you access the data in the jsp page? Hint: Expression Language

10 Setting the value RequestData data = new RequestData(); data.setPlayerName(request.getParameter(“ playerName”));

11 Beans + JSP To access the bean via the JSP, the bean data must be placed in the “SESSION”

12 Sessions  The session is a place that shared data can be placed.  Only a JSP or servlet can access the session.  Tomcat creates the session for each user/browser.  Each request has a separate session  Example:  Session session = request.getSession();  session.setAttribute("refData", data);

13 The Big Picture

14 Tomcat (and others) Tomcat is a multi-threaded application When a servlet is loaded, it remains in memory Each request for a servlet is handled by a new thread that it spawns (or pulled from a thread pool)

15 Avoiding Member Variables Synchronization issues. In class example

16 In Class - Java Review Inheritance

17 Base Class import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelperBase { protected HttpServletRequest request; protected HttpServletResponse response; public HelperBase(HttpServletRequest request, HttpServletResponse response) { this.request = request; this.response = response; }

18 Controller Helper public class ControllerHelper extends HelperBase { protected RequestDataDefault data =new RequestDataDefault(); public ControllerHelper(HttpServletRequest request, HttpServletResponse response) { super(request, response); } public Object getData() { return data; } protected void doGet() {.... }

19 doGet() protected void doGet() throws ServletException, IOException { request.getSession().setAttribute("helper", this); data.setHobby(request.getParameter("hobby")); data.setAversion(request.getParameter("aversion")); 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); }

20

21 MVC Model View Controller Model: RequestData View : JSP Controller: ControllerHelper

22 More chapter 3 next week Next Tuesday – Quiz about Chapter 3 and Chapter 2 – Start reading Chapter 4 for next week Start preparing for Mid-Term Exam – You will be tested in Chapter 1 thru 4 Maybe part of chapter 5. – More about this next week!

23 Office Hours Remember that I switch my office hours for Tuesdays.


Download ppt "CGS – 4854 Summer 2012 Web Site Construction and Management Instructor: Francisco R. Ortega Chapter 3 Part 1."

Similar presentations


Ads by Google