Saturday 5 September 2015

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.

How can we prevent implicit session creation in JSP?

By default JSP page creates a session but sometimes we don’t need session in JSP page. We can use JSP page directive session attribute to indicate compiler to not create session by default. It’s default value is true and session is created. To disable the session creation, we can use it like below.
1
<%@ page session ="false" %>

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.

How can we extend JSP technology?

We can extend JSP technology with custom tags to avoid scripting elements and java code in JSP pages.

Provide some JSP Best Practices?

Some of the JSP best practices are:
  1. Avoid scripting elements in JSP pages. If JSP EL, action elements and JSTL not serve your needs then create custom tags.
  2. Use comment properly, use JSP comments for code level or debugging purpose so that it’s not sent to client.
  3. Avoid any business logic in JSP page, JSP pages should be used only for response generation for client.
  4. Disable session creation in JSP page where you don’t need it for better performance.
  5. Use page, taglib directives at the start of JSP page for better readability.
  6. Proper use of jsp include directive or include action based on your requirements, include directive is good for static content whereas include action is good for dynamic content and including resource at runtime.
  7. Proper exception handling using JSP error pages to avoid sending container generated response incase JSP pages throw exception in service method.
  8. If you are having CSS and JavaScript code in JSP pages, it’s best to place them in separate files and include them in JSP page.
  9. Most of the times JSTL is enough for our needs, if you find a scenario where it’s not then check your application design and try to put the logic in a servlet that will do the processing and then set attributes to be used in JSP pages.
Please let me know if I have missed any important JSP interview question, I will be adding more to the list in future.

How do we print “
creates a new line in HTML” in JSP?

We can use c:out escapeXml attribute to escape the HTML elements so that it get’s shown as text in the browser, for this scenario we will write code like below.

What is jsp-config in deployment descriptor?

jsp-config element is used to configure different parameters for JSP pages. Some of it’s usage are:
  • Configuring tag libraries for the web application like below.
    1
    2
    3
    4
    5
    6
    <jsp-config>
            <taglib>
                <taglib-uri>http://journaldev.com/jsp/tlds/mytags</taglib-uri>
                <taglib-location>/WEB-INF/numberformatter.tld</taglib-location>
            </taglib>
    </jsp-config>
  • We can control scripting elements in JSP pages.
  • We can control JSP Expression Language (EL) evaluation in JSP pages.
  • We can define the page encoding for URL pattern.
  • To define the buffer size to be used in JSP page out object.
  • To denote that the group of resources that match the URL pattern are JSP documents, and thus must be interpreted as XML documents.