Objectives Servlets Review JSPs Web Application Organization Apr - - PDF document

objectives
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

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

slide-2
SLIDE 2

2

Review: Servlet Life Cycle

  • 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

Web Application Server

SurveyServlet Parameter Servlet

JAVASERVER PAGES (JSPS)

Apr 30, 2019 Sprenkle - CS335 4

slide-3
SLIDE 3

3

Discussion

Apr 30, 2019 Sprenkle - CS335 5

What made writing servlets difficult?

Motivation: JavaServer Pages (JSPs)

  • Simplify web application development
  • Separate UI from backend code

Ø Separate presentation layer

  • Difficult to write HTML in print statements

Apr 30, 2019 Sprenkle - CS335 6

Moving to here

slide-4
SLIDE 4

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 <%= %>

Apr 30, 2019 Sprenkle - CS335 8

<html> <body> <p> Hello! The time is now <%= new new java.util.Date() %> </p> </body> </html>

Expression Evaluated, turned into a String

slide-5
SLIDE 5

5

JSP Syntax: Scriptlet

Apr 30, 2019 Sprenkle - CS335 9

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

Example: SurveyServlet as a JSP

Apr 30, 2019 Sprenkle - CS335 10

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

slide-6
SLIDE 6

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

  • JSPs can access request parameters, session data

Apr 30, 2019 Sprenkle - CS335 12

These names must be used

slide-7
SLIDE 7

7

JSP Declarations

  • For instance variables and methods
  • We won’t do too much of this

Ø Let servlets do the work

Apr 30, 2019 Sprenkle - CS335 13

<%!

private ArrayList users; public void jspInit() { // on start up: set up } public void jspDestroy() { // on shut down: clean up }

%>

Web Application Architecture

Apr 30, 2019 Sprenkle - CS335 14

JSP Java Servlets Java Classes (Model) Client Server-side DataStore HTML With Dynamic Parts

  • Heavy lifting of requests
  • Forward to JSPs
slide-8
SLIDE 8

8

Communicating Between Servlets and JSPs: Login Example

Apr 30, 2019 Sprenkle - CS335 15

Login Servlet login.jsp welcome.jsp

Client Server-side

  • Check user name/password
  • Set authenticated

authenticated or error error attribute

  • Forward to welcome.jsp or login.jsp
  • Form to

login 1 2 User makes login request

Communicating Between Servlets and JSPs: Login Example

Apr 30, 2019 Sprenkle - CS335 16

Login Servlet login.jsp welcome.jsp

Client Server-side

  • Check user name/password
  • Set authenticated

authenticated or error error attribute

  • Forward to welcome.jsp or login.jsp
  • Display

error message

  • Form to

login

  • Check authenticated

authenticated

  • User’s Options

2 User makes login request error success

slide-9
SLIDE 9

9

Communicating Between JSPs and Servlets

  • Attributes

Ø Name/Value pairs Ø Values are Objects Ø Can get/set attributes on the HttpServletRequest HttpServletRequest

  • bject
  • Similar to session attributes but call methods on a request
  • bject

Ø 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

Login Servlet welcome.jsp

Forwarding Requests from Servlet

  • HttpServletRequest

HttpServletRequest’s getRequestDispatcher getRequestDispatcher method

Ø Returns a RequestDispatcher RequestDispatcher object

  • Can use RequestDispatcher

RequestDispatcher’s include include method similarly

Apr 30, 2019 Sprenkle - CS335 18

request.getRequestDispatcher("welcome.jsp"). forward(request, response);

The name of the resource to forward to

slide-10
SLIDE 10

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

Apr 30, 2019 Sprenkle - CS335 19

Look at code

Apr 30, 2019 Sprenkle - CS335 20

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

slide-11
SLIDE 11

11

Using the WEB-INF Directory

  • Example: User shouldn’t be able to access

petResponse.jsp directly

Apr 30, 2019 Sprenkle - CS335 21 Apr 30, 2019 Sprenkle - CS335 22

Trick: Ternary Operator

  • Alternative if-then-else syntax
  • Returns a value
  • Example of assigning a variable the minimum:

Ø Assign minVal value a if condition is true, b if condition is false

minVal = (a < b ? a : b); Condition

slide-12
SLIDE 12

12

Apr 30, 2019 Sprenkle - CS335 23

Ternary Operator in JSP

  • Allows for more concise code

<input type=text name="username" value="<%= userName != null null ? userName : ""%>"> Condition Returned if true Returned if false

Apr 30, 2019 Sprenkle - CS335 24

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

getScheme() à "http" getServerName() à "example.com" getServerPort() à 8080 getContextPath() à "/app" getServletPath() à "/dirpath" getPathInfo() à "/index.jsp" getParameter("cat") à "2" getParameterValues("cat") à {"2", "5"}

slide-13
SLIDE 13

13

Apr 30, 2019 Sprenkle - CS335 25

Use in JSP

<a href="<%=request.getContextPath()%>"> Main Page</a>