1. Short Question
Q1. What are internal Servlets?
Ans:
2. What is Session Tracking?
Ans: Session tracking is the capability of a server to maintain the current state of a
single client’s sequential requests.
Session tracking methods:
1. User authorization
2. Hidden fields
3. URL rewriting
4. Cookies
5. Session tracking API
The first four methods are traditionally used for session tracking in all the server-side
technologies. The session tracking API method is provided by the underlying technology (java
servlet or PHP or likewise). Session tracking API is built on top of the first four methods.
Session tracking method is built on the first four methods.
User Authorization
In the use of web application users can identified in different ways. The basic concept of this
method is that the user can provide username and password to login to the application. Based on
the username and password the user can be identified and the session can be maintained.
Hidden Fields
Hidden fields can be inserted in the web pages and information can be sent to the server for
session tracking. Hidden fields can be created by using the keyword “hidden”. These fields are
viewed using view source option from the browsers, but it cannot be visible directly to the user.
This type doesn’t need any special configuration from the browser of server and by default
available to use for session tracking. This cannot be used for session tracking when the
conversation included static resources link html pages.
Eg: - <INPUT TYPE=”hidden” NAME=”technology” VALUE=”servlet”>
2. URL Rewriting
When a request is create, additional parameter is added with the URL. In general appended
additional parameter will be sessionid or sometimes the userid. It helps to track the session. This
type of session tracking doesn’t need any special support from the browser.
Disadvantage:-Implementing this type of session tracking is tedious. We need to keep track of
the parameter as a chain link until the conversation completes and also should make sure that, the
parameter doesn’t clash with other application parameters.
Eg: – Original URL is: – http://server:port/servlet/ServletName.
Rewritten URL is:-http://server:port/servlet/ServletName?sessionid=7456.
Cookies
In session tracking methods cookies are mostly used technology. Cookie is a key
value pair of information, sent by the server to the browser. This should be saved
by the browser in its space in the client computer. Whenever the browser sends a
request to that server it sends the cookie along with it. Then the server can identify
the client using the cookie. The source code of creating a cookie is given below,
1
2
Cookie cookie = new Cookie(“userID”,”7456”);
res.addCookie(cookie);
Using cookies the session tracking is easy to implement and maintained.
Disadvantage: – The users can opt to disable cookies using their browser
preferences. In such case, the browser will not save the cookie at client computer
and session tracking fails.
Session tracking API
Session tracking API is built on top of the first four methods. This is inorder to
help the developer to minimize the overhead of session tracking. In this type of
session tracking is provided by the underlying technology. Let’s take the java
servlet example. Then, the servlet container manages the session tracking task and
the user need not do it explicitly using the java servlets. This is the best of all
methods, because all the management and errors related to session tracking will be
taken care of by the container itself.
3. Every client of the server will be mapped with a javax.servlet.http.HttpSession object. Java
servlets can use the session object to store and retrieve java objects across the session. Session
tracking is at the best when it is implemented using session tracking API.
We have to destroy the session by calling the invalidate () method on the session object.
This type of session tracking doesn’t need any special support from the browser. Disadvantage
is, implementing this type of session tracking is tedious. We need to keep track of the parameter
as a chain link until the conversation completes and also should make sure that, the parameter
doesn’t clash with other application parameters.
4. Cookies
Cookies are the mostly used technology for session tracking. Cookie is a key value pair of
information, sent by the server to the browser. This should be saved by the browser in its space
in the client computer. Whenever the browser sends a request to that server it sends the cookie
along with it. Then the server can identify the client using the cookie.
In java, following is the source code snippet to create a cookie:
Cookie cookie = new Cookie(“userID”, “7456″);
res.addCookie(cookie);
Session tracking is easy to implement and maintain using the cookies. Disadvantage is that, the
users can opt to disable cookies using their browser preferences. In such case, the browser will
not save the cookie at client computer and session tracking fails.
5. Session tracking API
Session tracking API is built on top of the first four methods. This is inorder to help the
developer to minimize the overhead of session tracking. This type of session tracking is provided
4. by the underlying technology. Lets take the java servlet example. Then, the servlet container
manages the session tracking task and the user need not do it explicitly using the java servlets.
This is the best of all methods, because all the management and errors related to session tracking
will be taken care of by the container itself.
Every client of the server will be mapped with a javax.servlet.http.HttpSession object. Java
servlets can use the session object to store and retrieve java objects across the session. Session
tracking is at the best when it is implemented using session tracking api.
3. Why http is a stateless protocol?
Ans: HTTP is a "stateless" protocol because even if a user sends a sequence of requests from the same
browser, the server is not able to tell that those are from the same user.
4. What is container and its role in EJB?
An Enterprise JavaBeans (EJB) container provides a run-time environment for enterprise beans
within the application server. The container handles all aspects of an enterprise bean's operation
within the application server and acts as an intermediary between the user-written business logic
within the bean and the rest of the application server environment.
The EJB container provides many services to the enterprise bean, including the following:
Beginning, committing, and rolling back transactions as necessary.
Maintaining pools of enterprise bean instances ready for incoming requests and moving
these instances between the inactive pools and an active state, ensuring that threading
conditions within the bean are satisfied.
Most importantly, automatically synchronizing data in an entity bean's instance variables
with corresponding data items stored in persistent storage.
5. Write down the three security features in EJB.
6. What is Web server?
Ans: A Web server is a program that, using the client/server model and the World Wide Web's
Hypertext Transfer Protocol ( HTTP ), serves the files that form Web pages to Web users (whose
computers contain HTTP clients that forward their requests).
Every computer on the Internet that contains a Web site must have a Web server program. Two leading
Web servers are Apache , the most widely-installed Web server, and Microsoft's Internet Information
5. Server ( IIS ). Other Web servers include Novell's Web Server for users of its NetWare operating system
and IBM's family of Lotus Domino servers, primarily for IBM's OS/390 and AS/400 customers.
A Web server is a system that delivers content or services to end users over the Internet. A Web
server consists of a physical server, server operating system (OS) and software used to facilitate
HTTP communication.
A Web server is also known as an Internet server.
1. Apache web server - the HTTP web server
Free and the most popular web server in the world developed by the Apache Software Foundation.
Apache web server is an open source software and can be installed and made to work on almost all
operating systems including Linux, Unix, Windows, FreeBSD, Mac OS X and more.
2. Apache Tomcat
The Apache Tomcat has been developed to support servlets and JSP scripts. Though it can serve
as a standalone server, Tomcat is generally used along with the popular Apache HTTP web
server or any other web server. Apache Tomcat is free and open source and can run on different
operating systems like Linux, Unix, Windows, Mac OS X, Free BSD.
Apache Tomcat
Microsoft's Internet Information Services (IIS) Windows
Server
IIS Windows Web Server has been developed by the software giant, Microsoft. It offers higher
levels of performance and security than its predecessors. It also comes with a good support
from the company and is the second most popular server on the web.
7. Write any four popular Domain Name Extensions.
8. What is Telnet?
Telnet is a user command and an underlying TCP/IP protocol for accessing remote computers.
9. Write any two HTML Tags for Form object.
10. What is XSL?
6. XSL is a language for expressing style sheets. An XSL style sheet is, like with CSS, a file that describes how
to display an XML document of a given type. XSL shares the functionality and is compatible with CSS2
11. What is the difference between HttpServlet and GenericServlet?
12. What is differenence between ServletContext and ServletConfig?
13. What are JavaScript types?
14. What is web application?
Ans; A web application is an application accessible from the web. A web application is
composed of web components like Servlet, JSP, Filter etc. and other components such as HTML.
The web components typically execute in Web Server and respond to HTTP request.
Long Questions
1. What is Three-Tier Architecture?
2. How a scripting language is different from a high level language?
3. Why is HTML is called a Markup Language? What are two types of tags? Explain each
with example.
4. What are two types of DTD? Explain.
5. What are the different types of dialog boxes available in Java Script. Explain?
6. What are JAVA servlets and how they are created?
Ans: Java Servlets are server-side Java program modules that process and answer client
requests and implement the servlet interface. It helps in enhancing Web server functionality
with minimal overhead, maintenance and support.
A servlet acts as an intermediary between the client and the server. As servlet modules run on
the server, they can receive and respond to requests made by the client. Request and response
objects of the servlet offer a convenient way to handle HTTP requests and send text data
back to the client.
Since a servlet is integrated with the Java language, it also possesses all the Java features
such as high portability, platform independence, security and Java database connectivity.
Servlet can be described in many ways, depending on the context.
Servlet is a technology i.e. used to create web application.
Servlet is an API that provides many interfaces and classes including documentations.
7. Servlet is an interface that must be implemented for creating any servlet.
Servlet is a class that extend the capabilities of the servers and respond to the incoming
request. It can respond to any type of requests.
Servlet is a web component that is deployed on the server to create dynamic web page.
Steps to create a servlet
Following 6 steps are followed to create a servlet in any server.
The steps are as follows:
1. Create a directory structure
2. Create a Servlet
3. Compile the Servlet
4. Create a deployment descriptor
5. Start the server and deploy the project
6. Access the servlet
The servlet can be created by three ways:
1. By implementing Servlet interface,
2. By inheriting GenericServlet class, (or)
3. By inheriting HttpServlet class
The mostly used approach is by extending HttpServlet because it provides http request specific
method such as doGet(), doPost(), doHead() etc.
1)Create a directory structures
The directory structure defines that where to put the different types of files so that web
container may get the information and respond to the client.
The Sun Microsystem defines a unique standard to be followed by all the server vendors. Let's
see the directory structure that must be followed to create the servlet.
8. 2)Create a Servlet
There are three ways to create the servlet.
1. By implementing the Servlet interface
2. By inheriting the GenericServlet class
3. By inheriting the HttpServlet class
The HttpServlet class is widely used to create the servlet because it provides methods to handle http
requests such as doGet(), doPost, doHead() etc.
3)Compile the servlet
For compiling the Servlet, jar file is required to be loaded. Different Servers provide different jar
files:
Jar file Server
1) servlet-api.jar Apache Tomacat
2) weblogic.jar Weblogic
3) javaee.jar Glassfish
9. 4) javaee.jar JBoss
Two ways to load the jar file
1. set classpath
2. paste the jar file in JRE/lib/ext folder
Put the java file in any folder. After compiling the java file, paste the class file of servlet in
WEB-INF/classes directory.
4)Create the deployment descriptor (web.xml file)
The deployment descriptor is an xml file, from which Web Container gets the information
about the servet to be invoked.
The web container uses the Parser to get the information from the web.xml file. There are many
xml parsers such as SAX, DOM and Pull.
There are many elements in the web.xml file. Here is given some necessary elements to run the
simple servlet program.
web.xml file
1. <web-app>
2.
3. <servlet>
4. <servlet-name>sonoojaiswal</servlet-name>
5. <servlet-class>DemoServlet</servlet-class>
6. </servlet>
7.
8. <servlet-mapping>
9. <servlet-name>sonoojaiswal</servlet-name>
10. <url-pattern>/welcome</url-pattern>
11. </servlet-mapping>
12.
13. </web-app>
Description of the elements of web.xml file
There are too many elements in the web.xml file. Here is the illustration of some elements that is
used in the above web.xml file. The elements are as follows:
10. <web-app> represents the whole application.
<servlet> is sub element of <web-app> and represents the servlet.
<servlet-name> is sub element of <servlet> represents the name of the servlet.
<servlet-class> is sub element of <servlet> represents the class of the servlet.
<servlet-mapping> is sub element of <web-app>. It is used to map the servlet.
<url-pattern> is sub element of <servlet-mapping>. This pattern is used at client side to invoke the
servlet.
5)Start the Server and deploy the project
To start Apache Tomcat server, double click on the startup.bat file under apache-tomcat/bin
directory.
One Time Configuration for Apache Tomcat Server
You need to perform 2 tasks:
1. set JAVA_HOME or JRE_HOME in environment variable (It is required to start server).
2. Change the port number of tomcat (optional). It is required if another server is running on same
port (8080).
1) How to set JAVA_HOME in environment variable?
To start Apache Tomcat server JAVA_HOME and JRE_HOME must be set in Environment
variables.
Go to My Computer properties -> Click on advanced tab then environment variables -> Click on
the new tab of user variable -> Write JAVA_HOME in variable name and paste the path of jdk
folder in variable value -> ok -> ok -> ok.
Go to My Computer properties:
Click on advanced system settings tab then environment variables:
11. Click on the new tab of user variable or system variable:
Write JAVA_HOME in variable name and paste the path of jdk folder in variable value:
There must not be semicolon (;) at the end of the path.
After setting the JAVA_HOME double click on the startup.bat file in apache tomcat/bin.
Note: There are two types of tomcat available:
1. Apache tomcat that needs to extract only (no need to install)
2. Apache tomcat that needs to install
It is the example of apache tomcat that needs to extract only.
Now server is started successfully.
2) How to change port number of apache tomcat
Changing the port number is required if there is another server running on the same system with
same port number.Suppose you have installed oracle, you need to change the port number of
apache tomcat because both have the default port number 8080.
Open server.xml file in notepad. It is located inside the apache-tomcat/conf directory . Change
the Connector port = 8080 and replace 8080 by any four digit number instead of 8080. Let us
replace it by 9999 and save this file.
5) How to deploy the servlet project
12. Copy the project and paste it in the webapps folder under apache tomcat.
But there are several ways to deploy the project. They are as follows:
By copying the context(project) folder into the webapps directory
By copying the war folder into the webapps directory
By selecting the folder path from the server
By selecting the war file from the server
Here, we are using the first approach.
You can also create war file, and paste it inside the webapps directory. To do so, you need to use
jar tool to create the war file. Go inside the project directory (before the WEB-INF), then write:
1. projectfolder> jar cvf myproject.war *
Creating war file has an advantage that moving the project from one location to another takes
less time.
6) How to access the servlet
Open broser and write http://hostname:portno/contextroot/urlpatternofservlet. For example:
1. http://localhost:9999/demo/welcome
7. Explain the attributes Cell padding, Cell spacing, Colspan, Rowspan in the table tag of
HTML.
8. What is concept of a “session” in web programming?
9. What are the advantages and disadvantages of using cookies for session management?
10. What are the requirements to run the application based on java servlet? Explain the life
cycle methods of a servlet.
13. 11. What is a database URL? Discuss the process to connect a database from the Servlet.
12.
Session Tracking in Servlets
Session simply means a particular interval of time.
Session Tracking is a way to maintain state (data) of an user. It is also known as session
management in servlet.
Http protocol is a stateless so we need to maintain state using session tracking techniques. Each
time user requests to the server, server treats the request as the new request. So we need to
maintain the state of an user to recognize to particular user.
HTTP is stateless that means each request is considered as the new request
Why use Session Tracking?
To recognize the user It is used to recognize the particular user.
Session Tracking Techniques
There are four techniques used in Session tracking:
1. Cookies
2. Hidden Form Field
3. URL Rewriting
4. HttpSession
Cookies in Servlet
A cookie is a small piece of information that is persisted between the multiple client requests.
A cookie has a name, a single value, and optional attributes such as a comment, path and domain
qualifiers, a maximum age, and a version number.
How Cookie works
By default, each request is considered as a new request. In cookies technique, we add cookie
with response from the servlet. So cookie is stored in the cache of the browser. After that if
request is sent by the user, cookie is added with request by default. Thus, we recognize the user
as the old user.
14. Types of Cookie
There are 2 types of cookies in servlets.
1. Non-persistent cookie
2. Persistent cookie
Non-persistent cookie
It is valid for single session only. It is removed each time when user closes the browser.
Persistent cookie
It is valid for multiple session . It is not removed each time when user closes the browser. It is
removed only if user logout or signout.
Advantage of Cookies
1. Simplest technique of maintaining the state.
2. Cookies are maintained at client side.
Disadvantage of Cookies
1. It will not work if cookie is disabled from the browser.
2. Only textual information can be set in Cookie object.
2) Hidden Form Field
In case of Hidden Form Field a hidden (invisible) textfield is used for maintaining the state of
an user.
In such case, we store the information in the hidden field and get it from another servlet. This
approach is better if we have to submit form in all the pages and we don't want to depend on the
browser.
Let's see the code to store value in hidden field.
1. <input type="hidden" name="uname" value="Vimal Jaiswal">
15. Here, uname is the hidden field name and Vimal Jaiswal is the hidden field value.
Real application of hidden form field
It is widely used in comment form of a website. In such case, we store page id or page name in
the hidden field so that each page can be uniquely identified.
Advantage of Hidden Form Field
1. It will always work whether cookie is disabled or not.
Disadvantage of Hidden Form Field:
1. It is maintained at server side.
2. Extra form submission is required on each pages.
3. Only textual information can be used.
3) URL Rewriting
In URL rewriting, we append a token or identifier to the URL of the next Servlet or the next
resource. We can send parameter name/value pairs using the following format:
url?name1=value1&name2=value2&??
A name and a value is separated using an equal = sign, a parameter name/value pair is separated
from another parameter using the ampersand (&). When the user clicks the hyperlink, the
parameter name/value pairs will be passed to the server. From a Servlet, we can use
getParameter () method to obtain a parameter value.
16. Advantage of URL Rewriting
1. It will always work whether cookie is disabled or not (browser independent).
2. Extra form submission is not required on each pages.
Disadvantage of URL Rewriting
1. It will work only with links.
2. It can send Only textual information.
4) HttpSession interface
1. HttpSession interface
2. How to get the HttpSession object
3. Commonly used methods of HttpSession interface
4. Example of using HttpSession
In such case, container creates a session id for each user.The container uses this id to identify the
particular user.An object of HttpSession can be used to perform two tasks:
1. bind objects
2. view and manipulate information about a session, such as the session identifier, creation time,
and last accessed time.
17. How to get the HttpSession object ?
The HttpServletRequest interface provides two methods to get the object of HttpSession:
1. public HttpSession getSession():Returns the current session associated with this request, or if
the request does not have a session, creates one.
2. public HttpSession getSession(boolean create):Returns the current HttpSession associated with
this request or, if there is no current session and create is true, returns a new session.
Commonly used methods of HttpSession interface
1. public String getId():Returns a string containing the unique identifier value.
2. public long getCreationTime():Returns the time when this session was created, measured in
milliseconds since midnight January 1, 1970 GMT.
3. public long getLastAccessedTime():Returns the last time the client sent a request associated
with this session, as the number of milliseconds since midnight January 1, 1970 GMT.
4. public void invalidate():Invalidates this session then unbinds any objects bound to it.
What is Java Server Faces (JSF)? Explain the Life Cycle of JSF.
Java Server Faces (JSF) is a Java Web Application framework based on
Model Views Controller (MVC). It simplifies the structure of user
interfaces (UI) for server-based applications by using reusable UI
components in a java page. It was formalized as a standard through
the Java Community Process (JCP) and is part of the Java Platform,
Enterprise Edition. JSF provides facility to connect data sources with
user interfaces widgets and to server-side event handlers. JSF reduces
the effort in creating and maintaining applications which will run on a
Java application by using reusable UI components.
Benefits
Based on reusable UI components.
Data transfer between UI components in very simple.
UI state managing multiple server requests.
18. Servlets: Servlets are Java classes that run at the server end. To
run servlets, your Web server should be servlet-enabled, i.e. a
servlet engine should be installed to interpret and run your
servlet code on your web server. Servlets are Java technology's
answer to CGI programming. Servlets are server side
components that provide a powerful mechanism for
developing server side programs. Servlets provide
component-based, platform-independent methods for
building Web-based applications. Servlets are not
designed for a specific protocol. It is different thing that
they are most commonly used with the HTTP protocols
Servlets uses the classes in the java packages javax.servlet
and javax.servlet.http. To create Servelet we need to
create a Class that implements httpservlet. Once that is
done we can override doGet and doPost method.
Advantages of Servlet:
Efficient: Each request in servlet is handled by light weigh thread.
Convenient. If you already know java, learning servlet is very simple.
Powerful. Java servlets is a powerful tool, Servlet can talk directly to web server.
Also session handling can be done easiliy in Servlet.
Portable. Servlets are written in Java and follow a well-standardized API. So you
can run the code in any machine which has JVM installed.
Inexpensive. There are a number of free or very inexpensive Web servers
available that are good for "personal" use or low-volume Web sites.
Open Source: Since Servlet is opensource, you need not pay to use it.
What is JSP?
Java Server Pages (JSP) is a technology that lets you mix regular, static HTML
with dynamically-generated HTML. JSP is the extension of HttpServlet instance.
JSP is basically used for presentation and servlet is used for bussiness logic
although you use servlet for presentation after all JSP page also boil down to jsp
19. servlet. We need JSP when we want to read or write values to session object.The
one major advantage of Jsp is we can java code directly embeded into html code.
Advantages of JSP:
1. Dynamic : One can build dynamic websites with JSP
2. Open Source: Since JSP is open source; we need not pay to anyone to use it.
3. Secure: JSP pages are secure.
4. Easy to learn: The business logic for JSP is servelet which is written Java.
So if you know Java, learning JSP application is simple.
Java Mail: Java Mail service is used to add mailing functionalities to java
application. Since it use java as platform, so we get all features of java such as
platform independent, open source, good community support etc. We can write
functionalility to read, compose, search and send mail using Java Mail service. The
Java Mail API 1.4.1 supports JDK 1.4 or higher version.
Steps to install Java Mail Service in your local machine
1. go to http://java.sun.com/products/javamail/downloads/index.html
2. Download javamail-1_4_1.zip 3. Unzip it
4. Set class path to the jar we get by unzipping the zip.
RMI (Remote Method Invocation)
A primary goal for the RMI designers was to allow programmers to develop
distributed Java programs with the same syntax and semantics used for non-distributed
programs. The Java Remote Method Invocation API, or Java RMI, is a
Java application programming interface that performs the object-oriented
equivalent of remote procedure calls.
1. The original implementation depends on Java Virtual Machine (JVM) class
representation mechanisms and it thus only supports making calls from one
JVM to another. The protocol underlying this Java-only implementation is
known as Java Remote Method Protocol (JRMP).
2. In order to support code running in a non-JVM context, a CORBA version
was later developed.
20. CORBA: The Common Object Request Broker Architecture (CORBA) is a standard
defined by the Object Management Group (OMG) that enables software
components written in multiple computer languages and running on multiple
computers to work together (i.e., it supports multiple platforms).
EJB (Enterprise Java Bean) EJB is a specification by Java for managed,
server-side component architecture. It is used for modular construction of
enterprise applications. EJB specification provides details on:
1. Transaction processing
2. Integration with Java Persistence API (JPA)
3. Concurrency control
4. Events using Java Message Service
5. Naming and directory services (JNDI)
6. Security
7. Deployment of software components in an application server
8. Remote procedure calls using RMI-IIOP.
9. Exposing business methods as Web Services.
In Simple java coding that is POJO (Plain old java object), developer has to write
code to manage sessions, events etc. EJB provide heavy weight container to
manange session, events etc.
EJB has capability for:
1. Session Bean: Synchronous
2. Message Driven Bean: Asynchronous