Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 http data.

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

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

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

5 Client-side state management Store data in the browser (client). Cookies are sometimes enabled for storing data in the browser. Cookies are limited

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

7 Cookies have a life! You can set the expiration date. You can delete your old cookies You can upload other peoples cookies!

8 What is the alternative to cookies? I need to have state management! URL Rewriting can help here! –http://www.docjava.com/index.html&2398rewn ri328urh32iuh –The junk at the end is a UUEncoded symbol that uniquely identifies the URL. –This encodes the session!

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

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

11 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

12 Servlet Dispatch How do we dispatch?

13 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

14 What is limited about switch? Can’t switch on a reference data type (i.e, string).

15 What use switch with a string? Parsers Servlet dispatch, which takes a command

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

17 What’s so great about a hashMap? Fast! O(constant). Cost – space! Trading space for time!!!!

18 How fast is the if-else clause is? If (bexp1) statement1 O(N) where N is the number of string.

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

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

21 DispatchProcessorClass Class DispatchProcessor { –doLogin(Request, Response) –doLogOut(Request, Response) –doCommand(String, Request, Response) Use reflection to map the String into a doCommand }

22 DispatchHub Servlet Single Entrypoint into the system Responsible to making sure that you have access. Message forwards to dispatch processor.

23 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….) –} }

24 I like if-else Compile-time check Type safe Reliable Not too slow for small numbers of strings. Hard to maintain.

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

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

27 Why not use the Command Pattern? New commandString(String s) { –Public void run() { Statements here…… –} };

28 How string dispatcher? Sd.add(new CommandString(“doLogin”){ –Public void run() { DispatchProcessong.doLogin(request, response); –} }); Sd.execute(“doLogin”);

29 Key Advantage Key in the HashMap is of any string that is unique. Not possible with reflection. “!@*#&@!*&” -> “doLogin”.

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

31 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

32 Final…Continued.. Invoke the compute method remotely and return the result.


Download ppt "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."

Similar presentations


Ads by Google