You are on page 1of 6

JSP Interview question

Contents Q: What is the life-cycle of JSP? Q: What is the jspInit() method? Q: What is the _jspService() method? Q: What is the jsp estroy() method? Q: !an "e override JSP lifecycle method? Q: #o" to override init() and destroy() method? Q: What is Implicit o$ject in JSP? Q: What are the scopes availa$le forJSP? Q: #o" can "e disa$le scriptin%? Q: ifference $et"een pa%e and pa%econte&t? Q: !an "e implement an interface in a JSP? Q: What is the difference $et"een Servlet!onte&t and Pa%e!onte&t? Q: ifference $et"een Servlet!onte&t and Servlet!onfi%? Q: What is the difference in 'sin% re('est)%et*e('est ispatcher() and conte&t)%et*e('est ispatcher()? Q: #o" to pass information from JSP to incl'ded JSP? Q: What is the difference $et"een directive incl'de and jsp incl'de? Q: #o" does JSP handle r'ntime e&ceptions? Q: #o" do yo' delete a !oo+ie "ithin a JSP? Q: ifference $et"een for"ard and send*edirect()? Q: What are different stmt availa$le and synta& and 'se of all? Q: #o" can "e create class level varia$le,method in JSP? Q: #o" to prevent cachin% for JSP pa%e? Q: IS JSP thread safe? Q: #o" to implement Sin%le -hread .odel? Q: ifferent "ay of Session trac+in%? Q: ifferent JSP /ction availa$le? Q: Scopes availa$le for JSP 'se $ean? Q: ifferent type of JSP comment availa$le? Q: Ifference $et"een Session0ean and 1ntity0ean?

Q: What is the life-cycle of JSP?


Ans: 1- Translation 2- Compilation 3- Loading the class 4- Instantiating the class 5- jspInit() invocation 6- _jspService() invocation 7- jspDestroy() invocation

Q: What is the jspInit() method?


Ans: The jspInit() method of the javax.servlet.jsp. sp!age interface is similar to the init() method of servlets. This method is invo"ed #y the container only once $hen a S! page is initiali%ed. It can #e overridden #y a page a&thor to initiali%e reso&rces s&ch as data#ase and net$or" connections' and to allo$ a S! page to read persistent config&ration data.

Q: What is the _jspService() method?


Ans: The _jspService() method of the javax.servlet.jsp.HttpJsp a!e interface is invo"ed every time a ne$ re(&est comes to a S! page. This method ta"es the HttpServlet"e#$est andHttpServlet"esp%nse o#jects as its arg&ments. ) page a&thor cannot override this method' as its implementation is provided #y the container.

Q: What is the jspDestroy() method?


Ans: The jsp&estr%'() method of the javax.servlet.jsp.Jsp a!e interface is invo"ed #y the container $hen a S! page is a#o&t to #e destroyed. This method is similar to the (estr%'() method of servlets. It can #e overridden #y a page a&thor to perform any clean&p operation s&ch as closing a data#ase connection.

Q: Can we override JSP lifecycle method?


Ans: init() and (estr%'() methods can #e overriden $hile service method cant #e overridden.

Q: How to override init() and destroy() method?


Ans: &eclarati%n statement can #e &sed for this p&rpose.

Q: What is Implicit object in JSP?


Ans: Implicit o#jects are java o#jects $hich Sp container provides developer to directly &se in S! page $itho&t needs to declare or initiating these o#jects. 1- re(&est 2- response 3- pageContext 4- session 5-)application 6- o&t 7- config *- page +- exception

Q: What are the scopes available forJSP?


Ans: pa!e)sc%pe:: It specifies that the o#ject $ill #e availa#le for the entire S! page #&t not o&tside the page. re(&est scope* It specifies that the o#ject $ill #e associated $ith a partic&lar re(&est and exist as long as the re(&est exists. applicati%n)sc%pe: It specifies that the o#ject $ill #e availa#le thro&gho&t the entire +e# application #&t not o&tside the application. sessi%n)sc%pe: It specifies that the o#ject $ill #e availa#le thro&gho&t the session $ith a partic&lar client.

Q: How can we disable scripting?


Ans: ,.jsp tr&e

Q: Difference between page and pagecontext?


