+ All Categories
Home > Documents > What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A...

What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A...

Date post: 21-Jan-2016
Category:
Upload: susan-beverley-boone
View: 222 times
Download: 0 times
Share this document with a friend
Popular Tags:
32
What is a Servlet? • Java Program that runs in a Java web server and conforms to the servlet api. • A program that uses class library that decodes and encodes http data.
Transcript
Page 1: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

What is a Servlet?

• Java Program that runs in a Java web server and conforms to the servlet api.

• A program that uses class library that decodes and encodes http data.

Page 2: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

What is HTTP data?

• Queries (get http 1.1 file name)

• Responses (i.e., html output to a printer made from a socket output stream).

• A web server does this! Webservers manage the http query and synthesize a response.

Page 3: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

What kind of features can I get from the servlet API?

• Object oriented interface to the http servlet requests and responses.

• A nice way to parse input parameters from forms written in html.

• State management.

Page 4: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

What is state management?

• State management enables the implementation of a finite state machine that is associate with a session.

• Http is inherently without memory.

• So how do store the state without memory?

Page 5: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

Client-side state management

• Store data in the browser (client).

• Cookies are sometimes enabled for storing data in the browser.

• Cookies are limited

Page 6: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

Whats a cookie?

• Data stored by client tranmitted as a part of the http stream from web server.

• Limits of a cookie:– Size (20 kB) per cookie– Number (20 cookies per domain)

• Not all browsers accept cookies

• Security can be a problem….

Page 7: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

Cookies have a life!

• You can set the expiration date.

• You can delete your old cookies

• You can upload other peoples cookies!

Page 8: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

What is the alternative to cookies?

• I need to have state management!

• URL Rewriting can help here!– http://www.docjava.com/

index.html&2398rewnri328urh32iuh– The junk at the end is a UUEncoded symbol

that uniquely identifies the URL.– This encodes the session!

Page 9: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

What do we do?

• Cookies

• URL rewriting– Visibly alters the URL and makes it impossible

to bookmark.

• HttpSession enable automatic changeover from URL rewriting to Cookies, so you don’t have to decide!

Page 10: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

A good feature!

• HttpSession class– Enable session management– Automatically change over from cookies to url

rewriting, when required.– Give an object oriented interface to session

management– Allows me to pretend to write Serializble

objects to the client.

Page 11: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

Why do I need a servlet API

• Using sockets you can build http processor!

• You don’t!

• But it makes it easier!

• Why?– Because of Services and features that are

provided

Page 12: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

Servlet Dispatch

How do we dispatch?

Page 13: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

What is dispatch?

• Multi-way change in the flow of control.

• If (bexp1) statement

• Else if (bexp2) statement2

• Switch(int, char, short,) // very efficient but very limited

Page 14: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

What is limited about switch?

• Can’t switch on a reference data type (i.e, string).

Page 15: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

What use switch with a string?

• Parsers

• Servlet dispatch, which takes a command

Page 16: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

How can a switch with string?

• Associate the string with a number.

• String s[] = {“s1”, “s2”,…};

• S[0] = “s1”.

• The numbers correspond to tokens.

• So use a HashMap.

Page 17: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

What’s so great about a hashMap?

• Fast! O(constant).

• Cost – space!

• Trading space for time!!!!

Page 18: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

How fast is the if-else clause is?

• If (bexp1) statement1

• O(N) where N is the number of string.

Page 19: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

Another way to dispatch..

• Reflection

• How fast is reflection?

• O(C) because a hash map is used to map the string into a method.

• It has no type safety!

• No compile time checking!

Page 20: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

How do I build a parser based reflection system?

• Use a class name and instance the class then invoke some methods.– Bad because you are making new instances

dynamically and that takes both speed and time.

• Use a an existing instance (singleton pattern) and invoke a method on that!– Use the command to map to a method– Request and response are the args.

Page 21: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

DispatchProcessorClass

• Class DispatchProcessor {– doLogin(Request, Response)– doLogOut(Request, Response)– doCommand(String, Request, Response)

• Use reflection to map the String into a doCommand

• }

Page 22: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

DispatchHub

• Servlet

• Single Entrypoint into the system

• Responsible to making sure that you have access.

• Message forwards to dispatch processor.

Page 23: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

What is the alternative?

• Class dispatchHub…{– doGet(request, response) {

• String command c =…

• If (c.equals(doLogin)) doLogin(…)

• Else (c.equals(doLogout)) doLogout(…)

• Else (c.equals….)

– }

• }

Page 24: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

I like if-else

• Compile-time check

• Type safe

• Reliable

• Not too slow for small numbers of strings.

• Hard to maintain.

Page 25: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

What is wrong with if-else?

• To add a command:– 1. expand the if-else clause by 1 string.– 2. You have to add a method for the command– 3. You have to add a command to you form– You have to be consistent in 3 places.

Page 26: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

What is so good about a reflection based dispatch?

– 1. Place a command the form– 2. Place a method in the dispatch processor.

• Not type save

• No run-time check

• A little slower?

Page 27: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

Why not use the Command Pattern?

• New commandString(String s) {– Public void run() {

• Statements here……

– }

• };

Page 28: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

How string dispatcher?

• Sd.add(new CommandString(“doLogin”){– Public void run() {

DispatchProcessong.doLogin(request, response);

– }

• });

• Sd.execute(“doLogin”);

Page 29: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

Key Advantage

• Key in the HashMap is of any string that is unique.

• Not possible with reflection.

• “!@*#&@!*&” -> “doLogin”.

Page 30: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

Moving classes is hard!

• examples.stringswitch.SwitchTest

• Could be moved to Utils or Junit

• If class names reside in a xml file or a HTML file, then these must be changed too!

Page 31: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

CR320 Final

• 1. Select a jar file in a directory

• 2. List the classes in the jar files that implement the Computable interface

• 3. Require the user to select one of those classes

• 4. Use the ByteCodeClassLoader to deploy the class to a compute server

Page 32: What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.

Final…Continued..

• Invoke the compute method remotely and return the result.


Recommended