220 likes | 325 Vues
In this lecture, we explore key concepts from Chapter 3 of Web Site Construction and Management. The focus is on Java Beans and servlets, discussing their architecture, member variables, serialization, and the importance of session management in Tomcat. We outline the MVC pattern with practical examples, detailing how to define and access Java Beans in JSP pages. This session emphasizes the need for good coding practices, avoiding synchronization issues, and prepares students for upcoming assessments. Attend office hours for further assistance and clarification on these topics.
E N D
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 • Accessible as long as the object is in memory • Accessible for the life of the servlet to any and all requests. • different users/clients.
Java Beans • Encapsulates other objects (data) into one • Makes data safer. • Makes it easier to move data around
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
A default (no argument) constructor • Must be serializable • All fields exposed with getters, setters • Default Validation using is
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.
In Class Question • Define a Java Bean with • playerName • playerSalary • How do you access the data in the jsp page? Hint: Expression Language
Setting the value • RequestData data = new RequestData(); • data.setPlayerName(request.getParameter(“playerName”));
Beans + JSP • To access the bean via the JSP, the bean data must be placed in the “SESSION”
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);
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)
Avoiding Member Variables • Synchronization issues. • In class example
In Class - Java Review • Inheritance
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; } }
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() { .... } }
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); }
MVC • Model View Controller • Model: RequestData • View : JSP • Controller: ControllerHelper
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!
Office Hours • Remember that I switch my office hours for Tuesdays.