Thursday, February 1, 2007

Can I invoke a JSP error page from a servlet?

Yes, you can invoke the JSP error page and pass the exception object to it from within a servlet. The trick is to create a request dispatcher for the JSP error page, and pass the exception object as a javax.servlet.jsp.jspException request attribute. However, note that you can do this from only within controller servlets.If your servlet opens an OutputStream or PrintWriter; the JSP engine will throw the following translation error:
Java.lang.IllegalStateException: Cannot forward as OutputStream or Writer has already been obtained the following code snippet demonstrates the invocation of a JSP error page from within a controller servlet:

protected void sendErrorRedirect(HttpServletRequest request,
HttpServletResponse response, String errorPageURL, Throwable e) throws
ServletException, IOException {
request.setAttribute ("javax.servlet.jsp.jspException", e);
getServletConfig().getServletContext().
getRequestDispatcher(errorPageURL).forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse
response)
{
try {
// do something
} catch (Exception ex) {
try {
sendErrorRedirect(request,response,"/jsp/MyErrorPage.jsp",ex);
} catch (Exception e) {
e.printStackTrace();
}
}
}


SOURCE : www.referjava.com

Difference between single thread and multi thread model servlet

A servlet that implements SingleThreadModel means that for every request, a single servlet instance is created. This is not a very scalable solution as most web servers handle multitudes of requests. A multi-threaded servlet means that one servlet is capable of handling many requests, which is the way most servlets should be implemented.
A single thread model for servlets is generally used to protect
sensitive data ( bank account operations ).

b. Single thread model means instance of the servlet gets created for each request recieved. Its not thread safe whereas in multi threaded only single instance of the servlet exists for what ever # of requests recieved. Its thread safe and is taken care by the servlet container.


SOURCE : www.referjava.com

the difference between ServetConfig and ServletContext?

Servlet Config as the name suggests, provides Servlet specific configuration information to a particular servlet. For Example: - the information that u specify in the web.xml file like this
...
Each servlet will have its own servlet config object, which will be specific to that servlet.

ServletContext
--------------
It provides application view to all the servlets. There is one
ServletContext per JVM for one application. It also provides methods to access application wide parameters specified in.... tag in web.xml. These will be accessible to all the servlets.

b. SevletConfig is used to give the information during the initializing period. ServletContext is used for they communication between the servlet and
the servletcontainer thatis to give MIME types. ServletContext is used to access other environments thatis accessing other servlets with in the same server.
ServletContext is used to share all the servlets.In servlet interface we are having the servletcontext and servletconfig within servletconfig we are having servletcontext.


SOURCE : www.referjava.com

How to communicate between two servlets?

Forward or redirect from one Servlet to another.

2. Load the Servlet from ServletContext and access methods.

b.using RequestDispatcher interface

c. Servlet Collaboration or Communication can be achieved either by using Request Dispatcher's Forward or Include methods (available with request object) or Send Redirect method(available with response object). Servlet Context is also one of the ways to acheive servlet collaboration coz it is the one which provides the information of all the servlets.


SOURCE : www.referjava.com

What is the Servlet Interface?

The central abstraction in the Servlet API is the Servlet interface. All servlets implement this interface, either directly or, more commonly, by extending a class that implements it such as HttpServlet.Servlets-->Generic Servlet-->HttpServlet-->MyServlet.
The Servlet interface declares, but does not implement, methods that manage the servlet and its communications with clients. Servlet writers provide some or all of these methods when developing a servlet.


SOURCE : www.referjava.com

What method used to add a jsp to the servlet?

The RequestDispatcher.include (req, resp) is used to add a jsp to the
Servlet.


SOURCE : www.referjava.com

what is the need of super.init (config) in servlets

This will call the super class init method to initialize the object by config.
b. Basically super.init () method should be used whenever u is overriding the init method.... else i don’t think there is any necessity.


SOURCE : www.referjava.com

importance of deployment descriptor in servlet?

. Deployment descriptor file contain the resources available to the bean) what type of transactions that bean is using, name of home interface etc

b. The deployment descriptor is not confined to servlets. It belongs to the application. The deployment descriptor basically tell the container where to look out for the resources, which patterns to follow, how are the resources mapped etc


SOURCE : www.referjava.com

.When you have action=get?

By default, every request sent is by GET. Only when we explicitly mention that the method is post, is the request sent by post. click meOn clicking the above link, the data is send by get method. Here, when we submit the form, the data is sent via the post method


SOURCE : www.referjava.com

. Can an init (ServletConfig config) method be overriden in servlets?

This cannot be overriden.

b. The init () method of servlet can be overridden. Remember the init () method is overloaded in the GenericServlet class. But the difference lies in which init method are u calling. If you override the init (ServletConfig config) method, then you will have to call explicitly super.init(config) inside your code. If you override the init () method, then u don’t have to call the super.init () method explicitly


SOURCE : www.referjava.com

How will u pass the argument from one servlet to another servlet?

. By Request dispatching.
b. we can use forward and include of request dispatcher and the arguments would be request and response for these methods


SOURCE : www.referjava.com

What is the super class of all servlets?

The Class GenericServlet is the super class of all known servlets. This class is extended by other classes like HttpServlet. The GenericServlet class is protocol independent.


SOURCE : www.referjava.com