1 / 53

CGS – 4854 Summer 2012

CGS – 4854 Summer 2012 . Instructor: Francisco R. Ortega Chapter 2. Web Site Construction and Management. Today’s Lecture – 05/16/12. Some Java questions to do at home. Chapter 2 Remember I have office hours Today!. Java Questions (@home).

chava
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 2 Web Site Construction and Management

  2. Today’s Lecture – 05/16/12 • Some Java questions to do at home. • Chapter 2 • Remember I have office hours Today!

  3. Java Questions (@home) • Write logic for a controller using JSP that calls confirm.jsp if: • Button was pressed and amount of total users currently requesting the page is less that 10000 • Hint USE static variable to add or remove users. • Otherwise, send to Busy.jsp

  4. Java Questions (@home) • Write logic for a controller using JSP that calls sold.jsp if • Button buy was pressed and textbox quantity is greater than 1. • Check that value of textbox is present before asking if > 1

  5. Java Questions (@home) • Write a controller using a servlet that checks to see if edit,confirmor cancel was pressed • If none pressed, send them to BadQuery.jsp • If any of them pressed send them to their page. • Check that the button was pressed and that value is not an empty string.

  6. Java Questions(@home) • How do you open a PrintWriter and write something to it in Java? • How do you read a file using a scanner? • Write the code that will do the following: if a number is less than 0, display 'negative'; if a number is greater than 100, display 'over 100'; for all other numbers, display 'valid grade'. • How do you test if a string is empty as opposed to not existing?

  7. Java Questions(@home) • How do you define a variable for a class named ShoppingCart? • How do you create a new object for a class named ShoppingCart? • Assume it takes CartList as parameter

  8. Java Questions(@home) • What is an abstract class? • What is an interface ? • Create some classes (and extend them) to show: • Abstract Classes • Use of interface • Create a class person and then student. • How do you call the base constructor of person?

  9. File Structure

  10. Web Applications • Web application can have many properties in common • For the rest of the chapter, we will see • Edit page • Confirm page • Process page • For a more smooth operation • Controller class will be needed • A servlet page

  11. Sending Data • <form action=“Confirm.jsp">

  12. Sending Data <form action=“Confirm.jsp"> <p> Test <br> <input type=“text" name="hobby" value="${param.hobby}"> <input type="submit“ name=“confirmBtn" value=“Confirm"> </form>

  13. Relative and Absolute Reference • Same as we sow in HTML • Questions • http://server.com/path/Edit.jsp • http://server.com/path/Confirm.jsp • http://server.com/utils/files/validate.jsp • What is the absolute and relative reference for Confirm.jsp if called from Edit.jsp ? • What is the absolute and relative reference for validate.jsp if called from Confirm.jsp ?

  14. Retrieving Values (Form Element) • ${param.field} • For example • ${param.hobby} • Try Edit.jsp from book (page 38)

  15. Hidden Field Technique • Edit.jsp sends data to Confirm.jsp • How to send data back to Edit.jsp? • Hidden Fields • <input type=“hidden” name=“hobby” value = “${param.hobby}”>

  16. Hidden Fields! Let’s Compare • Edit • <input type=“text” name=“hobby” value = “${param.hobby}”> • Confirm • <input type=“hidden” name=“hobby” value = “${param.hobby}”>

  17. More in Sending Data! • How do we send data to either edit.jsp or process.jsp?

  18. Inefficient Solution <form action="Edit.jsp"> <input type="hidden" name="hobby" value="${param.hobby}"> <input type="submit" name="editButton" value="Edit"> </form> <form action="Process.jsp"> <input type="hidden" name="hobby" value="${param.hobby}"> <input type="submit" name="processButton" value="Process"> </form>

  19. Better Solution: Controller • Using a controller is better solution

  20. Example: Confirm.jsp <form action="Controller.jsp"> <p> <input type="hidden" name="hobby" value="${param.hobby}"> <input type="submit" name="editButton" value="Edit"> <input type="submit" name="processButton" value="Process"> </form>

  21. Controller • It only contains java code • Each JSP will send its data to the controller • First, it will be shown as a JSP (for simplicity) • Objects: • HttpServletRequest • HttpServletResponse • Reference Parameters: • Request.getParameter(“hobby”)

  22. Testing Button • Button clicked will show in the query string If (request.getParameter(“processButton”) != null)

  23. Controller Logic • Each form’s actions is mapped to controller • Each submit button has unique name • Controller decides which page to call next • Controller knows URL of all JSP that controls

  24. Controller Logic if (request.getParameter("processButton") != null) { address = "Process.jsp"; } else if (request.getParameter("confirmButton") != null) { address = "Confirm.jsp"; } else { address = "Edit.jsp"; }

  25. Forward Control! • RequestDispatcher dispatcher = request.getRequestDispatcher(address); • dispatcher.forward(request,response)

  26. JSP or Servlet? • You can use either one • Easier to read in JSP • Better performance in Servlet • See Controller Code page 50 (or next slide)

  27. Controller Code JSP <% 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); %>

  28. Confirm + Controller <form action="Controller.jsp"> <p> <input type="hidden" name="hobby" value="${param.hobby}"> <input type="submit" name="editButton" value="Edit"> <input type="submit" name="processButton" value="Process"> </form>

  29. JSP vs Java Servlet JSP Java Servlet Does not have to recreate Prefer when lots Java and little HTML Java IDE very useful • It will be recreated each time • Prefer when lots of HTML and little JSP • Easy to write HTML • Hard to debug

  30. More JSP/Servlet • If you have lots of HTML and JAVA • Redesign your site: • Write more code in the controller

  31. JAVA CLASSPATH • Windows • setxCLASSPATH=classpath1;classpath2... • Linux/Unix/MacOsX • setenv CLASSPATH classpath1:classpath2...

  32. Classpath + Packages • http://users.cs.fiu.edu/~downeyt/webdev/packages.html • http://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html • http://docs.oracle.com/javase/1.4.2/docs/tooldocs/solaris/classpath.html • http://docs.oracle.com/javase/1.4.2/docs/tooldocs/solaris/classpath.html

  33. More about Packages • If your class directory is called “classes” • What is the name of the package for classes/store? • Store • How about classes/store/hardware ? • store.hardware

  34. Controller Servlet • Place servlet in a package • Location should not be in the default package • Import the following classes: • import java.io.IOException; • import javax.servlet.ServletException; • import javax.servlet.http.HttpServlet; • import javax.servlet.http.HttpServletRequest; • import javax.servlet.http.HttpServletResponse; • import javax.servlet.RequestDispatcher;

  35. Controller Servlet • Extend HttpServlet • public class Controller extendsHttpServlet{ …. } • Override some methods of HttpServlet Class • Place controller logic in this method: @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ... }

  36. Controller Logic • Override doGet or doPost methods @Override protected void doGet(HttpServletRequestrequest,HttpServletResponseresponse) throws ServletException, IOException { ... }

  37. Servlet Controller Code (page 55) public class Controller extends HttpServlet { protected void doGet(HttpServletRequestrequest,HttpServletResponse response) throws ServletException, IOException { if (request.getParameter("processButton") != null) { address = "../Process.jsp"; } else if (request.getParameter("confirmButton") != null) { address = "../Confirm.jsp"; } else { address = "../Edit.jsp"; } RequestDispatcherdispatcher = request.getRequestDispatcher(address); dispatcher.forward(request, response); } }

  38. Jsp + Servlet • <form action=“Controller”> • What’s the difference?

  39. Servlet Location • Source file could be anywhere • .class file must be in subdirectory classes • classes directory must already exist in CLASSPATH • Define mapping in web.xml • JSP must placed in the folder that is referenced

  40. Servlet Identity FQN • Fully Qualified Name (FQN) • is an unambiguous name that specifies which object, function, or variable a call refers to (WIKI) • The package and the class name are combined to define the unique name of a class.

  41. Servlet Identity FQN • We can use the FQN to define a short name for tomcat to be used internally <servlet> <servlet-name>Ch2Controller </servlet-name> <servlet-class>ch2.servletController.Controller </servlet-class> </servlet>

  42. Servlet Access • Create a short name for servlet in web.xml • See previous slide • Create a mapping <servlet-mapping> <servlet-name>Ch2Controller</servlet-name> <url-pattern>/ch2/servletController/Controller</url-pattern> </servlet-mapping>

  43. More about Servlet Mapping • If the next JSP is in the same directory where the controller is mapped • address=“Confirm.jsp” • If is not in the same directory • address=“/ch2/servletController/Confirm.jsp”

  44. Servlet mapping

  45. WAR Files • Web application ARchive (WAR) • http://users.cis.fiu.edu/~downeyt/webdev/classPage.shtml?CSS • If you scroll down to Tomcat Information • Deploying a WAR file

  46. Web Servlet Annotation • One may skip web.xml mapping • It does not work in all cases. • If you do the mapping in web.xml • Must remove the annotation from the class

  47. Web Servlet Annotation • Example @WebServlet( urlPatterns={"/ch2/servletController/annotated/Controller"}) public class Controller extends HttpServlet { … }

  48. Servlet Engine for Servlets • Developer must re-compiled • Default behaviors does not update .class • Some systems may be configured to automaticly update

More Related