15
Server Side: JSP and Java Servlets Chris North cs3724: HCI

Server Side: JSP and Java Servlets

Embed Size (px)

DESCRIPTION

Server Side: JSP and Java Servlets. Chris North cs3724: HCI. Client-side. Method: Download and run program on client Examples: Java applets, javascript, activeX, css, plugins, Good for: dynamic interaction, Reducing server load Animations, processing intensive - PowerPoint PPT Presentation

Citation preview

Page 1: Server Side: JSP and Java Servlets

Server Side:JSP and Java Servlets

Chris North

cs3724: HCI

Page 2: Server Side: JSP and Java Servlets

Client-side

• Method:• Download and run program on client

• Examples:• Java applets, javascript, activeX, css, plugins,

• Good for:• dynamic interaction,

• Reducing server load

• Animations, processing intensive

• Server security, crash protection

Page 3: Server Side: JSP and Java Servlets

Server-side

• Method:• Dynamically generated web pages on server• Download resulting html page to client

• Examples:• JSP, Java Servlets, php, asp, cgi, perl, includes, C

• Good for:• database interaction• Broad client compatibility, customize to browser/OS• Data synchro, collaborative apps• Access control• Small downloads• No installs• Code security, hack protection• Client security• Screen scraping• simple interaacations

Page 4: Server Side: JSP and Java Servlets

Example: Data-Driven Websites

• Websites that provide access to:• Lots o data

• Dynamic data

• Customized views of data

• E.g. ebay.com

• Scripts map data to html

Database

html

Page 5: Server Side: JSP and Java Servlets

Server-side processing

Client (Browser)

1. User click

5. Display html

Server (Web server)

2. Receive HTTP request

3. Execute script

4. Send html resultResponsehtml page

URL requestscript.jsp

html

Script.jsp

Page 6: Server Side: JSP and Java Servlets

Example

Books.exe Cart.exe Purchase.exe

•Shows books list•Add to cart

•View cart button

•Displays cart contents•Delete items

•Purchase button

•Charges credit card•Displays ‘thankyou’

•Link back to books

Amazon.com server

Page 7: Server Side: JSP and Java Servlets

Problems

• Many simultaneous users

• No state data maintained

• Client not in sync with server (e.g. back button)

Books.exe Cart.exe Purchase.exe

Page 8: Server Side: JSP and Java Servlets

Different than GUI programming!

• One user (per executable)

• ‘Global’ state data

• Client = server

Shopping.exe

Books windowCart window

Purchase window

Page 9: Server Side: JSP and Java Servlets

Problems

• Myscript.exe starts from scratch each time• Each page request is completely independent

• No state data maintained between requests» Who is the user?

» What is his data?

» Where is he in the ‘application’?

• How to maintain state info across page accesses?• Pile all state data into hidden form items

• Cookies

• Store state data in a database, index by unique user id

Page 10: Server Side: JSP and Java Servlets

Java Servlets

• Html:• Link: URL/servlet

• Form post

• Servlet• Init()

• Destroy()

• doGet()

• doPost()

Page 11: Server Side: JSP and Java Servlets

example

Out.println(“<html><body>”);

Out.println(“<p>hello world”);

Out.println(new java.util.Date( ));

Out.println(“</body></html>”);

• Browser recieves:<html><body>

Hello world 2:00pm, March 28, 2002

</body></html>

Page 12: Server Side: JSP and Java Servlets

Java Server Pages (JSP)

• Html + code• New tags <% %> and <%= %>• Myscript.jsp:<html><body>Hello world <%= new java.util.Date() %></body></html>

• Client receives:<html><body>Hello world 2:00pm, March 28, 2002</body></html>

Page 13: Server Side: JSP and Java Servlets

Processing form input

• Global object: request, session

<% session.putValue(“username”, request.getParameter(“UserName”)) %>

<html><body>

Thanks for giving us your name, <%= session.getValue(“username”) %>

</body></html>

Page 14: Server Side: JSP and Java Servlets

Session Data

• Global object: session<html><body>

Hello,

<%= session.getValue(“username”) %>

</body></html>

• Client receives:<html><body>

Hello, Chris

</body></html>

Page 15: Server Side: JSP and Java Servlets

JSP objects

• Application

• Config

• Out

• Request

• Response

• Session