1. Explain JSP and tell its uses.
JSP stands for Java Server Pages. It is a presentation layer technology independent of the platform. They are like HTML pages but with Java code pieces embedded in them. They are saved with a .jsp extension. They are compiled using the JSP compiler in the background and generate a Servlet from the page.
2. What is the requirement of a tag library?
A collection of custom tags is called a Tag Library. Recurring tasks are handled more easily and reused across multiple applications to increase productivity. They are used by Web Application designers who focus on presentation rather than accessing database or other services. Some popular libraries are String tag library and Apache display tag library.
3. Explain JSP Technology.
JSP is a standard extension of Java and is defined on top of Servlet extensions. Its goal is to simplify management and creation of dynamic web pages. It is platform-independent, secure, and it makes use of Java as a server side scripting language.
4. Explain Implicit objects in JSP.
Objects created by web container and contain information regarding a particular request, application or page are called Implicit Objects. They are :
5. How can multiple submits due to refresh button clicks be prevented?
Using a Post/Redirect/Get or a PRG pattern, this problem can be solved.
- 1). A form filed by the user is submitted to the server using the POST or GET method. The state in the database and business model are updated.
- 2). A redirect response is used to reply by the servlet for a view page.
- 3). A view is loaded by the browser using the GET command and no user data is sent. This is safe from multiple submits as it is a separate JSP page.
6. Is JSP technology extensible?
Yes, JSP is easily extensible by use and modification of tags, or custom actions, encapsulated in tag libraries.
7. Differentiate between response.sendRedirect(url) and <jsp:forward page = …> .
<jsp.forward> element forwards the request object from 1 JSP file to another. Target file can be HTML, servlet or another JSP file, but it should be in the same application context as forwarding JSP file.
sendRedirect send HTTP temporary redirect response to the browser. The browser then creates a new request for the redirected page. It kills the session variables.
8. Can a subsequent request be accessed with one’s servlet code, if a request attribute is already sent in his JSP?
The request goes out of scope, thus, it cannot be accessed. However, if a request attribute is set in one’s servlet, then it can be accessed in his JSP.
A JSP is a server side component and the page in translated to a Java servlet, and then executed. Only HTML code is given as output.
9. How to include static files in a JSP page?
Static pages are always included using JSP include directive. This way the inclusion is performed in the translation phase once. Note that a relative URL must be supplied for file attribute. Although static resources may be included, it is not preferred as each request requires inclusion.
10. Why is it that JComponent have add() and remove() methods but Component doesn’t?
JComponent is a subclass of Container. It contains other Components and JComponents.
11. How can a thread safe JSP page be implemented?
It can be done by having them implemented by the SingleThreadModel Interface. Add <%@page isThreadSafe=”false” %> directive in the JSP page.
12. How can the output of JSP or servlet page be prevented from being cached by the browser?
Using appropriate HTTP header attributes to prevent the dynamic content output by a JSP page from being cached by the browser.
13. How to restrict page errors display in a JSP page?
By setting up an “ErrorPage” attribute of PAGE directory to the name of the error page in the JSP page, and then in the error, JSP page set “isErrorpage=” TRUE”, Errors can be stopped from getting displayed.
14. What are JSP Actions?
They are XML tags, which direct the server to use existing components or control the behavior of the JSP engine. They consist of a typical prefix of “jsp:” and action name.
15. Differentiate between <jsp:include page=…> and <%@include file=…>.
Both these tags include information from 1 page to another.
The first tag acts as a function call between two Jsp’s. It is executed each time client page is accessed by the client. It is useful to modularize the web application. New content is included in the output.
The second tag content of file is textually embedded having similar directive. The changed content is not included in the output. It is helpful when code from one jsp is required by several jsp’s.
16. Can constructor be used instead of init(), to initialize servlet?
Yes, it is possible. But it is not preferred because init() was developed because earlier Java versions could not invoke constructors with arguments dynamically. So they could not assign a servletConfig. Today, however, servlet containers still call only no-arg constructor. So there is no access to servletContext or servletConfig.
17. Explain lifecycle methods.
1) jsplnit(): The container calls this to initialize servlet instance. It is called only once for the servlet instance and preceded every other method.
2) _jspService(): The container calls this for each request and passes it on to the objects.
3) jspDestroy(): It is called by the container just before destruction of the instance.
18. Explain JSP Output comments?
They are comments that can be viewed in HTML Source File.
19. Define Expression
Expression tag is used to insert Java values directly in the output. Its syntax is <%=expression%>
It contains a scripting language expression that is evaluated, then converted to a string, and then inserted where the expression comes in JSP file.
20. Define Composition.
Composition has a stronger relationship with the object than Aggregation.
21. Define JSP Scriptlet.
It a JSP tag that encloses Java code in JSP pages. Their syntax is <% %>. Code written in scriptlet executes every time the program is run.
22. How can information from one JSP be passed to another JSP?
The tag <Jsp:param> allows us to pass information between multiple Jsp’s.
23. Explain the uses of <jsp:usebean> tag.
scope= “page | application”
This tag creates an instance of java bean. It firstly tries to find if bean instance already exist and assign stores a reference in the variable. Type is also specified; otherwise it instantiates from the specified class storing a reference in the new variable.
24. Explain handling of runtime exceptions.
Errorpage attribute is used to uncatch the run-time exceptions forwarded automatically to an error processing page.
It redirects the browser to JSP page error.jsp if an uncaught exception is faced during request handling. It is an error processing page.
25.Why does _jspService() start with an ‘_’ but other lifecycle methods do not?
Whatever content made in a JSP page goes inside the _jspService() method by the container. If it is overriding, the compiler gives an error, but the other 2 lifecycles can be easily overridden. So ‘_’ shows that we cannot override this method.
I hope you enjoy this article! If you have any doubt then please feel free to comment me below.