InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
What’s the difference between JavaBeans and taglib directives? |
|
Answer» JavaBeans and taglib fundamentals were introduced for reusability. But following are the major differences between them −
|
|
| 2. |
How does JSP engines instantiate tag handler classes instances? |
|
Answer» JSP engines will always instantiate a new tag handler instance every time a tag is encountered in a JSP page. A pool of tag instances are maintained and reusing them where possible. When a tag is encountered, the JSP engine will try to find a Tag instance that is not being used and use the same and then release it. |
|
| 3. |
What are the options in JSP to include files? |
|
Answer» In JSP, we can perform inclusion in the following ways −
<% include file = ”header.jsp” %>
<% include file = ”header.jsp” %>
<% pageContext.include(“/header.jsp”); %>
<% RequestDispatcher rd = request.getRequestDispatcher(“/header.jsp”); Rd.include(request,response);%> |
|
| 4. |
When do use application scope? |
|
Answer» If we want to make our data available to the entire application then we have to use application scope. |
|
| 5. |
How is scripting disabled? |
|
Answer» Scripting is disabled by setting the scripting-invalid element of the deployment descriptor to true. It is a subelement of jsp-property-group. Its valid values are true and false. The syntax for disabling scripting is as follows − <jsp-property-group> <url-pattern>*.jsp</url-pattern> <scripting-invalid>true</scripting-invalid></jsp-property-group> |
|
| 6. |
A JSP page, include.jsp, has a instance variable "int a", now this page is statically included in another JSP page, home.jsp, which also has an instance variable "int a" declared. What happens when the home.jsp page is requested by the client? |
|
Answer» It causes compilation error, as two variables with same name can't be declared. This happens because, when a page is included statically, entire code of included page becomes part of the new page. at this time there are two declarations of variable 'a'. Hence compilation error. |
|
| 7. |
Why is _jspService() method starting with an '_' while other life cycle methods do not? |
|
Answer» _jspService() method will be written by the container hence any methods which are not to be overridden by the end user are typically written starting with an '_'. This is the reason why we don't override _jspService() method in any JSP page. |
|
| 8. |
Can we override the jspInit(), _jspService() and jspDestroy() methods? |
|
Answer» We can override jspinit() and jspDestroy() methods but not _jspService(). |
|
| 9. |
How to pass information from JSP to included JSP? |
|
Answer» Using <%jsp:param> tag. |
|
| 10. |
What is the page directive is used to prevent a JSP page from automatically creating a session? |
|
Answer» <% page session = "false"> |
|
| 11. |
Can you make use of a ServletOutputStream object from within a JSP page? |
|
Answer» No. You are supposed to make use of only a JSPWriter object (given to you in the form of the implicit object out) for replying to clients. A JSPWriter can be viewed as a buffered version of the stream object returned by response.getWriter(), although from an implementational perspective, it is not. |
|
| 12. |
How do you pass control from one JSP page to another? |
|
Answer» Use the following ways to pass control of a request from one servlet to another or one jsp to another −
|
|
| 13. |
Can a JSP page process HTML FORM data? |
|
Answer» Yes. However, unlike Servlet, you are not required to implement HTTP-protocol specific methods like doGet() or doPost() within your JSP page. You can obtain the data for the FORM input elements via the request implicit object within a scriptlet or expression. |
|
| 14. |
How do I include static files within a JSP page? |
|
Answer» Static resources should always be included using the JSP include directive. This way, the inclusion is performed just once during the translation phase. Do note that you should always supply a relative URL for the file attribute. Although you can also include static resources using the action, this is not advisable as the inclusion is then performed for each and every request. |
|
| 15. |
Is JSP technology extensible? |
|
Answer» YES. JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries. |
|
| 16. |
What is difference between and ? |
|
Answer» <%-- comment --%> is JSP comment and is ignored by the JSP engine. <!-- comment --> is an HTML comment and is ignored by the browser. |
|
| 17. |
What is locale? |
|
Answer» This is a particular cultural or geographical region. It is usually referred to as a language symbol followed by a country symbol which are separated by an underscore. For example "en_US" represents english locale for US. |
|
| 18. |
What is Localization? |
|
Answer» Localization means adding resources to a web site to adapt it to a particular geographical or cultural region for example Hindi translation to a web site. |
|
| 19. |
In JSP page how can we handle runtime exception? |
|
Answer» We can use the errorPage attribute of the page directive to have uncaught run-time exceptions automatically forwarded to an error processing page. Example: <% page errorPage = "error.jsp" %> It will redirect the browser to the JSP page error.jsp if an uncaught exception is encountered during request processing. Within error.jsp, will have to indicate that it is an error-processing page, using the directive: <% page isErrorPage="true" %> |
|
| 20. |
What type of errors you might encounter in a JSP code? |
Answer»
|
|
| 21. |
How can we disable EL ? |
|
Answer» We can disable using isELIgnored attribute of the page directive − <% page isELIgnored = "true|false" %> If it is true, EL expressions are ignored when they appear in static text or tag attributes. If it is false, EL expressions are evaluated by the container. |
|
| 22. |
What are the implicit EL objects in JSP ? |
|
Answer» The JSP expression language supports the following implicit objects −
|
|
| 23. |
What is JSP Expression Language? |
|
Answer» JSP Expression Language (EL) makes it possible to easily access application data stored in JavaBeans components. JSP EL allows you to create expressions both (a) arithmetic and (b) logical. A simple syntax for JSP EL is − ${expr} Here expr specifies the expression itself. |
|
| 24. |
What is a JSP custom tag? |
|
Answer» A custom tag is a user-defined JSP language element. When a JSP page containing a custom tag is translated into a servlet, the tag is converted to operations on an object called a tag handler. The Web container then invokes those operations when the JSP page's servlet is executed. |
|
| 25. |
What are JSTL XML tags? |
|
Answer» The JSTL XML tags provide a JSP-centric way of creating and manipulating XML documents. Following is the syntax to include JSTL XML library in your JSP. <% taglib prefix = "x" uri = "http://java.sun.com/jsp/jstl/xml" %> |
|
| 26. |
What are JSTL SQL tags? |
|
Answer» The JSTL SQL tag library provides tags for interacting with relational databases (RDBMSs) such as Oracle, mySQL, or Microsoft SQL Server. Following is the syntax to include JSTL SQL library in your JSP − <% taglib prefix = "sql" uri = "http://java.sun.com/jsp/jstl/sql" %> |
|
| 27. |
What are JSTL formatting tags ? |
|
Answer» The JSTL formatting tags are used to format and display text, the date, the time, and numbers for internationalized Web sites. Following is the syntax to include Formatting library in your JSP − <% taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %> |
|
| 28. |
What is the use of tag? |
|
Answer» The <c:url> tag formats a URL into a string and stores it into a variable. This tag automatically performs URL rewriting when necessary. |
|
| 29. |
What is the use of tag? |
|
Answer» The <c:redirect > tag redirects the browser to an alternate URL by providing automatically URL rewriting, it supports context-relative URLs, and it supports the <c:param> tag. |
|
| 30. |
What is the use of tag? |
|
Answer» The <c:param> tag allows proper URL request parameter to be specified with URL and it does any necessary URL encoding required. |
|
| 31. |
What is the use of , tag? |
|
Answer» The <c:forEach >, <c:forTokens> tags exist as a good alternative to embedding a Java for, while, or do-while loop via a scriptlet. |
|
| 32. |
What is the use of tag? |
|
Answer» The <c:choose> works like a Java switch statement in that it lets you choose between a number of alternatives. Where the switch statement has case statements, the <c:choose> tag has <c:when> tags. A a switch statement has default clause to specify a default action and similar way <c:choose> has <otherwise> as default clause. |
|
| 33. |
What is the use of tag? |
|
Answer» The <c:if> tag evaluates an expression and displays its body content only if the expression evaluates to true. |
|
| 34. |
What is the use of tag? |
|
Answer» The <c:catch> tag catches any Throwable that occurs in its body and optionally exposes it. Simply it is used for error handling and to deal more gracefully with the problem. |
|
| 35. |
What is the use of tag? |
|
Answer» The <c:remove > tag removes a variable from either a specified scope or the first scope where the variable is found (if no scope is specified). |
|
| 36. |
What is the use of tag? |
|
Answer» The <c:set > tag is JSTL-friendly version of the setProperty action. The tag is helpful because it evaluates an expression and uses the results to set a value of a JavaBean or a java.util.Map object. |
|
| 37. |
What the different types of JSTL tags are ? |
|
Answer» Types of JSTL tags are −
|
|
| 38. |
How do you implement the auto refresh in JSP? |
|
Answer» The simplest way of refreshing a Webpage is using method setIntHeader() of response object. Following is the signature of this method − public void setIntHeader(String header, int headerValue) This method sends back header "Refresh" to the browser along with an integer value which indicates time interval in seconds. |
|
| 39. |
What is auto refresh feature? |
|
Answer» Consider a webpage which is displaying live game score or stock market status or currency exchange ration. For all such type of pages, you would need to refresh your Webpage regularly using refresh or reload button with your browser. JSP makes this job easy by providing you a mechanism where you can make a webpage in such a way that it would refresh automatically after a given interval. |
|
| 40. |
How can you implement hit counter to avoid loss of count data with each restart of the application? |
|
Answer» You can follow the below steps −
|
|
| 41. |
How do you implement hit counter in JSP? |
|
Answer» To implement a hit counter you can make use of Application Implicit object and associated methods getAttribute() and setAttribute(). This object is a representation of the JSP page through its entire lifecycle. This object is created when the JSP page is initialized and will be removed when the JSP page is removed by the jspDestroy() method. |
|
| 42. |
What is a hit count for a Webpage? |
|
Answer» A hit counter tells you about the number of visits on a particular page of your web site. |
|
| 43. |
What is the difference between and response.sendRedirect(url)? |
|
Answer» 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. |
|
| 44. |
What is JSP page redirection? |
|
Answer» Page redirection is generally used when a document moves to a new location and we need to send the client to this new location or may be because of load balancing, or for simple randomization. |
|
| 45. |
Where will be the uploaded files stored? |
|
Answer» You can hard code this in your program or this directory name could also be added using an external configuration such as a context-param element in web.xml. |
|
| 46. |
How can you upload a file using JSP? |
|
Answer» To upload a single file you should use a single <input .../> tag with attribute type = "file".To allow multiple files uploading, include more than one input tags with different values for the name attribute. |
|
| 47. |
How can you delete a session data? |
|
Answer» When you are done with a user's session data, you have several options −
|
|
| 48. |
How is Session Management done in JSP? |
|
Answer» Session management can be achieved by the use of −
<input type = "hidden" name = "sessionid" value = "12345">
This implies that when the form is submitted, the specified name and value will be getting included in GET or POST method. |
|
| 49. |
How to delete cookies with JSP? |
|
Answer» To delete cookies is very simple. If you want to delete a cookie then you simply need to follow up following three steps −
|
|
| 50. |
How to read cookies with JSP? |
|
Answer» To read cookies, you need to create an array of javax.servlet.http.Cookie objects by calling the getCookies( ) method of HttpServletRequest. Then cycle through the array, and use getName() and getValue() methods to access each cookie and associated value. |
|