Search This Blog

Tuesday, March 07, 2006

Some more Java Interview Questions Part-4

What is the servlet?
Servlet is a script, which resides and executes on server side, to create dynamic HTML. In servlet programming we will use java language. A servlet can handle multiple requests concurrently

What is the architechture of servlet package?
Servlet Interface is the central abstraction. All servlets implements this Servlet
Interface either direclty or indirectly
( may implement or extend Servlet Interfaces sub classes or sub interfaces)

Servlet

Generic Servlet

HttpServlet ( Class ) -- we will extend this class to handle GET / PUT HTTP requests

MyServlet

What is the difference between HttpServlet and GenericServlet?
A GenericServlet has a service() method to handle requests.
HttpServlet extends GenericServlet added new methods
doGet()
doPost()
doHead()
doPut()
doOptions()
doDelete()
doTrace() methods
Both these classes are abstract.

What's the difference between servlets and applets?
Servlets executes on Servers. Applets executes on browser. Unlike applets, however, servlets have no graphical user interface.

What are the uses of Servlets?
A servlet can handle multiple requests concurrently, and can synchronize requests. Servlets can forward requests to other servers and servlets. Thus servlets can be used to balance load among several servers.


When doGET() method will going to execute?
When we specified method='GET' in HTML
Example : < name="'SSS'" method="'GET'">

When doPOST() method will going to execute?
When we specified method='POST' in HTML
< name="'SSS'" method="'POST'">


What is the difference between Difference between doGet() and doPost()?
GET Method : Using get method we can able to pass 2K data from HTML
All data we are passing to Server will be displayed in URL (request string).

POST Method : In this method we does not have any size limitation.
All data passed to server will be hidden, User cannot able to see this info
on the browser.

Which code line must be set before any of the lines that use the PrintWriter?
setContentType() method must be set.

Which protocol will be used by browser and servlet to communicate ?
HTTP


In how many ways we can track the sessions?
Method 1) By URL rewriting
Method 2) Using Session object

Getting Session form HttpServletRequest object
HttpSession session = request.getSession(true);

Get a Value from the session
session.getValue(session.getId());

Adding values to session
cart = new Cart();

session.putValue(session.getId(), cart);
At the end of the session, we can inactivate the session by using the following command
session.invalidate();

Method 3) Using cookies
Method 4) Using hidden fields

Some more Java Interview Questions Part-5

How Can You invoke other web resources (or other servelt / jsp ) ?
Servelt can invoke other Web resources in two ways: indirect and direct.

Indirect Way : Servlet will return the resultant HTML to the browser which will point to another Servlet (Web resource)

Direct Way : We can call another Web resource (Servelt / Jsp) from Servelt program itself, by using RequestDispatcher object.

You can get this object using getRequestDispatcher("URL") method. You can get this object from either a request or a Context.

Example :
RequestDispatcher dispatcher = request.getRequestDispatcher("/jspsample.jsp");
if (dispatcher != null)
dispatcher.forward(request, response);
}


How Can you include other Resources in the Response?
Using include method of a RequestDispatcher object.

Included WebComponent (Servlet / Jsp) cannot set headers or call any method (for example, setCookie) that affects the headers of the response.

Example : RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/banner");
if (dispatcher != null)
dispatcher.include(request, response);
}


What is the difference between the getRequestDispatcher(String path) ServletRequest interface and ServletContext interface?

The getRequestDispatcher(String path) method of ServletRequest interface accepts parameter the path to the resource to be included or forwarded to, which can be relative to the request of the calling servlet. If the path begins with a "/" it is interpreted as relative to the current context root.

The getRequestDispatcher(String path) method of ServletContext interface cannot accepts relative paths. All path must sart with a "/" and are interpreted as relative to curent context root. If the resource is not available, or if the server has not implemented a RequestDispatcher object for that type of resource, getRequestDispatcher will return null. Your servlet should be prepared to deal with this condition.



What is the use of ServletContext ?
Using ServletContext, We can access data from its environment. Servlet context is common to all Servlets so all Servlets share the information through ServeltContext.

Is there any way to generate PDF'S dynamically in servlets?
We need to use iText. A open source library for java. Please refer sourceforge site for sample servlet examples.

What is the difference between using getSession(true) and getSession(false) methods?
getSession(true) - This method will check whether already a session is existing for the user. If a session is existing, it will return that session object, Otherwise it will create new session object and return taht object.

getSession(false) - This method will check existence of session. If session exists, then it returns the reference of that session object, if not, this methods will return null.

Building servlets with session tracking

Jeanne Murray developerWorks staff, IBM December 2000 This tutorial teaches techniques for building Internet applications using servlet and JSP technology. A key point is to enable session handling, so the servlet knows which user is doing what. The tutorial shows a URL bookmarking system in which multiple users access a system to add, remove, and update an HTML listing of bookmarks. The servlet uses JSP technology to handle the user interaction. After a quick (2 minutes, tops).

Read Full Article

Saturday, March 04, 2006

EJB Interview Questions Part-2

Why an onMessage call in Message-driven bean is always a seperate transaction?

EJB 2.0 specification: "An onMessage call is always a separate transaction, because there is never a transaction in progress when the method is called."

When a message arrives, it is passed to the Message Driven Bean through the onMessage() method, that is where the business logic goes.

Since there is no guarantee when the method is called and when the message will be processed, is the container that is responsible of managing the environment, including transactions.


Why are ejbActivate() and ejbPassivate() included for stateless session bean even though they are never required as it is a nonconversational bean?

