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

CGS – 4854 Summer 2012

  • Upload
    macon

  • View
    38

  • Download
    0

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

PowerPoint Presentation

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

1Todays LectureChapter 3Power Point + BoardIt pays to attend class!Remember I have office hours Today!2Member VariablesA servlet is a Java classAccessible as long as the object is in memoryAccessible for the life of the servlet to any and all requests.different users/clients.

Java BeansEncapsulates other objects (data) into oneMakes data safer.Makes it easier to move data around

Java Beans SpecificationsAutomatic operations on your objectsauto-generation from data stores, or vice versaframeworks such as Hibernate to work with thembasically, the tools and frameworks exist that work with JavaBeans and make your life easierA default (no argument) constructorMust be serializableAll fields exposed with getters, settersDefault Validation using is

JavaBean conventionsThe 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 setHobbyNotice that the element name is lower case h, but the methods have upper case H.

In Class QuestionDefine a Java Bean with playerNameplayerSalaryHow do you access the data in the jsp page? Hint: Expression Language

Setting the valueRequestData data = new RequestData();data.setPlayerName(request.getParameter(playerName));

Beans + JSPTo access the bean via the JSP, the bean data must be placed in the SESSION SessionsThe 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 sessionExample:Session session = request.getSession();session.setAttribute("refData", data);

The Big Picture

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

Avoiding Member VariablesSynchronization issues. In class exampleIn Class - Java ReviewInheritanceBase Classimport 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 Helperpublic 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); }

MVCModel View ControllerModel: RequestDataView : JSP Controller: ControllerHelper More chapter 3 next weekNext TuesdayQuiz about Chapter 3 and Chapter 2Start reading Chapter 4 for next weekStart preparing for Mid-Term ExamYou will be tested in Chapter 1 thru 4 Maybe part of chapter 5. More about this next week!Office HoursRemember that I switch my office hours for Tuesdays. 46176.0