530 likes | 642 Views
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).
E N D
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) • 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
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
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.
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?
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
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?
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
Sending Data • <form action=“Confirm.jsp">
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>
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 ?
Retrieving Values (Form Element) • ${param.field} • For example • ${param.hobby} • Try Edit.jsp from book (page 38)
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}”>
Hidden Fields! Let’s Compare • Edit • <input type=“text” name=“hobby” value = “${param.hobby}”> • Confirm • <input type=“hidden” name=“hobby” value = “${param.hobby}”>
More in Sending Data! • How do we send data to either edit.jsp or process.jsp?
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>
Better Solution: Controller • Using a controller is better solution
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>
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”)
Testing Button • Button clicked will show in the query string If (request.getParameter(“processButton”) != null)
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
Controller Logic if (request.getParameter("processButton") != null) { address = "Process.jsp"; } else if (request.getParameter("confirmButton") != null) { address = "Confirm.jsp"; } else { address = "Edit.jsp"; }
Forward Control! • RequestDispatcher dispatcher = request.getRequestDispatcher(address); • dispatcher.forward(request,response)
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)
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); %>
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>
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
More JSP/Servlet • If you have lots of HTML and JAVA • Redesign your site: • Write more code in the controller
JAVA CLASSPATH • Windows • setxCLASSPATH=classpath1;classpath2... • Linux/Unix/MacOsX • setenv CLASSPATH classpath1:classpath2...
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
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
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;
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 { ... }
Controller Logic • Override doGet or doPost methods @Override protected void doGet(HttpServletRequestrequest,HttpServletResponseresponse) throws ServletException, IOException { ... }
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); } }
Jsp + Servlet • <form action=“Controller”> • What’s the difference?
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
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.
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>
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>
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”
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
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
Web Servlet Annotation • Example @WebServlet( urlPatterns={"/ch2/servletController/annotated/Controller"}) public class Controller extends HttpServlet { … }
Servlet Engine for Servlets • Developer must re-compiled • Default behaviors does not update .class • Some systems may be configured to automaticly update