Ans: Differences are 1- The page o#ject represents the generated servlet instance itself' i.e.' it is same as the -this- "ey$ord &sed in a ava file 2- The pageContext o#ject represents the environment for the page' contains information li"e page attri#&tes' access to the re(&est' response and session o#jects' as $ell as the sp+riter referenced #y o&t. 3- !age context also has methods for incl&ding another ./L0s contents' and for for$arding or redirecting to another ./L

Q: Can we implement an interface in a JSP?


Ans: 1o

Q: What is the difference between ServletContext and PageContext?


Ans: Servlet,%ntext: 2ives the information a#o&t the container. a!e,%ntext: 2ives the information a#o&t the /e(&est.

Q: Difference between ServletContext and ServletConfig?


Ans: Servlet,%n-i!: 3ne ServletConfig 3#ject is created per servlet It can #e &sed to access ServletContext !arameters are config&red in DD(deployment description)

Servlet,%ntext 3ne ServletContext $ill #e created per $e# application. Can #e &sed to access $e# app parameter. Can #e &sed to get server Info.

Q: What is the difference in using request.getRequestDispatcher() and context.getRequestDispatcher()?


Ans: re#$est.!et"e#$est&ispatc.er(pat.)* In order to create it $e need to give the relative path of the reso&rce' c%ntext.!et"e#$est&ispatc.er(pat.): In order to create it $e need to give the a#sol&te path of the reso&rce.

Q: How to pass information from JSP to included JSP?


Ans:).sing /0jsp:para12)tag.

Q: What is the difference between directive include and jsp include?


Ans:)/03)incl$(e2:).sed to incl&de static reso&rces d&ring translation time. S! incl&de* .sed to incl&de dynamic content or static content d&ring r&ntime.

Q: How does JSP handle runtime exceptions?


Ans: .sing error!age attri#&te of page directive and also $e need to specify)is4rr%r a!e5tr$e if the c&rrent page is intended to ./L redirecting of a S!.

Q: How do you delete a Cookie within a JSP?


Ans: Coo"ie mycoo" 4 ne$ Coo"ie(-name-'-val&e-)5 response.addCoo"ie(mycoo")5 Coo"ie "illmycoo" 4 ne$ Coo"ie(-mycoo"-'-val&e-)5 "illmycoo".set6ax)ge(7)5 "illmycoo".set!ath(-8-)5 "illmycoo".addCoo"ie("illmycoo")5

Q: Difference between forward and sendRedirect()?


Ans: Sen("e(irect()): In send/edirect() response is send #ac" to the #ro$ser $ith header information containing the page $hich servlet $ants #ro$ser to redirect &ser to. In this &ser get to "no$ that he has #een rero&ted and one extra re(&est is fired $hich is not in the case of 9or$ard. 6%r7ar((): It j&st rero&te the re(&est to other reso&rce(page8servlet). This rero&ting happens internal to server and .ser doesn0t "no$ a#o&t this rero&ting.

Q: What are different stmt available and syntax and use of all?
Ans: S! elements are Scriptlet:):;< ;= &irective:):;> ;= 4xpressi%n:):;4 ;=

Q: How can we create class level variable/method in JSP?


Ans: ?es $e can &sing Declaration in S!. S'ntax* :;< ;=

Q: How to prevent caching for JSP page?


Ans: :; response.set@eader(-CacheAControl-'-noAcache-)5 response.set@eader(-!ragma-'-noAcache-)5 response.setDate@eader (-Bxpires-' 7)5 ;=

Q: IS JSP thread safe?


Ans: 1o

Q: How to implement Single Thread Model?


Ans: :;> page4-- isthreadsafe4-tr&e- ;=

Q: Different way of Session tracking?


Ans: 1- Session Trac"ing 2- @idden field

Q: Different JSP Action available?


Ans: 1o

Q: Scopes available for JSP use bean?


Ans: 1o

Q: Different type of JSP comment available?


Ans: Comments Type 1-)H89:),%11ents): These comments $ill #e passed to the client side. Syntax * 2-)JS ),%11ents): These comments $ill not #e passed to client side. Syntax * :;AA AA4--=

Q: DIfference between SessionBean and EntityBean?


Ans: 1o

Q: Services provided by container?


Ans: 1- Transaction management for the #ean 2- Sec&rity for the #ean 3- !ersistence of the #ean 4- /emote access to the #ean 5- Lifecycle management of the #ean 6- Data#aseAconnection pooling 7- Instance pooling for the #ean

You might also like