Académique Documents
Professionnel Documents
Culture Documents
A JSP is used mainly for presentation only. A JSP can only be HttpServlet
that means the only supported protocol in JSP is HTTP. But a servlet can support any
protocol like HTTP, FTP, SMTP etc.
JavaBeans are Java utility classes you defined. Beans have a standard format for Java
classes. You use tags
Custom tags and beans accomplish the same goals -- encapsulating complex behavior
into simple and accessible forms. There are several differences:
Custom tags can manipulate JSP content; beans cannot. Complex operations can be
reduced to a significantly simpler form with custom tags than with beans. Custom tags
require quite a bit more work to set up than do beans. Custom tags usually define
relatively self-contained behavior, whereas beans are often defined in one servlet and
used in a different servlet or JSP page. Custom tags are available only in JSP 1.1 and
later, but beans can be used in all JSP 1.x versions.
In POST data is submitted inside body of the HTTP request. The data is not visible on the
URL and it is more secure.
6 Q What is session?
A The session is an object used by a servlet to track a user's interaction with
a Web application across multiple HTTP requests. The session is stored on the server.
1
7 Q What is servlet mapping?
A The servlet mapping defines an association between a URL pattern and a
servlet. The mapping is used to map requests to Servlets.
9 Q What is a servlet ?
A servlet is a java program that runs inside a web container.
12 Q How many JSP scripting elements are there and what are they?
A There are three scripting language elements: declarations, scriptlets,
expressions.
2
o jspInit() method is called
o _jspService is called to handle service calls
o _jspDestroy is called to destroy it when the servlet is not required.
18 Q What is a Expression?
A Expressions are act as place holders for language expression, expression is
evaluated each time the page is accessed. This will be included in the service method of
the generated servlet.
19 Q What is a Declaration?
A It declares one or more variables or methods for use later in the JSP source
file. A declaration must contain at least one complete declarative statement. You can
declare any number of variables or methods within one declaration tag, as long as
semicolons separate them. The declaration must be valid in the scripting language used in
the JSP file. This will be included in the declaration section of the generated servlet.
20 Q What is a Scriptlet?
A A scriptlet can contain any number of language statements, variable or
expressions that are valid in the page scripting language. Within scriptlet tags, you can
declare variables to use later in the file, write expressions valid in the page scripting
language, use any of the JSP implicit objects or any object declared with a
<jsp:useBean>. Generally a scriptlet can contain any java code that are valid inside a
normal java method. This will become the part of generated servlet's service method.
3
occurs will be lost. This extra round trip a redirect is slower than forward. Client can
disable sendRedirect.
24 Q Is JSP extensible ?
A Yes, it is. JSP technology is extensible through the development of custom
actions, or tags, which are encapsulated in tag libraries.
33 Q What is the difference between <%@ include ...> (directive include) and
<jsp:include> ?
A @ include is static include. It is inline inclusion. The contents of the file
will get included on Translation phase. It is something like inline inclusion. We cannot
have a dynamic filename for directive include. <jsp:include> is dynamic include. Here
4
the included file will be processed as a separate file and the response will be included.
We can have a dynamic filename for <jsp:include>. We can aslo pass parameters to
<jsp:include
<%
response.setHeader("Cache-Control","no-store"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
%>
A You will get this error only when you try to redirect a page after you
already have flushed the output buffer. This happens because HTTP specification force
the header to be set up before the lay out of the page can be shown. When you try to send
a redirect status, your HTTP server cannot send it right now if it hasn't finished to set up
the header. Simply it is giving the error due to the specification of HTTP 1.0 and 1.1
5
A A jsp:useBean action may optionally have a body. If the body is specified,
its contents will be automatically invoked when the specified bean is instantiated (Only at
the time of instantiation.) Typically, the body will contain scriptlets or jsp:setProperty
tags to initialize the newly instantiated bean, although you are not restricted to using
those alone.
43 Q What is JSP?
A JSP is a server side scripting technology. JSP allows Java as well as a few
special tags to be embedded into a web file (HTML/XML, etc). The suffix must ends
with .jsp.
6
A There is no such limit on the amount of information that can be saved in a
Session Object. The only limit is the Session ID length , which should not exceed more
than 4K.
A Session is stored in server but cookie stored in client. Session should work
regardless of the settings on the client browser. There is no limit on the amount of data
that can be stored on session. But it is limited in cookie. Session can store objects and
cookies can store only strings. Cookies are faster than session.
54 Q What is HttpTunneling?
A HTTP tunneling is used to encapsulate other protocols within the HTTP or
HTTPS protocols. Normally the intranet is blocked by a firewall and the network is
exposed to the outer world only through a specific web server port, that listens for only
HTTP requests. To use any other protocol, that by passes the firewall, the protocol is
embedded in HTTP and send as HttpRequest.
7
requests exhaust the servlet instance pool. In that case, all the un serviced requests are
queued until something becomes free - which results in poor performance.
62 Q What is the difference between Context init parameter and Servlet init
parameter?
A Servlet init parameters are for a single servlet only. No body out side that
servlet can access that. It is declared inside the <servlet> tag inside Deployment
Descriptor, where as context init parameter is for the entire web application. Any servlet
or JSP in that web application can access context init parameter. Context parameters are
declared in a tag <context-param> directly inside the <web-app> tag. The methods for
accessing context init parameter is getServletContext ().getInitParamter (“name”) where
as method for accessing servlet init parameter is getServletConfig ().getInitParamter
(“name”);
8
A The return type of attribute is object, where the return type of parameter is
String. The method to retrieve attribute is getAttribute () where as for parameter is
getParamter (). We have a method setAttribute to set an attribute. But there is no setters
available for setting a parameter.
66 Q What is the difference between setting the session time out in deployment
descriptor and setting the time out programmatically?
A In DD time out is specified in terms of minutes only. But in
programmatically it is specified in seconds. A session time out value of zero or less in
DD means that the session will never expire. To specify session will never expire
programmatically it must be negative value.
71 Q How can you stop JSP execution in the middle of processing a request?
A We can use the return statement to stop the processing of JSP. Because
JSP is compiled to servlet and all the statements will go inside service method, any time
you can stop the processing using return statement.
9
73 Q How will you pass information from JSP to included JSP?
A By using <%jsp:param> tag.
75 Q How can I enable session tracking for JSP pages if the browser has
disabled cookies?
A By default session tracking uses cookies to associate a session identifier
with a user. If the browser does not support cookies, or if cookies are disabled, you can
still use session tracking using URL rewriting. For URL rewriting to be effective, you
need to append the session ID for each and every link that is part of your servlet response.
By using the methods response.encodeURL() and response.encodeRedirectURL() we can
achieve this.
80 Q How many cookies can one set in the response object of the servlet? Also,
are there any restrictions on the size of cookies?
10
A If the client is using Netscape, the browser can receive and store 300 total
cookies and 4 kilobytes per cookie. And the no of cookie is restricted to 20 cookies per
server or domain
>
81 Q When a session object gets added or removed to the session, which event
will get notified ?
A HttpSessionBindingListener will get notified When an object is added
and/or removed from the session object, or when the session is invalidated, in which case
the objects are first removed from the session, whether the session is invalidated
manually or automatically (timeout).
84 Q What are the different web servers available name few of them?
A Tomcat, Java Web Server, JRun
11
A init() is called whenever the servlet is loaded for the first time into the web
server. Destroy will be called whenever the servlet is removed from the web server.
12
98 Q Explain the directory structure of a web application?
A ServletResponse allows the servlet to set the content length and MIME
type of that response. It provides an output stream, ServletOutputStream and a Writer
through which the servlet can send data.
13
A Servlet and JSP containers are collectively referred to as Web containers.
107 Q How can I print the stack trace of an exception from a JSP page?
A By creating an object of PrintWriter we can print the stack trace in JSP
page. Eg:
PrintWriter pw = response.getWriter();
exception.printStackTrace(pw);
111 Q What is the difference between an application server and a web server?
14
1 Q Why threads block or enters to waiting state on I/O?
A Threads enters to waiting state or block on I/O because other
threads can execute while the I/O operations are performed.
4 Q What is synchronization
6 Q What is a Vector
A Vector is a grow able array of objects.
15
10 Q What is the difference between time slicing and preemptive
scheduling ?
A In preemptive scheduling, highest priority task continues execution
till it enters a not running state or a higher priority task comes into existence. In time
slicing, the task continues its execution for a predefined period of time and reenters the
pool of ready tasks.
13 Q What are the differences between boolean & operator and &
operator
A When an expression containing the & operator is evaluated, both
operands are evaluated. And the & operator is applied to the operand. When an
expression containing && operator is evaluated, the first operand is evaluated. If the first
operand returns a value of true then only the second operand is evaluated otherwise the
second part will not get executed. && is also called short cut and.
16
18 Q What is the difference between synchronized block and
synchronized method ?
A Synchronized blocks place locks for the specified block where as
synchronized methods place locks for the entire method.
24 Q What must be the order of catch blocks when catching more than
one exception?
A The sub classes must come first. Otherwise it will give a compile
time error.
25 Q How can we call a method or variable of the super class from child
class ?
A We can use super.method() or super.variable syntax for this
purpose.
28 Q What is serialization ?
A Serialization is the process of saving the state of an object.
17
29 Q What is de-serialization?
A De-serialization is the process of restoring the state of an object.
30 Q What is externalizable ?
A It is an interface that extends Serializable. It is having two different
methods writeExternal() and readExternal. This interface allows us to customize the
output.
31 Q Does garbage collection guarantee that a program will not run out of
memory?
A Garbage collection does not guarantee that a program will not run out of
memory. It is also possible for programs to create objects that are not subject to garbage
collection. And there is no guarantee that Garbage Collection thread will be executed.
18
38 Q What must a class do to implement an interface?
A It must identify the interface in its implements clause. Also it must provide
definition for all the methods in the interface otherwise it must be declared abstract.
43 Q What is the difference between static and non static inner class ?
A A non-static inner class can have an object instances that are associated
with instances of the class's outer class. A static inner class can not have any object
instances.
A java.lang.Object.
46 Q What is the difference between readers and streams?
A Readers are character oriented where streams are byte oriented. The
readers are having full support for Unicode data.
19
A There are 8 primitive types in java. boolean , char, byte, short, int long,
float, double.
49 Q What is static ?
A static means one per class. static variables are created when the class
loads. They are associated with the class. In order to access a static we don't need
objects. We can directly access static methods and variable by calling
classname.variablename.
55 Q What is casting ?
A Casting means converting one type to another. There are mainly two types
of casting. Casting between primitive types and casting between object references.
Casting between primitive numeric types is used to convert larger data types to smaller
data types. Casting between object references is used to refer to an object by a compatible
class, interface, or array type reference.
20
57 Q What is a void return type ?
A A void indicates that the method will not return anything.
59 Q What are the different ways in which a thread can enter into waiting state?
A There are three ways for a thread to enter into waiting state. By invoking
its sleep() method, by blocking on I/O, by unsuccessfully attempting to acquire an
object's lock, or by invoking an object's wait() method.
62 Q What is the difference between the prefix and postfix forms of the ++
operator?
A The prefix form first performs the increment operation and then returns
the value of the increment operation. The postfix form first returns the current value of
the expression and then performs the increment operation on that value.
21
66 Q What is hashCode?
A The hashcode of a Java Object is simply a number (32-bit signed int) that
allows an object to be managed by a hash-based data structure. A hashcode should be,
equal for equal object (this is mandatory!) , fast to compute based on all or most of the
internal state of an object, use all or most of the space of 32-bit integers in a fairly
uniform way , and likely to be different even for objects that are very similar. If you are
overriding hashCode you need to override equals method also.
A The File class contains information the files and directories of the local
file system. The RandomAccessFile class contains the methods needed to directly access
data contained in any part of a file.
69 Q What is final ?
A A final is a keyword in java. If final keyword is applied to a variable, then
the variable will become a constant. If it applied to method, sub classes cannot override
the method. If final keyword is applied to a class we cannot extend from that class.
22
Package/Default are accessible from the same package. Private is only accessible from
within the class.
83 Q What is a Dictionary?
A Dictionary is a parent class for any class that maps keys to values., In a
dictionary every key is associated with at most one value.
23
A JAR stands for Java Archive. This is a file format that enables you to
bundle multiple files into a single archive file. A jar file will contains a manifest.mf file
inside META-INF folder that describes the version and other features of jar file.
89 Q What is JVM?
A JVM stands for Java Virtual Machine. It is the run time for java programs.
All are java programs are running inside this JVM only. It converts java byte code to OS
specific commands. In addition to governing the execution of an application's byte codes,
the virtual machine handles related tasks such as managing the system's memory,
providing security against malicious code, and managing multiple threads of program
execution.
90 Q What is JIT?
A JIT stands for Just In Time compiler. It compiles java byte code to native
code.
91 Q What is java byte code?
94 Q What is finalize() ?
A Finalize is a protected method in java. When the garbage collector is
executes , it will first call finalize( ), and on the next garbage-collection it reclaim the
24
objects memory. So finalize( ), gives you the chance to perform some cleanup operation
at the time of garbage collection.
95 Q What is multi-threading?
A Multi-threading is the scenario where more than one threads are running.
96 Q What is deadlock?
A Deadlock is a situation when two threads are waiting on each other to
release a resource. Each thread waiting for a resource which is held by the other waiting
thread.
99 Q What is internationalization?
A Internationalization is the process of designing an application so that it can
be adapted to various languages and regions without changes.
103 Q Strings are immutable. But String s="Hello"; String s1=s+"World" returns
HelloWorld how ?
A Here actually a new object is created with the value of HelloWorld
25
105 Q What is path?
A It is an the location where the OS will look for finding out the executable
files and commands.
111 Q How does a try statement determine which catch clause should be used to
handle an exception?
A When an exception is thrown , the catch block of the try statement are
examined in the order in which they appear. The first catch block that is capable of
handling the exception is executed. The remaining catch blocks are ignored
26
A If a class doesn't have a constructor, the JVM will provide a default
constructor for the class.
116 Q What will happen if you are invoking a thread's interrupt method while the
thread is waiting or sleeping?
A When the task enters to the running state, it will throw an
InterruptedException.
117 Q What are the different ways in which a thread can enter into waiting state?
A There are three ways for a thread to enter into waiting state. By invoking
its sleep() method, by blocking on I/O, by unsuccessfully attempting to acquire an
object's lock, or by invoking an object's wait() method.
118 Q What are the the different ways for creating a thread?
A A thread can be created by subclassing Thread, or by implementing the
Runnable interface.
27
123 Q How will you define an interface?
A An interface is defined with the keyword interface. Eg:
public interface MyInterface { }
Bootstrap ClassLoaders
Extensive ClassLoaders
System Classpath ClassLoaders
Application ClassLoaders
28
A Native threads can switch between threads preemptively. Green threads
switch only when control is explicitly given up by a thread ( Thread.yield(),
Object.wait(), etc.) or a thread performs a blocking operation (read(), etc.). On multi-CPU
machines, native threads can run more than one thread simultaneously by assigning
different threads to different CPUs. Green threads run on only one CPU. Native threads
create the appearance that many Java processes are running: each thread takes up its own
entry in the process table. One clue that these are all threads of the same process is that
the memory size is identical for all the threads - they are all using the same memory. The
process table is not infinitely large, and processes can only create a limited number of
threads before running out of system resources or hitting configured limits.
29
141 Q What is reflection?
A Reflection is the process of finding out the different features of a class
dynamically.
30
A The Comparable is for natural ordering and Comparator is for custom
ordering. But we can override the compareTo method of comparable interface to give a
custom ordering.
152 Q How do you send data from an applet to Servlet ? What are the steps
involved in it ?
A You can use the java.net.URLConnection and java.net.URL classes to
open a standard HTTP connection to the web server. The server then passes this
information to the servlet in the normal way.
Basically, the applet pretends to be a web browser, and the servlet doesn't know the
difference. As far as the servlet is concerned, the applet is just another HTTP client.
31
using the new operator. We cannot invoke a constructor directly. A method is an ordinary
member function of a class. It has its own name, a return type (which may be void), and
is invoked using the dot operator.
32
167 Q What are the methods in Object?
A clone, equals, wait, finalize, getClass, hashCode, notify, notifyAll,
toString
A The JVM is guaranteed to treat reads and writes of data of 32 bits or less
as atomic. For long or double variable, programmers should take care in multi-threading
environment. Either put these variables in a synchronized method or block, or declare
them volatile.
174 Q Does the code in finally block get executed if there is an exception and a
return statement in a catch block?
33
A If an exception occurs and there is a return statement in catch block, the
finally block is still executed. The finally block will not be executed when the
System.exit(1) statement is executed earlier or the system shut down earlier or the
memory is used up earlier before the thread goes to finally block.
175 Q Considering notepad/IE or any other thing as process, What will happen if
you start notepad or IE 3 times? Where 3 processes are started or 3 threads are started ?
A 3 processes will start.
176 Q What are the restrictions placed on the values of each case of a switch
statement?
A At compile time, each case values of switch statement must evaluate to a
an int value.
34
A A stream is a flow of data from one direction to another. . There are two
types of Streams Byte Streams: Provide a convenient means for handling input and
output of bytes. And Character Streams: Provide a convenient means for handling input
& output of characters.
A TCP/IP is a two-way communication between the client and the server and
it is a reliable and there is a confirmation regarding reaching the message to the
destination. UDP is a one-way communication only between the client and the server and
it is not a reliable and there is no confirmation regarding reaching the message to the
destination.
35
A By running the serialver tool with the name of the class as the command
line argumet, as shown in the example that follows: serialver java.lang.String
Q What is JDBC?
A JDBC technology is an API (included in both J2SE and J2EE releases)
that provides cross-DBMS connectivity to a wide range of SQL databases and access to
other tabular data sources, such as spreadsheets or flat files. With a JDBC technology-
enabled driver, you can connect all corporate data even in a heterogeneous environment
5 Q What is DriverManager ?
36
A DriverManager is a class in java.sql package. It is the basic service for
managing a set of JDBC drivers.
6 Q What is a ResultSet ?
A A table of data representing a database result set, which is usually
generated by executing a statement that queries the database.
A ResultSet object maintains a cursor pointing to its current row of data. Initially the
cursor is positioned before the first row. The next method moves the cursor to the next
row, and because it returns false when there are no more rows in the ResultSet object, it
can be used in a while loop to iterate through the result set.
7 Q What is Connection?
A Connection class represents a connection (session) with a specific
database. SQL statements are executed and results are returned within the context of a
connection.
A Connection object's database is able to provide information describing its tables, its
supported SQL grammar, its stored procedures, the capabilities of this connection, and so
on. This information is obtained with the getMetaData method.
Type 2: Native API Partly Java Driver- A native-API partly Java technology-enabled
driver converts JDBC calls into calls on the client API for Oracle, Sybase, Informix,
DB2, or other DBMS. Note that, like the bridge driver, this style of driver requires that
some binary code be loaded on each client machine.
37
Type 3: Network protocol Driver- A net-protocol fully Java technology-enabled driver
translates JDBC API calls into a DBMS-independent net protocol which is then translated
to a DBMS protocol by a server. This net server middleware is able to connect all of its
Java technology-based clients to many different databases. The specific protocol used
depends on the vendor. In general, this is the most flexible JDBC API alternative. It is
likely that all vendors of this solution will provide products suitable for Intranet use. In
order for these products to also support Internet access they must handle the additional
requirements for security, access through firewalls, etc., that the Web imposes. Several
vendors are adding JDBC technology-based drivers to their existing database middleware
products.
Type 4: JDBC Net pure Java Driver - A native-protocol fully Java technology-enabled
driver converts JDBC technology calls into the network protocol used by DBMSs
directly. This allows a direct call from the client machine to the DBMS server and is a
practical solution for Intranet access. Since many of these protocols are proprietary the
database vendors themselves will be the primary source for this style of driver. Several
database vendors have these in progress.
38