objectives
play

Objectives Servlets Review JSPs Web Application Organization Apr - PDF document

Objectives Servlets Review JSPs Web Application Organization Apr 30, 2019 Sprenkle - CS335 1 Servlets Review What is the servlet life cycle? Init parameters Where are init parameters defined? How do we access a


  1. Objectives • Servlets Review • JSPs • Web Application Organization Apr 30, 2019 Sprenkle - CS335 1 Servlets Review • What is the servlet life cycle? • Init parameters Ø Where are init parameters defined? Ø How do we access a servlet’s init parameter? Ø Why do we use init parameters? • How can we save state across multiple requests from a user? Ø What are the pros and cons of each? • Since multiple users can access a servlet at the same time, what problem should we be concerned about? Ø What is the cause of the issue? Ø How do we solve it (theoretically; we haven’t shown how to implement yet)? Apr 30, 2019 Sprenkle - CS335 2 1

  2. Review: SurveyServlet Servlet Life Cycle Parameter Servlet Web Application Server • Web application server creates one instance of servlet Ø Calls init method of servlet created • As requests come in, WAS calls service method of appropriate servlet Ø In turn, servlet calls appropriate doMethod • When web application server shuts down, calls destroy method of each servlet Apr 30, 2019 Sprenkle - CS335 3 JAVASERVER PAGES (JSPS) Apr 30, 2019 Sprenkle - CS335 4 2

  3. Discussion What made writing servlets difficult? Apr 30, 2019 Sprenkle - CS335 5 Motivation: JavaServer Pages (JSPs) • Simplify web application development • Separate UI from backend code Ø Separate presentation layer • Difficult to write HTML in print statements Moving to here Apr 30, 2019 Sprenkle - CS335 6 3

  4. JavaServer Pages (JSPs) • Merge HTML and Java Ø Separate static HTML from dynamic Ø Make HTML templates, fill in dynamic content Ø Encourages separation of tasks • Web application server compiles JSPs into servlet code Ø Clean and efficient • Easier to develop, deploy, modify scripted pages Ø How much trouble did you have with HTML in Strings? Apr 30, 2019 Sprenkle - CS335 7 JSP Syntax: Expression • Enclose code in <%= %> <html> <body> <p> Hello! The time is now <%= new new java.util.Date() %> </p> </body> </html> Expression Evaluated, turned into a String Apr 30, 2019 Sprenkle - CS335 8 4

  5. JSP Syntax: Scriptlet <html> <body> <% // This is a scriptlet. The "date" variable // we declare here is available in the // embedded expression later on. java.util.Date date = new new java.util.Date(); %> <p> Hello! The time is now <%= date %> </p> </body> </html> Apr 30, 2019 Sprenkle - CS335 9 Example: SurveyServlet as a JSP <% for (int i = 0; i < animalNames.length; i++) { %> <tr> <td><%=animalNames[i]%></td> <td><%=votes[i]%></td> <td><%=formattedPercentages[i]%></td> <% totalVotes += votes[i]; %> </tr> To be displayed at end <% } %> Apr 30, 2019 Sprenkle - CS335 10 5

  6. JSP Directives • Page Directive Ø Java files to import (like import statement in Java) <%@ page import="java.util.*,java.text.*" %> <%@ page import="ourcode.MyClass"%> • Include Directive Ø Include contents of another file: JSP, HTML, or text Ø Example: include site’s common headers or footers <%@ include file="header.jsp" %> Apr 30, 2019 Sprenkle - CS335 11 JSP Variables • By default, JSPs have some variables Ø Not explicitly declared in the file Ø HttpServletRequest request request Ø HttpServletResponse response response Ø HttpSession session session These names must be used • JSPs can access request parameters, session data Apr 30, 2019 Sprenkle - CS335 12 6

  7. JSP Declarations • For instance variables and methods <%! private ArrayList users; public void jspInit() { // on start up: set up } public void jspDestroy() { // on shut down: clean up } %> • We won’t do too much of this Ø Let servlets do the work Apr 30, 2019 Sprenkle - CS335 13 Web Application Architecture HTML With Server-side Dynamic Parts Java Classes JSP (Model) Client DataStore Java Servlets • Heavy lifting of requests • Forward to JSPs Apr 30, 2019 Sprenkle - CS335 14 7

  8. Communicating Between Servlets and JSPs: Login Example welcome.jsp 1 Server-side • Form to login.jsp Client login 2 Login Servlet User makes login request • Check user name/password • Set authenticated authenticated or error error attribute • Forward to welcome.jsp or login.jsp Apr 30, 2019 Sprenkle - CS335 15 Communicating Between Servlets and JSPs: Login Example • Check authenticated authenticated • User’s Options welcome.jsp Server-side • Display login.jsp Client error message • Form to success 2 error login Login Servlet User makes login request • Check user name/password • Set authenticated authenticated or error error attribute • Forward to welcome.jsp or login.jsp Apr 30, 2019 Sprenkle - CS335 16 8

  9. Communicating Between JSPs and Servlets • Attributes Login welcome.jsp Servlet Ø Name/Value pairs Ø Values are Objects Ø Can get/set attributes on the HttpServletRequest HttpServletRequest object • Similar to session attributes but call methods on a request object Ø Different lifetime • Parameters Ø Names and Values are Strings Strings Ø From forms or in URLs Ø Also in the HttpServletRequest HttpServletRequest object Apr 30, 2019 Sprenkle - CS335 17 Forwarding Requests from Servlet • HttpServletRequest HttpServletRequest ’s getRequestDispatcher getRequestDispatcher method Ø Returns a RequestDispatcher RequestDispatcher object The name of the resource to forward to request.getRequestDispatcher("welcome.jsp"). forward(request, response); • Can use RequestDispatcher RequestDispatcher ’s include include method similarly Apr 30, 2019 Sprenkle - CS335 18 9

  10. Adding a JSP to SurveyServlet SurveyServlet • Separate heavy lifting from the HTML • Think of JSP as a template Ø What is static about the response page? Ø What is dynamic? • Servlet will handle most of the work Look at code Apr 30, 2019 Sprenkle - CS335 19 Protecting JSPs • If there are JSPs that you don’t want users to be able to access directly by typing in the URL, put them in the WEB-INF directory Ø Web application server blocks access to the JSP Ø Don’t need code to check authorization again • Only get to JSP through a servlet that checks authorization • Forward requests from a servlet to the JSP by including WEB-INF in the URI Apr 30, 2019 Sprenkle - CS335 20 10

  11. Using the WEB-INF Directory • Example: User shouldn’t be able to access petResponse.jsp directly Apr 30, 2019 Sprenkle - CS335 21 Trick: Ternary Operator • Alternative if-then-else syntax • Returns a value • Example of assigning a variable the minimum: Condition minVal = (a < b ? a : b); Ø Assign minVal value a if condition is true , b if condition is false Apr 30, 2019 Sprenkle - CS335 22 11

  12. Ternary Operator in JSP • Allows for more concise code <input type=text name="username" value="<%= userName != null null ? userName : ""%>"> Returned if true Condition Returned if false Apr 30, 2019 Sprenkle - CS335 23 HttpServletRequest HttpServletRequest • getContextPath() Ø Returns the portion of the request URI that indicates the context of the request. • Example with various Request methods http://example.com:8080/app/dirpath/index.jsp?cat=2&cat=5 getContextPath() à "/app" getScheme() à "http" getServletPath() à "/dirpath" getServerName() à "example.com" getPathInfo() à "/index.jsp" getServerPort() à 8080 getParameter("cat") à "2" getParameterValues("cat") à {"2", "5"} Apr 30, 2019 Sprenkle - CS335 24 12

  13. Use in JSP <a href="<%=request.getContextPath()%>"> Main Page</a> Apr 30, 2019 Sprenkle - CS335 25 13

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend