Servlet Basics
- Standalone application: it is specific to one computer. So the resources and data of the application are specific to one computer. Eg: AWT frame windows, swing frame window application
- Two tier application:
- it need two computer connected in the network, so that application data and resources are visible in various computer of that network
- Ex: socket application, jdbc application
- To execute each java application one jvm is required to execute multiple java application parallel/ simultaneously from a computer.
- We can see multiple jvm running parallel in a computer to execute multiple java application simultaneously or parallel
- Web Application/Web site:
- it is a collection of web resources like Servlet, Jsp and Html program
- Such web resource is capable of generating one web page.
- Each web page will be display on browser window having hypertext.
- Hypertext: The text of the page that contains hyperlinks and allows non sequential navigation in web pages
- Web server:
- it is a piece of s/w to listen the client request continuously and passed request to appropriate web resource and manages web application
- it interacts with the client through a web browser
- it delivers the web pages to the client and to an application by using the web browser and the http protocols respectively
- it is the package of large number of programs installed on a computer connected to internet or intranet for downloading the requested files using FTP, serving email and building and publishing web pages.
- it work on a client server model
- A computer connected to the internet must have a server program
- While talking about java language then a web server is a server that is used to support the web component like the Servlet and Jsp
- Web server architecture:
- We can add and remove middle ware services on our application without effecting application logic
- Every web server supplies few services as build in middle ware service
- Responsibility of web server:
- listen to clients continuously to collect http request
- passes the http request to appropriate web resource program
- provides servlet container
- use built in middle ware service
- sends web resource generated result as http response back to browser in the form of web pages
- Web resource: it provides environment to execute dynamically and send result as web pages to browser window having http response. Eg: Tomcat, Resin, web server. Web resource is divides into two types ie., static and dynamic
- Static Web Resource: a web application is resource program that generates fixed content based static web pages. Eg: html program. Generally static web resource program are client side program
- Dynamic Web Resource: the web resources program that generates option contain based on the input values in the request based on the time of request generation eg: Servlet, Jsp. Generally dynamic web resource program are server side program
- Website: it is a collection of both static and dynamic web page.
- To develop and execute web application we need the following setup
- Browser s/w
- J2sdk s/w
- Technologies to develop static web resource program like html
- Technologies to develop dynamic web resource program like Servlet, jsp
- Web server / Http server s/w to manage web application
- Db s/w and more
- Setup required for develop web application
o Browser gives http request to the web resource of web application by typing url in the address bar
o Web server receive this request and passes the request to appropriate web resources of web application
o Logic in web resource program execute and result will be generated
o Web resource program interacts with db s/w if necessary
o Web resource program sends its result to web servers
o Web server sends that result as http response to the browser window in the form of web page
- Protocol: it is set of rules followed by two parties who want to participate in communication
- Http protocol: it is set of rules that are required to get communication between browser window and web server
- Common gateway interface (CGI)
- the common gateway interface, which is normally referred as CGI , was one of the practical techniques developed for creating dynamic content
- By using the cgi, a web server passes requests to an external program and after executing the program the content is sent to the client as the output.
- In cgi, when a server receives a request it creates a new process to run the cgi program, so creating a process for each request requires significant server resources and time which limits the number of requests that can be processed concurrently.
- Cgi application is platform dependent.
- There is no doubt that cgi played a major role in the explosion of the internet but its performance, scalability issues makes it less than optimal solutions.
- Java Servlet:
- it a generic server extension that means a java class can be loaded dynamically to expand the functionality of a server
- Servlets are used with web servers and run inside a jvm on the server so these are safe and portable.
- Unlike applet they do not requires support for java in the web browser
- Unlike cgi, Servlet don’t use multiple processes to handle separate request.
- Servlets can be handled by separate threads within the same process.
- Servlets are also potable and platform independent
- Features of Servlet 2.5
- This version has been released on September 26, 2005 by the Sun Microsystems.
- It is not necessary that all web servers and application servers support the features of Servlet 2.5.
- Still most of the popular containers like Tomcat 5.5 and JBoss 4.0 support Servlet 2.4.
- The list of the added features is given below:
o Dependency on J2SE 5.0: The minimum platform requirement for Servlet 2.5 is JDK 1.5. Servlet 2.5 can't be used in versions below than JDK1.5. All the available features of JDK1.5 like generics, autoboxing, and an improved for loop etc are guaranteed available to Servlet 2.5 programmers.
o Support For annotations: Annotations provide a mechanism for decorating java code constructs (classes, methods, fields, etc.) with metadata information. Annotations are mark code in such a way that code processors may alter their behavior based on the metadata information.
o Several web.xml convenience: Servlet 2.5 introduces several small changes to the web.xml file to make it more convenient to use. For example while writing a <filter-mapping>, we can now use an asterisk in a <servlet-name> which will represent all servlet as well as JSP.
o Previously we used to do
<filter-mapping>
<filter-name>FilterName</filter-name>
<servlet-name>FilterName</servlet-name>
</filter-mapping>
Now,
<filter-mapping>
<filter-name>FilterName</filter-name>
<servlet-name>*</servlet-name>
</filter-mapping>
o Previously in <servlet-mapping> or <filter-mapping> there used to be only one <url-pattern>, but now we can have multiple <url-pattern>, like
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/abc/*</url-pattern>
<url-pattern>/abc/*</url-pattern>
</servlet-mapping>
A part from these changes, many more facilities added in web.xml.
o A Handful of removed restrictions: Servlet 2.5 removed a few restrictions around error handling and session tracking. Now it has removed the restriction that the <error-page> could not call the setStatus() method to alter the error code that triggered them. In session tracking, Servlet 2.5 eased a rule that a servlet called by RequestDispatcher include() couldn't set response headers.
o Some edge case clarifications: The servlet 2.4 specification says that before calling request.getReader() we must call request.setCharacterEncoding(). However there is no such clarification given why it is so.
- Advantages of Servlet
- no cgi limitation
- abundant third party tools and web servers supporting Servlet
- access to entire family of java api
- reliable, better performance and scalability
- platform and server independent
- secure
- most servers allows automatic reloading Servlet by administrative action
- Server side programming
- All of us (or most of us) would have started programming in Java with the ever famous “Hello World!” program.
- If you can recollect, we saved this file with a .java extension and later compiled the program using javac and then executed the class file with java.
- Apart from introducing you to the language basics, the point to be noted about this program is that – “It is a client side program”.
- This means that you write, compile and also execute the program on a client machine (e.g. Your PC).
- No doubt, this is the easiest and fastest way to write, compile and execute programs.
- But, it has little practical significance when it comes to real world programming.
FAQ
Q) Why Server Side Programming?
A) Because ,
- Though it is technically feasible to implement almost any business logic using client side programs, logically or functionally it carries no ground when it comes to enterprise applications (e.g. banking, air ticketing, e-shopping etc.).
- To further explain, going by the client side programming logic; a bank having 10,000 customers would mean that each customer should have a copy of the program(s) in his or her PC which translates to 10,000 programs! In addition, there are issues like security, resource pooling, concurrent access and manipulations to the database which simply cannot be handled by client side programs.
- The answer to most of the issues cited above is – “Server Side Programming”. Figure-1 illustrates Server side architecture in the simplest terms.
- Advantages of Server Side Programs
- All programs reside in one machine called the Server.
- Any number of remote machines (called clients) can access the server programs.
- New functionalities to existing programs can be added at the server side which the clients’ can advantage without having to change anything from their side.
- Migrating to newer versions, architectures, design patterns, adding patches, switching to new databases can be done at the server side without having to bother about clients’ hardware or software capabilities.
- Issues relating to enterprise applications like resource management, concurrency, session management, security and performance are managed by service side applications.
- They are portable and possess the capability to generate dynamic and user-based content (e.g. displaying transaction information of credit card or debit card depending on user’s choice).
- Types of Server Side Programs
Active Server Pages (ASP)
Java Servlets
Java Server Pages (JSPs)
PHP
- The objective of server side programs:
- is to centrally manage all programs relating to a particular application (e.g. Banking, Insurance, e-shopping, etc).
- Clients with bare minimum requirement (e.g. Pentium II, Windows XP Professional, MS Internet Explorer and an internet connection) can experience the power and performance of a Server (e.g. IBM Mainframe, Unix Server, etc) from a remote location without having to compromise on security or speed.
- More importantly, server programs are not only portable but also possess the capability to generate dynamic responses based on user’s request.
- To develop and execute any basic java based web application
- take any browser window of browser s/w
- choose html program as client side web resource program
- choose any java based web server/application server s/w
- develop server side web resource program with Servlet / Jsp
- chose any database s/w