JavaBeat FAQs
FAVORITES
Download a movie FAQ
JAVABEAT
Home
Articles
Tips
QnA
Forums
FAQs Topics Books Topic
Java Java Books
Struts Struts Books
Spring Spring Books
Hibernate Hibernate Books
JBoss Seam Seam Books
J2EE J2EE Books
EJB EJB Books
JSF JSF Books
Servlets Servlets Books
JDBC JDBC Books
RMI RMI Books
JMS JMS Books
JNI JNI Books
Design Patterns Patterns Books
Exception Exception Books
Apache Ant Ant Books
Groovy
Cobertura
Quartz
1000 Interview Questions on Java / J2EE technologies. Buy now for JUST 12.99 USD or 400 INR. Send us mail to sales@javabeat.net

Java Servlets Interview Questions

Java Servlet is the serverside Java programming language. We can say it as serverside applet. How applet is used for writing the client side code, servlet is used for writing the serverside programming language. Servlet programming is first created by Sun Microsystems in June 1997. The latest version is Servlet 2.5 which is released with JEE 5.0 specification. Servlet is a specification and it can be implemented by anyone who can fulfil the mandatory requirements of the specification maintained by Sun.

Servlet Articles

What is servlet?

Servlet Life Cycle

New Features in Servlet 3.0

 
Servlets FAQs - 1 | Servlets FAQs - 2 | Servlets FAQs - 3 | Servlets FAQs - 4 | Servlets FAQs - 5
Servlets FAQs - 6 | Servlets FAQs - 7 | Servlets FAQs - 8 | Servlets FAQs - 9 | Servlets FAQs - 10

21) How do you share session objects between servlets and JSP?
 

Sharing sessions between a servlet and a JSP page is straight forward. JSP makes it a little easy by creating a session object and making it availabe already. In a servlet you would have to do it yourself. This is how:

//create a session if one is not created already now
HttpSession session = request.getSession(true);
//assign the session variable to a value.
session.putValue("variable","value");

in the jsp page this is how you get the session value:

<%
session.getValue("varible");
%>

22) What is a servlet?
 

A servlet is a way of extending your web server with a Java program to perform tasks previously dealt with by CGI scripts or proprietary server extension frameworks.

 

23) Is there any method to unload a servlet from Web Server memory without restarting the server?
 

There is no standard method/mechanism to unload a servlet from memory. Some servers, like JWS, provide the means to load and unload servlets from their administration module. Others, like Tomcat, require you to just replace the WAR file.

24) What distinguishes a JavaBean from a Servlet?
 

JavaBeans are a set of rules to follow to create reusable software components, or beans. This contains properties and events. At the end you have a component which could be examined by a program (like an IDE) to allow the user of your JavaBean component to configure it and to run in its Java programs.

Servlets are Java classes running in a Servlet engine implementing a particular interface: Servlet, forcing you to implement some methods (service()). The servlet is an extension of your web server where this servlet is running on and only lets you know when a user requests a GET or POST calls from a web page to your servlet.

So, both have nothing in common except Java.

25) How much data we can store in a session object?
 

Any amount of data can be stored there because the session is kept on the server side.

The only limitation is sessionId length, which shouldn't exceed ~4000 bytes - this limitation is implied by HTTP header length limitation to 4Kb since sessionId may be stored in the cookie or encoded in URL (using "URL rewriting") and the cookie specification says the size of cookie as well as HTTP request (e.g. GET /document.html\n) cannot be longer then 4kb.

26) What is the difference between the doGet and doPost methods?
 

doGet is called in response to an HTTP GET request. This happens when users click on a link, or enter a URL into the browser's address bar. It also happens with some HTML FORMs (those with METHOD="GET" specified in the FORM tag).

doPost is called in response to an HTTP POST request. This happens with some HTML FORMs (those with METHOD="POST" specified in the FORM tag).

Both methods are called by the default (superclass) implementation of service in the HttpServlet base class. You should override one or both to perform your servlet's actions. You probably shouldn't override service().

 

27) What is the difference between encodeRedirectUrl and encodeURL?
 

encodeURL and encodeRedirectURL are methods of the HttpResponse object. Both rewrite a raw URL to include session data if necessary. (If cookies are on, both are no-ops.)

encodeURL is for normal links inside your HTML pages.

encodeRedirectURL is for a link you're passing to response.sendRedirect(). It has slightly different syntax requirements too gory to get into here.

28) Can I use System.exit() in servlets?
 

Gack! No no no no no...

At best, you'll get a security exception. At worst, you'll make the servlet engine, or maybe the entire web server, quit. You don't really want to do that, huh? :-)

29) I am opening a single JDBC connection in my init() method. Do I need to synchronize on the Connection or the Statement object?
 

You shouldn't have to. If your JDBC driver supports multiple connections, then the various createStatement methods will give you a thread-safe, reentrant, independent Statement that should work OK, even if other requests/threads are also accessing other Statements on the same Connection.

Of course, crossing your fingers never hurts... Many early JDBC drivers were not re-entrant. The modern versions of JDBC drivers should work OK, but there are never any guarantees.

Using connection pooling will avoid the whole issue, plus will lead to improved performance. See this FAQ for more information.

30) How can I determine the name and version number of the servlet or JSP engine that I am using?
 

From within a servlet, you can invoke the ServletContext.getServerInfo() method as follows:

String thisServer= getServletConfig().getServletContext().getServerInfo();

If you are using JSP, you can use this expression:

<%= application.getServerInfo() %>
Related Articles
JAX-WS Web Services in NetBeans 6.1
JPA in NetBeans 6.1
EJB 3.0 and WebServices
Types of Managed Bean scopes in Spring Framework
OpenCms 7 Development
Liferay Portal Enterprise Intranets
ZK Developer’s Guide : Online Media Library
The BIRT Environment and Your First Report
AJAX - The Complete Reference
The Java 6.0 Compiler API
more articles
java6 ejb3 jsf hibernate eclipse ajax groovy spring seam java struts webservice j2me guice java5 jca tapestry soa linux ria books

Favorites
C# problem error
Java News
AffiliatedAds.com
Access Control
Busby seo challenge contest
Sohbet
Chat
Webmaster Hosting Forum
Java Jobs
MyVideoLib
Sohbet
chat
Latest QnA
SCJD Tips
When we start a thread by applying start() method on it ,how does it knows that to execute run()method?
About Wrapper class in Java
How to configure weblogic 7.0 in MyEclipse?
Static Block and Static Initializer in Java

JavaBeat Website (2004-2008), India
javabeat | planetoss | about us | write for us | useful resources
Copyright (2004 - 2008), JavaBeat