1 / 22

CGS – 4854 Summer 2012

CGS – 4854 Summer 2012. Instructor: Francisco R. Ortega Chapter 3 Part 1. Web Site Construction and Management. Today’s Lecture. Chapter 3 Power Point + Board It pays to attend class! Remember I have office hours Today!. Member Variables. A servlet is a Java class

macon
Download Presentation

CGS – 4854 Summer 2012

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

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

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

  7. 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 getHobbyand setHobby • Notice that the element name is lower case h, but the methods have upper case H.

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

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

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

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

  12. The Big Picture

  13. 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)

  14. Avoiding Member Variables • Synchronization issues. • In class example

  15. In Class - Java Review • Inheritance

  16. 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; } }

  17. 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() { .... } }

  18. 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); }

  19. MVC • Model View Controller • Model: RequestData • View : JSP • Controller: ControllerHelper

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

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

More Related