P. 1
Advantage of Servlets

Advantage of Servlets

|Views: 1|Likes:
Published by jarsuy

More info:

Published by: jarsuy on Aug 30, 2011
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less





Advantage of Servlets Over "Traditional" CGI?

Java servlets are more efficient, easier to use, more powerful, more portable, and cheaper than traditional CGI and than many alternative CGI-like technologies. (More importantly, servlet developers get paid more than Perl programmers :-).

Efficient. With traditional CGI, a new process is started for each HTTP request. If the CGI program does a relatively fast operation, the overhead of starting the process can dominate the execution time. With servlets, the Java Virtual Machine stays up, and each request is handled by a lightweight Java thread, not a heavyweight operating system process. Similarly, in traditional CGI, if there are N simultaneous request to the same CGI program, then the code for the CGI program is loaded into memory N times. With servlets, however, there are N threads but only a single copy of the servlet class. Servlets also have more alternatives than do regular CGI programs for optimizations such as caching previous computations, keeping database connections open, and the like. Convenient. Hey, you already know Java. Why learn Perl too? Besides the convenience of being able to use a familiar language, servlets have an extensive infrastructure for automatically parsing and decoding HTML form data, reading and setting HTTP headers, handling cookies, tracking sessions, and many other such utilities. Powerful. Java servlets let you easily do several things that are difficult or impossible with regular CGI. For one thing, servlets can talk directly to the Web server (regular CGI programs can't). This simplifies operations that need to look up images and other data stored in standard places. Servlets can also share data among each other, making useful things like database connection pools easy to implement. They can also maintain information from request to request, simplifying things like session tracking and caching of previous computations. Portable. Servlets are written in Java and follow a well-standardized API. Consequently, servlets written for, say I-Planet Enterprise Server can run virtually unchanged on Apache, Microsoft IIS, or WebStar. Servlets are supported directly or via a plugin on almost every major Web server. Inexpensive. There are a number of free or very inexpensive Web servers available that are good for "personal" use or low-volume Web sites. However, with the major exception of Apache, which is free, most commercial-quality Web servers are relatively expensive. Nevertheless, once you have a Web server, no matter the cost of that server, adding servlet support to it (if it doesn't come preconfigured to support servlets) is generally free or cheap.

HTTP Redirectives Servlets take an HTTP request at the server do some processing and send back HTTP response to the client. There are two most widely used HTTP directives. GET and POST. A GET request is made by a client to retrieve a resources from the server. This resource could be a document, an image or even the resultset from a database query. The GET can also pass additional information to the server encoded as name/value pairs on the HTTP request. This additional information could be authentication data required to get a connection to a perform a database query.

nor any other information from your machine. once and only once.POST on the other hand. The server creates a new ID for you in Amazon's database and then sends name-value pairs to your machine in the header for the Web page it sends. For example. is geared specifically towards sending large amounts of information. There are other pieces of information that the server can send with the name-value pair. your browser sends a request to the Web site for the page (see How Web Servers Work for a discussion). if you type the URL http://www. to the server. it will look on your machine for a cookie file that Amazon has set. . A cookie is a piece of text that a web server can store on a user’s hard disk. It is not a program. That is all cookie data is. Amazon knows that you have not visited before. Another is a path (so that the site can associate different cookie values with different parts of the site). your browser will send all of the name-value pairs in the file to Amazon's server along with the URL. A name-value pair is simply a named piece of data.amazon. If name-value pairs are received. When the browser does this. If it finds no cookie file.com into your browser. Your machine stores the name-value pairs on your hard disk. we want the cookie to at least store a unique session-id for the client browser. nor anything else on your machine. It cannot retrieve information from other cookie files. The Web server can change name-value pairs or add new pairs whenever you visit the site and request a page. Cookies A cookie is a small piece of information passed by the web server for storage at the client. A Web site can only receive the data it has stored on your machine. possibly megabytes. your browser will contact Amazon's server and request its home page. and later it receives it back. The pieces of information are stored as name-value pairs. Amazon can use them. In terms of anonymous session tracking. you send it and your’e done. If no name-value pairs are received. cookie data is simply name-value pairs stored on your hard disk by a Web site. and it cannot do anything. Cookies allow a website to store information on a user’s machine and later retrieve it. The data moves in the following manner: • • • • • If you type the URL of a Web site into your browser. Amazon's Web server receives the cookie data and the request for a page. The Web site stores the data. If it finds an Amazon cookie file. It cannot look at any other cookie. A web site can retrieve only the information that it has placed on your machine. One of these is an expiration date. How Does Cookie Data Move? As you saw in the previous section. Hence a post request is literally a post. it will send no cookie data.

getMaxInactiveInterval() What is JSP JSP(Java Server Pages) is a technology that lets you mix regular. getCreationTime(). There session objects have a built in data structure that let you store any number of keys and associated values. For JSP this process runs software known as Tomcat which takes the JSP page and converts the entire page into a Java programme which is then compiled. they’re just automatically associated with therequests.You have control over this process. It is done by calling the getSession() method of HTTPServletRequest. In particular when you are doing on-line shopping. HTTP Session Tracking There are a number of problems arise from the fact that HTTP is a stateless protocol. You can set an option in your browser so that the browser informs you every time a site sends name-value pairs to you. Although the data that is explicitly associated with a session is a part that we need most. strong information in a session and discarding completed or abandon sessions. the request is transferred from the normal WWW . at first glance. looking up information associated with a session. this makes the development process very simple and its maintenance also becomes very easy. JSP is handled by a separate server process. There are three typical solutions to this problem. The raw HTML in the original page is wrapped in Java output methods. whereas ASP and PHP are processed by a server plug-in. creating a new session object when necessary. getLastAccessedTime(). On requesting a JSP page. • Cookies • URL Rewriting • Hidden form fields However servlet provides an outstanding technical solution: the HTTPSession API. associated with the current request. JSP enable the developers to directly insert java code into jsp file. How JSP works JSP pages are. JSP provides a facility where you can write server-side scripted pages using the full power of Java programming language and the rich set of class libraries associated with JAVA. isNew(). You can use getValue(“key”) to look up a previously stored value. static HTML with dynamically generated HTML. The difference is that. Using sessions in servlets is quite straightforward and involves looking up the session object. To specify information you use putValue method. HTTPSession objects live on the server. it is real annoyance that the web server can’t easily remember previous transactions. there are some other pieces of information that are sometimes useful as well. rather similar to ASP and PHP pages in that special pseudotags are used to mark out parts of otherwise normal HTML pages that are to handled specially.getId(). You can then accept or deny the values. This makes applications like shopping cart very problematic.

Aside from the regular HTML three main types of JSP constructs that you embed in a page: scripting elements. directives let you control the overall structures of the servelet. Scripting elements lets you specify java code that will become part of the resultant servlet.g. and action control the behaviour of the JSP engine.server (e. Recompilation takes place if the last modification date stamp on the JSP page is more recent than the last modification date stamp on any cached compiled version. directives and actions. JSP lets you separate the dynamic part of your pages from the static HTML. . Apache) to Tomcat which first checks whether it has cached an already compiled version of the Java programme associated with the page.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->