100000380
Points
Questions
185
Answers
186
-
A PageContext is an instance of a javax.servlet.jsp.PageContext object that provides a context to store references of objects used by the JSP page. PageContext is used to get and set attributes with different scopes like page, request, session and application and to forward request to other resources.
This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.
- 1366 views
- 1 answers
- 0 votes
-
Cookie can be deleted by setting setMaxAge(0) on the cookie object. A cookie, democookie, can be deleted using the following scriptlet:
<% Cookie delCookie = new Cookie("democookie", null); delCookie.setMaxAge(0); delCookie.setPath("/"); response.addCookie(delCookie); %>
This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.
- 1241 views
- 1 answers
- 0 votes
-
The implicit object ‘exception‘ is not available to all JSP pages. The ‘exception’ object is available only to JSP error pages. JSP error pages are pages to which processing is forwarded when an exception is thrown from another JSP page. The implicit ‘exception’ object represents the uncaught exception that was thrown from another JSP page and that resulted in this error page being invoked.
The exception object is accessible only from the JSP error page instance to which processing was forwarded when the exception was encountered. JSP page can be marked as an error page by setting the attribute of the page directive ‘isErrorPage‘ to ‘true‘.
This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.
- 1270 views
- 1 answers
- 0 votes
-
<%! … %> are JSP Declaration tags while <% … %> are JSP Scriptlet tags.
In JSP declarations – <%! code %>, the code is inserted into the body (outside of the methods) of the servlet class generated from the JSP page.
In Scriptlet – <% code %>, the code goes into the _jspService() method of the servlet generated from the JSP page. The code is visible only to the _jspService() method of the servlet class.
This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.
- 1371 views
- 1 answers
- 0 votes
-
Yes, methods can be written inside a JSP page.
They have to be written using JSP declarations <%! ……… %>. The JSP declarations puts the method outside of any method in the generated servlet. Methods can’t be written inside scriptlet because any code written inside scriptlet goes into the _jspService() method of the generated servlet. However writing methods in JSP page is not recommended, instead preferred way is to use custom tag libraries.
This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.
- 1207 views
- 1 answers
- 0 votes
-
An alternative to synchronization is available in the form of thread-local variables. A thread-local variable is one whose value at any one time is linked to which thread it is being accessed from. In other words, it has a separate value per thread. Each thread maintains its own, separate map of thread-local variable values.
Thread-local variables are used via the ThreadLocal class in Java. Every thread has its own ThreadLocal variable and they can use it’s get() and set() methods to get the default value or change its value local to Thread. ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread.
This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.
- 3665 views
- 1 answers
- 0 votes
-
No, constructors are not inherited in Java. A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.
- 2612 views
- 1 answers
- 0 votes
-
Let us understand the difference with the example below:
<%! int x=10;Â %> <% Â Â Â Â Â Â Â int y=100; Â Â Â Â Â Â Â out.println("Hello...."); %>Â
In this example, ‘x’ is the variable that is declared inside the declarative with a value ’10’ and ‘y’ is the variable that is declared inside a scriptlet with a value ‘100’.
The variable x declared using declaration goes into the servlet class (outside any method) generated from the JSP page which is accessible to all methods of the class whereas the variable y declared inside scriptlet goes into the _jspService() method of the servlet generated, which is accessible only inside the _jspService() method.
This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.
- 1247 views
- 1 answers
- 0 votes
-
In JSPs there are multiple ways to handle the exceptions :
- Using errorPage and isErrorPage attribute of page directive
- Using <error-page> element in the web.xml file
- Using Java Scriplets within a JSP file
Page Directive Attributes – errorPage and isErrorPage:
Any uncaught exceptions thrown in the body of the JSP page implementation class result in the forwarding of the client request and uncaught exception to the errorPage URL specified by the JSP page directive.
For example:
<%@ page errorPage="error.jsp" %>
redirects the browser to the JSP page error.jsp if an uncaught exception is encountered during request processing. Within error.jsp, to indicate that it is an error-processing page, isErrorPage attribute must be set to true in the page directive:
<%@ page isErrorPage="true" %>
The Throwable object describing the exception may be accessed within the error page via the exception implicit object only if isErrorPage attribute is set to true.
<%@ page isErrorPage="true" %> ... <%= exception.getMessage() %>
<error-page> element in the web.xml file:
This approach is better alternative as the user doesn’t need to use the page directive to declare the error page in each JSP file. Making a single entry in the web.xml file serves the purpose. We can either specify the exception type or the error code with the location element.
<error-page> <exception-type> java.lang.Exception </exception-type> <location> /error.jsp </location> </error-page>
Java Scriptlet:
This approach is the traditional way of handling exceptions. This is not considered to be the best way of handling exception but used in case fast debugging is required. In this approach the developer can simply use the try … catch .. format within the scriptlet like any normal Java code.
This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.
- 1514 views
- 1 answers
- 0 votes
-
Question:
public class A { public A(int x) { System.out.println("Constructor invoked "+x); } }
Answer:
Yes, this is an error condition. Since constructors are not inherited, results in the below compilation error.
Constructor A in class A cannot be applied to given types;
public class B extends A{ ^
required: int
found: no arguments
reason: actual and formal argument lists differ in length
This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.
- 1352 views
- 1 answers
- 0 votes