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

Post on 21-Jan-2016

222 views 0 download

Tags:

transcript

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.

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.

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.

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?

Client-side state management

• Store data in the browser (client).

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

• Cookies are limited

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

Cookies have a life!

• You can set the expiration date.

• You can delete your old cookies

• You can upload other peoples cookies!

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!

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!

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.

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

Servlet Dispatch

How do we dispatch?

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

What is limited about switch?

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

What use switch with a string?

• Parsers

• Servlet dispatch, which takes a command

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.

What’s so great about a hashMap?

• Fast! O(constant).

• Cost – space!

• Trading space for time!!!!

How fast is the if-else clause is?

• If (bexp1) statement1

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

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!

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.

DispatchProcessorClass

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

• Use reflection to map the String into a doCommand

• }

DispatchHub

• Servlet

• Single Entrypoint into the system

• Responsible to making sure that you have access.

• Message forwards to dispatch processor.

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….)

– }

• }

I like if-else

• Compile-time check

• Type safe

• Reliable

• Not too slow for small numbers of strings.

• Hard to maintain.

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.

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?

Why not use the Command Pattern?

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

• Statements here……

– }

• };

How string dispatcher?

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

DispatchProcessong.doLogin(request, response);

– }

• });

• Sd.execute(“doLogin”);

Key Advantage

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

• Not possible with reflection.

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

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!

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

Final…Continued..

• Invoke the compute method remotely and return the result.