JSP Java Interview Questions – Set 10

What do you understand by client side and server side templating

The modern Rich Internet Applications (RIA) use the design concept of “single page web design”, where a single rich page makes ajax based service calls to render different sections of a page instead of the traditional approach of loading a new page of each user action.The “single page web design” can make use of  client side and server side technologies as discussed below. Each approach has its use. It also show

The modern rich single page web applications built today harness the power of dynamically typed and interpreted languages like JavaScript for faster prototyping and increased developer productivity and statically typed Java based frameworks for maintainability, robustness, easier refactoring, and scalability. The modern browsers using faster JavaScript engines and efficient minification of JavaScript files have made client side templating a reality.

The server side templates are such as JSPs, Facelets for JSF, Apache Velocity, tiles, Sitemesh, etc.

The client side templating libraries based on JavaScript include dust.js, underscore.js, mustache.js, etc

The client side and server side templates like Google closure.

Server side templating and data composition 

Generates the markup on the server. For example, returns generated HTML code.

Client  side templating and data composition

Generates the markup on the client. More suited for applications that load more data from different back end service providers via ajax. The services could return data in JSON or XML format  and then add the HTML snippets for the new data via the client side templates.

.

Javascript templating is a technique to render templates on client-side (i.e. within the browser) with Javascript using a JSON data retrieved via RESTful web service calls. The template is basically HTML markup, sprayed with tags and variables that will insert values from the JSON data or execute a programming logic.

For example,

  • Import jQuery template library

<script src=”http://code.jquery.com/jquery.min.js” type=”text/javascript”></script>

<script src=”jquery.tmpl.js” type=”text/javascript”></script>

  • Define JSON data to be used to fill the template values. This can be returned from the server via a RESTFul service call.

var carsData = [

{ make: “Toyota”, model: “Camry” },

{ make: “BMW”, model: “3 Series” },

{ make: “Mazda”, model: “3” }

];

  • Define your client side template like

<script id=”carsTemplate” type=”text/html”>

<li><a href=”cars/${make}”>${model}</a></li>

</script>

or the complete HTML code may look like:

<html lang=”en”><head>

<script src=”http://code.jquery.com/jquery.min.js” type=”text/javascript”></script>

<script src=”jquery.tmpl.js” type=”text/javascript”></script>

<script id=”clientTemplate” type=”text/html”>

<li>make: ${make}, model: ${model}</li>

</script></head><body><ul></ul></body></html>

  • Invoking the template enginge (e.g jQuery) plugin to render the template with data

$(“#carsTemplate”).tmpl(carsData).appendTo( “ul” );

The  <li> tags will be appended to the <ul> tag with the retrieved JSON data values.

s the power of JavaScript.

Can you give some examples of thread racing conditions you had experienced

  • Declaring variables in JSP pages are not thread-safe. The declared variables in JSP pages end-up as instance variables in the converted Servlets. <%! Calendar c = Calendar.getInstance(); %>
  • Decalring instance variables in Servlets is not thread safe, as Servlets are inherently multi-threaded and gets accessed by multiple-threads. Same is true for the Actionclasses in the struts framework.
  • Some of the Java standard library classes likeSimpleDateFormat is not thread-safe. Always check the API to see if a particular class is thread-safe. If a particular class or library is not therad-safe, you could do one of three things.

Provide your own wrapper class that decorates the third-party library with proper synchronization.

This is a typical use of the decorator design pattern.

Use an alternative library, which is thread-safe if available.

For example, Joda Time Library.

Use it in a thread-safe manner.

For example, you could use the SimpleDateFormat class as shown below within a ThreadLocal class.

Each thread will have its own instance of the SimpleDateFormat object.

public class DateFormatTest

{

//          anonymous inner class. Each thread will have its own copy

private final static ThreadLocal<SimpleDateFormat> shortDateFormat

=  new ThreadLocal<SimpleDateFormat>()

{

protected SimpleDateFormat initialValue()

{

return new SimpleDateFormat(“dd/MM/yyyy”);

}

};

public Date convert(String strDate)throws ParseException

{

//          get the SimpleDateFormat instance for this thread and parse the date string

Date d = shortDateFormat.get().parse(strDate);

return d;

}

}

  • The one that is very popular with the interviewers is writing the singleton classes that are not thread-safe.