1. What is the difference between JSP and Servlets ? | ||||||||||||||||||||||||||||||
JSP is used mainly for presentation only. A JSP can only be HttpServlet that means the only supported protocol in JSP is HTTP. But a servlet can support any protocol like HTTP, FTP, SMTP etc. | ||||||||||||||||||||||||||||||
2. What is difference between custom JSP tags and beans? | ||||||||||||||||||||||||||||||
Custom JSP tag is a tag you defined. You define how a tag, its attributes and its body are interpreted, and then group your tags into collections called tag libraries that can be used in any number of JSP files. To use custom JSP tags, you need to define three separate components: the tag handler class that defines the tag's behavior ,the tag library descriptor file that maps the XML element names to the tag implementations and the JSP file that uses the tag library JavaBeans are Java utility classes you defined. Beans have a standard format for Java classes. You use tags Custom tags and beans accomplish the same goals -- encapsulating complex behavior into simple and accessible forms. There are several differences: Custom tags can manipulate JSP content; beans cannot. Complex operations can be reduced to a significantly simpler form with custom tags than with beans. Custom tags require quite a bit more work to set up than do beans. Custom tags usually define relatively self-contained behavior, whereas beans are often defined in one servlet and used in a different servlet or JSP page. Custom tags are available only in JSP 1.1 and later, but beans can be used in all JSP 1.x versions. | ||||||||||||||||||||||||||||||
3. How many JSP scripting elements are there and what are they? | ||||||||||||||||||||||||||||||
There are three scripting language elements: declarations, scriptlets, expressions. | ||||||||||||||||||||||||||||||
4. How do I include static files within a JSP page? | ||||||||||||||||||||||||||||||
Static resources should always be included using the JSP include directive. This way, the inclusion is performed just once during the translation phase. | ||||||||||||||||||||||||||||||
5. What are the lifecycle of JSP? | ||||||||||||||||||||||||||||||
When presented with JSP page the JSP engine does the following 7 phases. » Page translation: -page is parsed, and a java file which is a servlet is created. » Page compilation: page is compiled into a class file » Page loading : This class file is loaded. » Create an instance :- Instance of servlet is created » jspInit() method is called » _jspService is called to handle service calls » _jspDestroy is called to destroy it when the servlet is not required. | ||||||||||||||||||||||||||||||
6. What is a Expression? | ||||||||||||||||||||||||||||||
Expressions are act as place holders for language expression, expression is evaluated each time the page is accessed. This will be included in the service method of the generated servlet. | ||||||||||||||||||||||||||||||
7. What is a Declaration? | ||||||||||||||||||||||||||||||
It declares one or more variables or methods for use later in the JSP source file. A declaration must contain at least one complete declarative statement. You can declare any number of variables or methods within one declaration tag, as long as semicolons separate them. The declaration must be valid in the scripting language used in the JSP file. This will be included in the declaration section of the generated servlet. | ||||||||||||||||||||||||||||||
8. What is a Scriptlet? | ||||||||||||||||||||||||||||||
A scriptlet can contain any number of language statements, variable or expressions that are valid in the page scripting language. Within scriptlet tags, you can declare variables to use later in the file, write expressions valid in the page scripting language, use any of the JSP implicit objects or any object declared with a <jsp:useBean>. Generally a scriptlet can contain any java code that are valid inside a normal java method. This will become the part of generated servlet's service method. | ||||||||||||||||||||||||||||||
9. What are the implicit objects? | ||||||||||||||||||||||||||||||
A Certain objects that are available for the use in jsp documents without being declared first. These objects are parsed by the JSP engine and inserted into the generated servlet. The implicit objects are: request, response, pageContext, session, application, out, config, page, exception . | ||||||||||||||||||||||||||||||
10. What's the difference between forward and sendRedirect? | ||||||||||||||||||||||||||||||
forward is server side redirect and sendRedirect is client side redirect. When you invoke a forward request, the request is sent to another resource on the server, without the client being informed that a different resource is going to process the request. This process occurs completely with in the web container And then returns to the calling method. When a sendRedirect method is invoked, it causes the web container to return to the browser indicating that a new URL should be requested. Because the browser issues a completely new request any object that are stored as request attributes before the redirect occurs will be lost. This extra round trip a redirect is slower than forward. Client can disable sendRedirect. | ||||||||||||||||||||||||||||||
11. What are the different scopes available ? | ||||||||||||||||||||||||||||||
page, request, session, application | ||||||||||||||||||||||||||||||
12. Is JSP extensible ? | ||||||||||||||||||||||||||||||
Yes, it is. JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries. | ||||||||||||||||||||||||||||||
13. How do I use a scriptlet to initialize a newly instantiated bean? | ||||||||||||||||||||||||||||||
A jsp:useBean action may optionally have a body. If the body is specified, its contents will be automatically invoked when the specified bean is instantiated (Only at the time of instantiation.) Typically, the body will contain scriptlets or jsp:setProperty tags to initialize the newly instantiated bean, although you are not restricted to using those alone. | ||||||||||||||||||||||||||||||
14. What is jsp? | ||||||||||||||||||||||||||||||
JSP is a server side scripting technology. JSP allows Java as well as a few special tags to be embedded into a web file (HTML/XML, etc). The suffix must ends with .jsp. | ||||||||||||||||||||||||||||||
15. What are JSP Actions? | ||||||||||||||||||||||||||||||
JSP actions use constructs in XML syntax to control the behavior of the servlet engine. You can dynamically insert a file, reuse JavaBeans components, forward the user to another page, or generate HTML for the Java plugin. Available actions include: jsp:include, jsp:useBean, jsp:setProperty, jsp:getProperty, jsp:forward and Jsp: plugin. | ||||||||||||||||||||||||||||||
16. What is a output comment? | ||||||||||||||||||||||||||||||
A comment that is sent to the client in the viewable page source. The JSP engine handles an output comment as un interpreted HTML text, returning the comment in the HTML output sent to the client. You can see the comment by viewing the page source from your Web browser. | ||||||||||||||||||||||||||||||
17. What is a Hidden Comment | ||||||||||||||||||||||||||||||
Hidden Comments are JSP comments. A comments that documents the JSP page but is not sent to the client. The JSP engine ignores a hidden comment, and does not process any code within hidden comment tags. | ||||||||||||||||||||||||||||||
18. How to pass information from JSP to included JSP? | ||||||||||||||||||||||||||||||
By using <jsp:param> tag. | ||||||||||||||||||||||||||||||
19. What is the better way to enable thread-safe servlets and JSPs? SingleThreadModel Synchronization? | ||||||||||||||||||||||||||||||
The better approach is to use synchronization. Because SingleThreadModel is not scalable. SingleThreadModel is pretty resource intensive from web server's perspective. The most serious issue is when the number of concurrent requests exhaust the servlet instance pool. In that case, all the un serviced requests are queued until something becomes free - which results in poor performance. | ||||||||||||||||||||||||||||||
20. What is the difference between ServletContext and PageContext? | ||||||||||||||||||||||||||||||
ServletContext gives the information about the container and PageContext gives the information about the Request. | ||||||||||||||||||||||||||||||
21. Why in Servlet 2.4 specification SingleThreadModel has been deprecated? | ||||||||||||||||||||||||||||||
SingleThreadModel is pretty resource intensive from web server's perspective. When the number of concurrent requests exhaust the servlet instance pool, all the un serviced requests are queued until something becomes free - which results in poor performance. | ||||||||||||||||||||||||||||||
22. How do you pass data (including JavaBeans) to a JSP from a servlet? | ||||||||||||||||||||||||||||||
By forwarding the request to the servlet ( the data must be there in the request scope) we can pass the data from a JSP to servlet. Also we can use a session to pass the data. | ||||||||||||||||||||||||||||||
23. What JSP lifecycle methods we can override? | ||||||||||||||||||||||||||||||
You cannot override the _jspService() method within a JSP page. You can however, override the jspInit() and jspDestroy() methods within a JSP page. | ||||||||||||||||||||||||||||||
24. How can you declare methods in your JSP page? | ||||||||||||||||||||||||||||||
You can declare methods as declarations in your JSP page. The methods can then be invoked within any other methods you declare, or within JSP scriptlets and expressions. | ||||||||||||||||||||||||||||||
25. How do you pass an init parameter to a JSP? | ||||||||||||||||||||||||||||||
You need to configure the DD for passing init parameter to a JSP. You can configure the DD as follows. <servlet> <servlet-name>test.jsp</servlet-name> <jsp-file>test.jsp</jsp-name> <init-param> <param-name>Abc</param-name> <param-value>Xyz</param-value> </init-param> </servlet> | ||||||||||||||||||||||||||||||
26. When a session object gets added or removed to the session, which event will get notified ? | ||||||||||||||||||||||||||||||
HttpSessionBindingListener will get notified When an object is added and/or removed from the session object, or when the session is invalidated, in which case the objects are first removed from the session, whether the session is invalidated manually or automatically (timeout). | ||||||||||||||||||||||||||||||
27. How can I print the stack trace of an exception from a JSP page? | ||||||||||||||||||||||||||||||
By creating an object of PrintWriter we can print the stack trace in JSP page. Eg: PrintWriter pw = response.getWriter(); exception.printStackTrace(pw); | ||||||||||||||||||||||||||||||
28. Do objects stored in a HTTP Session need to be serializable? Or can it store any object? | ||||||||||||||||||||||||||||||
No, the objects need not to be serializable. We can store any type of objects in session. | ||||||||||||||||||||||||||||||
29. What is the differecnce between JspWriter and PrintWriter? | ||||||||||||||||||||||||||||||
JspWriter is buffered. | ||||||||||||||||||||||||||||||
30. How will you include a static file in a JSP page? | ||||||||||||||||||||||||||||||
You can include a static resource to a JSP using <jsp:directive > or <%@ inlcude >. | ||||||||||||||||||||||||||||||
31. How you can perform browser redirection? | ||||||||||||||||||||||||||||||
We can use the method sendRedirect of HttpServletResponse or forward method of RequestDispatcher. | ||||||||||||||||||||||||||||||
32. Can we use ServletOutputStream object from a JSP page? | ||||||||||||||||||||||||||||||
No. You are supposed to use JSPWriter object (given to you in the form of the implicit object out) only for replying to clients. | ||||||||||||||||||||||||||||||
33. How can you stop JSP execution in the middle of processing a request? | ||||||||||||||||||||||||||||||
We can use the return statement to stop the processing of JSP. Because JSP is compiled to servlet and all the statements will go inside service method, any time you can stop the processing using return statement. | ||||||||||||||||||||||||||||||
34. How can I invoke a JSP error page from a servlet? | ||||||||||||||||||||||||||||||
You can invoke the JSP error page and pass the exception object to it from within a servlet. For that you need to create a request dispatcher for the JSP error page, and pass the exception object as a javax.servlet.jsp.jspException request attribute. | ||||||||||||||||||||||||||||||
35. How will you pass information from JSP to included JSP? | ||||||||||||||||||||||||||||||
By using <%jsp:param> tag. | ||||||||||||||||||||||||||||||
36. How does JSP handle runtime exceptions? | ||||||||||||||||||||||||||||||
Using errorPage attribute of page directive JSP handles runtime exceptions. We need to specify isErrorPage=true if the current page is intended to use as a JSP error page. | ||||||||||||||||||||||||||||||
37. How can I enable session tracking for JSP pages if the browser has disabled cookies? | ||||||||||||||||||||||||||||||
By default session tracking uses cookies to associate a session identifier with a user. If the browser does not support cookies, or if cookies are disabled, you can still use session tracking using URL rewriting. For URL rewriting to be effective, you need to append the session ID for each and every link that is part of your servlet response. By using the methods response.encodeURL() and response.encodeRedirectURL() we can achieve this. | ||||||||||||||||||||||||||||||
38. What do you understand by JSP Actions? | ||||||||||||||||||||||||||||||
JSP actions are XML tags that direct the server to use existing components or control the behavior of the JSP engine. JSP Actions consist of a typical (XML-based) prefix of "jsp" followed by a colon, followed by the action name followed by one or more attribute parameters. There are six JSP Actions: <jsp:include/> <jsp:forward/> <jsp:plugin/> <jsp:usebean/> <jsp:setProperty/> <jsp:getProperty/> | ||||||||||||||||||||||||||||||
39. What is the difference between <jsp:forward page = ... > and response.sendRedirect(url),?. | ||||||||||||||||||||||||||||||
The <jsp:forward> element forwards the request object containing the client request information from one JSP file to another file. The target file can be an HTML file, another JSP file, or a servlet, as long as it is in the same application context as the forwarding JSP file. sendRedirect sends HTTP temporary redirect response to the browser, and browser creates a new request to go the redirected page. The response.sendRedirect kills the session variables. | ||||||||||||||||||||||||||||||
40. Identify the advantages of JSP over Servlet. | ||||||||||||||||||||||||||||||
a) Embedding of Java code in HTML pages b) Platform independence c) Creation of database-driven Web applications d) Server-side programming capabilities | ||||||||||||||||||||||||||||||
41. What are implicit Objects available to the JSP Page? | ||||||||||||||||||||||||||||||
Implicit objects are the objects available to the JSP page. These objects are created by Web container and contain information related to a particular request, page, or application. The JSP implicit objects are: | ||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||
42. What are all the different scope values for the <jsp:useBean> tag? | ||||||||||||||||||||||||||||||
<jsp:useBean> tag is used to use any java object in the jsp page. Here are the scope values for <jsp:useBean> tag: a) page b) request c) session and d) application | ||||||||||||||||||||||||||||||
43. What is JSP Output Comments? | ||||||||||||||||||||||||||||||
JSP Output Comments are the comments that can be viewed in the HTML source file. Example: <!-- This file displays the user login screen --> and <!-- This page was loaded on <%= (new java.util.Date()).toLocaleString() %> --> | ||||||||||||||||||||||||||||||
44. What is expression in JSP? | ||||||||||||||||||||||||||||||
Expression tag is used to insert Java values directly into the output. Syntax for the Expression tag is: <%= expression %> An expression tag contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file. The following expression tag displays time on the output: <%=new java.util.Date()%> | ||||||||||||||||||||||||||||||
45. What types of comments are available in the JSP? | ||||||||||||||||||||||||||||||
There are two types of comments are allowed in the JSP. These are hidden and output comments. A hidden comments does not appear in the generated output in the html, while output comments appear in the generated output. Example of hidden comment: <%-- This is hidden comment --%> Example of output comment: <!-- This is output comment --> | ||||||||||||||||||||||||||||||
46. What is JSP declaration? | ||||||||||||||||||||||||||||||
JSP Decleratives are the JSP tag used to declare variables. Declaratives are enclosed in the <%! %> tag and ends in semi-colon. You declare variables and functions in the declaration tag and can use anywhere in the JSP. Here is the example of declaratives: <%@page contentType="text/html" %> <html> <body> <%! int cnt=0; private int getCount(){ //increment cnt and return the value cnt++; return cnt; } %> Values of Cnt are: <%=getCount()%> </body> </html> | ||||||||||||||||||||||||||||||
47. What is JSP Scriptlet? | ||||||||||||||||||||||||||||||
JSP Scriptlet is jsp tag which is used to enclose java code in the JSP pages. Scriptlets begins with <% tag and ends with %> tag. Java code written inside scriptlet executes every time the JSP is invoked. Example: <% //java codes String userName=null; userName=request.getParameter("userName"); %> | ||||||||||||||||||||||||||||||
48. What are the life-cycle methods of JSP? | ||||||||||||||||||||||||||||||
Life-cycle methods of the JSP are: a) jspInit(): The container calls the jspInit() to initialize the servlet instance. It is called before any other method, and is called only once for a servlet instance. b)_jspService(): The container calls the _jspservice() for each request and it passes the request and the response objects. _jspService() method cann't be overridden. c) jspDestroy(): The container calls this when its instance is about to destroyed. The jspInit() and jspDestroy() methods can be overridden within a JSP page. |
Sunday, April 29, 2012
JSP INTERVIEW QUESTIONS
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment