The Following JSP interview questions, servlets interview questions, Java architect interview questions can help you get through an interview (1 to 20)
1)What is a singleton class?How do you write it? Illustrate with an example?
A class which must create only one object (ie. One instance) irrespective of any number of object created based on that class.
Steps to develop single ton class:-
(i)Create a Private construtor, so that outside class can not access this constructor. And declare a private static reference of same class
(ii)Write a public Factory method which creates an object. Assign this object to private static Reference and return the object
Sample Code:-
class SingleTon
{
private static SingleTon st=null;
private SingleTon()
{
System.out.println(“\nIn constructor”);
}
public static SingleTon stfactory()
{
if(st==null)
st=new SingleTon();
return st;
}
}
2) What is the difference between using HttpSession & a Stateful Session bean? Why can't HttpSession be used instead of of Session bean?
HttpSession is used to maintain the state of a client in webservers which are based on Http protocol. Where as Stateful Session bean is a type of bean which can also maintain the state of the client in Application servers based on RMI-IIOP
3)What is Temporary Servlet?
When we send a request to access a JSP, servlet container internally creates a 'servlet' & executes it. This servlet is called as 'Temporary servlet'. In general this servlet will be deleted immediately. to create & execute a servlet base on a JSP we can used following command.
Java weblogic.jspc—keepgenerated *.jsp
4) Generally Servlets are used for complete HTML generation. If you want to generate partial HTML's that include some static text (this should not be hard coded in servlets) as well as some dynamic text, what method do you use?
Using 'RequestDispather.include(“xx.html”) in the servlet code we can mix the partial static HTDirectoryML page.
Ex:- RequestDispatcher rd=ServletContext.getRequestDispatcher(“xx.html”);
rd.include(request,response);
5)Difference between jsp:forward and jsp:include tags?
jsp:forward action tag redirect the servlet Request and Servlet Response to another resource specified in the tag. The path of the resource is 'relative path'. The output generated by current JSP will be discarded.
jsp:include action tag process the resource as part of the current jsp. It include the o/p generated by resource, which is specified in the Tag to the current JSP
(*) In both the cases single Request proceses multiple resources
6)Purpose of jsp:plugin tag
It is used to generate client browser specified HTML tags which results in download of the java plugin software. And the execution of the applet or java Bean component that is specified in the tag.
7)Which method is called first each time a Servlet is invoked?
When the servlet obj is not created init() method will be called. It servlet object already exists service() method will be called.
8)Why DB connections are not written directly in JSP's?
The main purpose of JSP is to seperate the business logic & presentation logic. DB connections deal with pure java code. If write this kind of code in JSP, servlet container converts the JSP to servlet using JSP compiler which is the time consuming process so this is better to write code wich deals with DB connections, Authentication and Authorization etc. in the java bean and use the tag to perform the action.
9)What is key diffrence between using an jsp:forward tag and HttpServletResponse.sendRedirect()?
Ans: The jsp:forward action tag allows the request to be forwarded to another resource (servlet/jsp/HTML) which are available in the same web-application (context).
Where as in the case of Response.sendRedirect() the request to be forwarded/redirected to another resource in the same web-application or some other web-application in the same server, or to a resource on some other web-server. The key difference is by using jsp:forward tag
single request from a browser can process multiple resources. Where as by using SendRedirect() multiple requests take place in order to process multiple resources.
10)Why beans are used in J2EE architecture instead of writing all the code in jsp's?
In order to seperate the business logic and presentation logic beans are used. We write the business logic in the Beans. Where there is a change in business process we have to modify only the bean code not jsp code and viceversa.
11)How can a servlet can call JSP error page?
By simply including an error-page tag in “web.xml” file
error-page
exception-type javax.servlet.ServletException /exception-type
location /error.jsp /location
the placement of the error file i.e /errorjsp.jsp within the location tags and then the end tag of error-page
By using Response.sendRedirect(“error.jsp”); or by using RequestDispatcher.forward(“error.jsp”); we can call jsp error page in servlet.
12)Explain MVC (Model-View-Controller) architecture?
MVC:- In model view controller design pattern, the software has split into 3 pieces.
MODEL-> Responsible for Managing Data-> Ex. Java Beans
VIEW-> Responsible for generating the views->Ex. Jsp's
CONTROLLER-> Responsible for user interactions & co-ordinate with MODEL &VIEW
13)what are JSP implicit Objects:-
out: used to send the content to the browser
ex: out.println(“xx”)
Response: Represent HttpServlet Reques
Response: Represent HttpServletResponse
Page Context: used to access the objects like session, servlet context, Request, Response etc...
Session: Points to HttpSession objects
Application: Points to servlet context objects
Page: Points to current servlet objects
Exception: Points to Exception
14)JSP Action Tags, Directive Tags & Scripting Tags?
Action Tag: These tags effect the runtime behaviour of the jsp page and the response set back to client tags are :
jsp: usebean tag : instantiates a new jsp bean class
jsp: setProperty tag : setting the properties at runtime
jsp:param tag : used to sending parameters
jsp:include tag : include a resource at runtime
jsp:forward tag: forward the control to specific resource
jsp:plugin tag : to plugin a web resource url applet
Directives:
These tags effect the overall structure of the servlet that result from translation. 3 types of directives are defined.
Page directive: used to define and manipulate the no. of page dependent attributes that effect the whole jsp.
include directive: This instructs the container to include the content of the resource in the current jsp this file is parsed once, when translation is done.
Taglib directive: To use tag extentions to define custom tags.
Scriptlets: There are used to include javacode in jsp page these are of 3 types
scriptlet: used to write pure java code
declarations: used to declare java variables and methods
Expressions: These send the value of a java expression back to client. These results are converted to string and displayed at client.
15) jsp:useBean tag and it's properties?
To separate the code from the presentation it would be good idea to encapsulate the code in java object instantiate it and use it in our jsp. This instance is varaible is called 'id'.We can also specific life time of this object by using 'scope' attribute
jsp:useBean id='name' scope=”scope” class=”beanclass”/
Properties:
id: a case sensitive name is used.
Scope: The scope with in which the reference is available. page, request,session, application are possible values page is default
class: the fully qulified classes name
beanname: The name of the java beanclass
type: This type must be super class of the beans classes
16)How do you implement Singe Thread Model in JSP?
By simply include a page directives tag
%@page isThreadSafe=”false” %
17)How do you declare a page as Error Page. What tag you include in a JSP page so that it goes to specified error page in case of an exception?
By including a page directive tag %@ page isErrorPage=”true”%
then this page is including a page directive in our jsp pages
%@page errorpage=”url of the above jsp”% tag
calling this page is including a page directive in our jsp page
%@page errorpage=”url of the above jsp”%
18)What is Request Dispatcher? How do you get it?
The name itself reveals that it is used to dispatch the control to requesting resource. It is an interface used to include any resource with in our servlet or forward the controler to any other resource. It contains two methods.
Forward(“---url”);
forward(“---url”);
For getting this we have a method in servlet context interface. So we can get it by servlet context object
sc.getRequestDispatcher();
19) Difference between JSP & Servlets?
Both are web applications used to produce web content that means dynamic web pages. Bot are used to take the requests and service the clients. The main difference between them is, In servlets both the presentation and business logic are place it together. Where as in jsp both are separated by defining java beans .
In jsp's the overall code is modulated so the developer who deesn't know about java can write jsp pages by simply knowing the additional tages and class names.
One more important to be considered is servlet take less time to compile. Jsp is a tool to simplify the process and make the process automate.
20)Difference between Hashtable & HashMap?
Hash table is a Collection of objects .To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method. Hashtable is Sychronized and permit non- null values only.
Hashtable maps keys to values. Any non-null object can be used as a key or as a value.
The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.
No comments:
Post a Comment