What is JSP and why do we need it?
JSP stands for Java Server Pages. JSP is java server side technology to create dynamic web pages. JSP is extension of Servlet technology to help developers create dynamic pages with HTML like syntax.
We can create user views in servlet also but the code will become very ugly and error prone. Also most of the elements in web page is static, so JSP page is more suitable for web pages. We should avoid business logic in JSP pages and try to use it only for view purpose. JSP scripting elements can be used for writing java code in JSP pages but it’s best to avoid them and use JSP action elements, JSTL tags or custom tags to achieve the same functionalities.
One more benefit of JSP is that most of the containers support hot deployment of JSP pages. Just make the required changes in the JSP page and replace the old page with the updated jsp page in deployment directory and container will load the new JSP page. We don’t need to compile our project code or restart server whereas if we make change in servlet code, we need to build the complete project again and deploy it. Although most of the containers now provide hot deployment support for applications but still it’s more work that JSP pages.
What are the life-cycle methods for a Jsp?
JSP lifecycle methods are:
- jspInit(): This method is declared in JspPage and it’s implemented by JSP container implementations. This method is called once in the JSP lifecycle to initialize it with config params configured in deployment descriptor. We can override this method using JSP declaration scripting element to initialize any resources that we want to use in JSP page.
- _jspService(): This is the JSP method that gets invoked by JSP container for each client request by passing request and response object. Notice that method name starts with underscore to distinguish it from other lifecycle methods because we can’t override this method. All the JSP code goes inside this method and it’s overridden by default. We should not try to override it using JSP declaration scripting element. This method is defined in HttpJspPage interface.
- jspDestroy(): This method is called by container when JSP is unloaded from memory such as shutting down application or container. This method is called only once in JSP lifecycle and we should override this method to release any resources created in JSP init method.
What is difference between hide comment and output comment?
The jsp comment is called hide comment whereas html comment is called output comment. If user views the source of the page, the jsp comment will not be shown whereas html comment will be shown.
What are different types of comments in JSP?
JSP pages provide two types of comments that we can use:
HTML Comments: Since JSP pages are like HTML, we can use HTML comments like <– HTML Comment –>. These comments are sent to client also and we can see it in HTML source. So we should avoid any code level comments or debugging comments using HTML comments.
JSP Comments: JSP Comments are written using scriptlets like <%– JSP Comment –%>. These comments are present in the generated servlet source code and doesn’t sent to client. For any code level or debugging information comments we should use JSP comments.
What are JSP implicit objects?
SP implicit objects are created by container while translating JSP page to Servlet source to help developers. We can use these objects directly in scriptlets that goes in service method, however we can’t use them in JSP Declaration because that code will go at class level.
We have 9 implicit objects that we can directly use in JSP page. Seven of them are declared as local variable at the start of _jspService() method whereas two of them are part of _jspService() method argument that we can use.
- out Object
- request Object
- response Object
- config Object
- application Object
- session Object
- pageContext Object
- page Object
- exception Object
Which implicit object is not available in normal JSP pages?
JSP exception implicit object is not available in normal JSP pages and it’s used in JSP error pages only to catch the exception thrown by the JSP pages and provide useful message to the client.
Why use of scripting elements in JSP is discouraged?
JSP pages are mostly used for view purposes and all the business logic should be in the servlet or model classes. We should pass parameters to JSP page through attributes and then use them to create the HTML response in JSP page.
Most part of the JSP page contains HTML code and to help web designers to easily understand JSP page and develop them, JSP technology provides action elements, JSP EL, JSP Standard Tag Library and custom tags that we should use rather than scripting elements to bridge the gap between JSP HTML part and JSP java part.
How can we handle the exceptions in JSP?
There are two ways to perform exception handling, one is by the errorPage element of page directive, and second is by the error-page element of web.xml file.
How is JSP used in the MVC model?
JSP is usually used for presentation in the MVC pattern (Model View Controller ) i.e. it plays the role of the view. The controller deals with calling the model and the business classes which in turn get the data, this data is then presented to the JSP for rendering on to the client.
What are the different scope values for the <jsp:useBean> tag?
There are 4 values:
- page
- request
- session
- application
What is the difference between ServletContext and PageContext?
ServletContext gives the information about the container whereas PageContext gives the information about the Request.
What is EL in JSP?
The Expression Language(EL) is used in JSP to simplify the accessibility of objects. It provides many objects that can be used directly like param, requestScope, sessionScope, applicationScope, request, session etc.
What is basic differences between the JSP custom tags and java beans?
- Custom tags can manipulate JSP content whereas 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 are available only in JSP 1.1 and later, but beans can be used in all JSP 1.x versions.
What is JSTL?
JSP Standard Tag Library is library of predefined tags that ease the development of JSP.
What are the types of JSTL tags?
Based on the JSTL functions, they are categorized into five types.
- Core Tags – Core tags provide support for iteration, conditional logic, catch exception, url, forward or redirect response etc.
- Formatting and Localization Tags – These tags are provided for formatting of Numbers, Dates and i18n support through locales and resource bundles.
- SQL Tags – JSTL SQL Tags provide support for interaction with relational databases such as Oracle, MySql etc.
- XML Tags – XML tags are used to work with XML documents such as parsing XML, transforming XML data and XPath expressions evaluation.
- JSTL Functions Tags – JSTL tags provide a number of functions that we can use to perform common operation, most of them are for String manipulation such as String Concatenation, Split String etc.
What is JSP Custom Tag and what are it’s components?
Sometimes JSP EL, Action Tags and JSTL tags are not enough and we might get tempted to write java code to perform some operations in JSP page. Fortunately JSP is extendable and we can create our own custom tags to perform certain operations.
We can create JSP Custom Tags with following components:
- JSP Custom Tag Handler
- Creating Tag Library Descriptor (TLD) File
- Deployment Descriptor Configuration for TLD
We can add custom tag library in JSP page using taglib directive and then use it.
Can we use JavaScript with JSP Pages?
Yes why not, I have seen some developers getting confused with this. Even though JSP is a server side technology, it’s used to generate client side response and we can add javascript or CSS code like any other HTML page.
What is difference between JspWriter and Servlet PrintWriter?
PrintWriter is the actual object responsible for writing the content in response. JspWriter uses the PrintWriter object behind the scene and provide buffer support. When the buffer is full or flushed, JspWriter uses the PrintWriter object to write the content into response.