To have a consistent interface, so that there is no different interface that you need to implement for Stateful Session Bean and Stateless Session Bean.

Both Stateless and Stateful Session Bean implement javax.ejb.SessionBean and this would not be possible if stateless session bean is to remove ejbActivate and ejbPassivate from the interface.

Static variables in EJB should not be relied upon as they may break in clusters.Why?
Static variables are only ok if they are final. If they are not final, they will break the cluster. What that means is that if you cluster your application server (spread it across several machines) each part of the cluster will run in its own JVM.

Say a method on the EJB is invoked on cluster 1 (we will have two clusters - 1 and 2) that causes value of the static variable to be increased to 101. On the subsequent call to the same EJB from the same client, a cluster 2 may be invoked to handle the request. A value of the static variable in cluster 2 is still 100 because it was not increased yet and therefore your application ceases to be consistent. Therefore, static non-final variables are strongly discouraged in EJBs.

If I throw a custom ApplicationException from a business method in Entity bean which is participating in a transaction, would the transaction be rolled back by container?
EJB Transaction is automatically rolled back only when a SystemException (or a subtype of it) is thrown.

Your ApplicationExceptions can extend from javax.ejb.EJBException, which is a sub class of RuntimeException. When a EJBException is encountered the container rolls back the transaction. EJB Specification does not mention anything about Application exceptions being sub-classes of EJBException.

You can tell container to rollback the transaction, by using setRollBackOnly on SessionContext/EJBContext object as per type of bean you are using.

EJB Interview Questions Part-3

Does Stateful Session bean support instance pooling?
Stateful Session Bean conceptually doesn't have instance pooling.

Can I map more than one table in a CMP?
no, you cannot map more than one table to a single CMP Entity Bean. CMP has been, in fact, designed to map a single table.

Can I invoke Runtime.gc() in an EJB?
You shouldn't. What will happen depends on the implementation, but the call will most likely be ignored.

Can a Session Bean be defined without ejbCreate() method?
The ejbCreate() methods is part of the bean's lifecycle, so, the compiler will not return an error because there is no ejbCreate() method.

However, the J2EE spec is explicit:

• the home interface of a Stateless Session Bean must have a single create() method with no arguments,
while the session bean class must contain exactly one ejbCreate() method, also without arguments.

• Stateful Session Beans can have arguments (more than one create method)

How to implement an entity bean which the PrimaryKey is an autonumeric field
The EJB 2 Spec (10.8.3 - Special case: Unknown primary key class) says that in cases where the PrimaryKeys are generated automatically by the underlying database, the bean provider must declare the findByPrimaryKey method to return java.lang.Object and specify the Primary Key Class as java.lang.Object in the Deployment Descriptor.

When defining the Primary Key for the Enterprise Bean, the Deployer using the Container Provider's tools will typically add additional container-managed fields to the concrete subclass of the entity bean class.

In this case, the Container must generate the Primary Key value when the entity bean instance is created (and before ejbPostCreate is invoked on the instance.)

What is clustering?
Clustering is grouping machines together to transparantly provide enterprise services. Clustering is an essential piece to solving the needs for today's large websites.

The client does not know the difference between approaching one server or approaching a cluster of servers.


Is it possible to share an HttpSession between a JSP and EJB? What happens when I change a value in the HttpSession from inside an EJB?


You can pass the HttpSession as parameter to an EJB method,only if all objects in session are serializable. This has to be consider as "passed-by-value", that means that it's read-only in the EJB. If anything is altered from inside the EJB, it won't be reflected back to the HttpSession of the Servlet Container.

If my session bean with single method insert record into 2 entity beans, how can know that the process is done in same transaction (the attributes for these beans are Required)?

Introduction to JavaServer Pages technology

Noel J. Bergman August 2001 This dW-exclusive tutorial introduces the fundamentals of JavaServer Pages (JSP) technology and discusses the elements that define JSP technology: concepts, syntax, and semantics. It also identifies and exemplifies each element and uses short, specific, topical examples to illustrate each element and illuminate important issues. After a quick (2 minutes, tops) registration, you can begin the tutorial. The tutorial should take you about 1 hour to complete

Read Full Article

Friday, March 03, 2006

JSP Standard Tag Libraries, Part 1

If you need a primer as to what JSTL encompasses, check out part one of this series. In this article, we will cover more of the details of how to use the various tags in the different Tag Library Descriptors (TLDs). We'll go though samples using the conditionals, iteration, and URL, U18N, SQL, and XML tags. The goal of this article is to show the key components of the JSTL, learn how to use the...n

Read Full Article

JSP Overview

In this excerpt from JavaServer Pages, 2nd Edition , the second in a two-part series providing an overview of JSP, you'll find an introduction to JSP application design with MVC and learn about JSP processing. JSP Processing Just as a web server needs a servlet container to provide an interface to servlets, the server needs a JSP container to process JSP pages. The JSP...n

Read Full Article

Wednesday, March 01, 2006

JSTL 1.0, EJB 2.1 and XML Basics

Dear Reader, The future of JSP development is here, and its name is the JSP Standard Tag Library. JSTL provides a set of standardized JSP custom actions to handle the tasks needed in almost all JSP pages, including conditional pro- cessing, internationalization, database access, and XML processing. This week on ONJava.com , Hans Bergsten, author of O'Reilly's " JavaServer Pages, 2nd Edition ," starts a series of articles on the new JSTL.

Read Full Article