Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1

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

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

4 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

5 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

6 Java Questions (@home) Write a controller using a servlet that checks to see if edit,confirm or 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.

7 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?

8 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

9 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?

10 File Structure Tutorials/WEB-INF/ classes/*.class lib/*.jar web.xmlindex.jsp edit.jsp confirm.jsp

11 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

12 Sending Data

13 Sending Data Test <input type=“text" name="hobby" value="${param.hobby}">

14 Relative and Absolute Reference Same as we sow in HTML Questions – http://server.com/path/Edit.jsp http://server.com/path/Edit.jsp – http://server.com/path/Confirm.jsp http://server.com/path/Confirm.jsp – http://server.com/utils/files/validate.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 ?

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

16 Hidden Field Technique Edit.jsp sends data to Confirm.jsp – How to send data back to Edit.jsp? Hidden Fields –

17 Hidden Fields! Let’s Compare Edit – Confirm –

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

19 Inefficient Solution <input type="hidden" name="hobby" value="${param.hobby}"> <input type="submit" name="editButton" value="Edit"> <input type="hidden" name="hobby" value="${param.hobby}"> <input type="submit" name="processButton" value="Process">

20 Better Solution: Controller Using a controller is better solution

21 Example: Confirm.jsp <input type="hidden" name="hobby" value="${param.hobby}"> <input type="submit" name="editButton" value="Edit"> <input type="submit" name="processButton" value="Process">

22 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”)

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

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

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

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

27 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)

28 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); %>

29 Confirm + Controller <input type="hidden" name="hobby" value="${param.hobby}"> <input type="submit" name="editButton" value="Edit"> <input type="submit" name="processButton" value="Process">

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

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

32 JAVA CLASSPATH Windows – setx CLASSPATH=classpath1;classpath2... Linux/Unix/MacOsX – setenv CLASSPATH classpath1:classpath2...

33 Classpath + Packages http://users.cs.fiu.edu/~downeyt/webdev/p ackages.html http://users.cs.fiu.edu/~downeyt/webdev/p ackages.html http://javarevisited.blogspot.com/2011/01/h ow-classpath-work-in-java.html http://javarevisited.blogspot.com/2011/01/h ow-classpath-work-in-java.html http://docs.oracle.com/javase/1.4.2/docs/to oldocs/solaris/classpath.html http://docs.oracle.com/javase/1.4.2/docs/to oldocs/solaris/classpath.html http://docs.oracle.com/javase/1.4.2/docs/to oldocs/solaris/classpath.html http://docs.oracle.com/javase/1.4.2/docs/to oldocs/solaris/classpath.html

34 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

35 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;

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

37 Controller Logic Override doGet or doPost methods @Override protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {... }

38 Servlet Controller Code (page 55) public class Controller extends HttpServlet { protected void doGet(HttpServletRequest request,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"; } RequestDispatcher dispatcher = request.getRequestDispatcher(address); dispatcher.forward(request, response); }

39 Jsp + Servlet What’s the difference?

40 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

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

42 Servlet Identity FQN We can use the FQN to define a short name for tomcat to be used internally Ch2Controller ch2.servletController.Controller

43 Servlet Access Create a short name for servlet in web.xml – See previous slide Create a mapping Ch2Controller /ch2/servletController/Controller

44

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

46 Servlet mapping

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

48 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

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

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

51

52 Servlet in IDEs After you build web project – Execute them When WAR file is built – Source packages will be copied to classes To export WAR File – File  Export  Web  War File

53 Class Files Avoid using default package – Only keep your configuration files If web.xml is not created – File->New-> Standard Deployment Desctiptor

54 Questions Any Questions Review your java!


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

Similar presentations


Ads by Google