+91 90691 39140 | +1 253 214 3115 | info@hub4tech.com | hub4tech

JSP Interview Questions and Answers


1
What is JSP?

JavaServerPages is server side component in a web application, used to dynamically generate HTML / XML documents. JSP allows the java code dynamically embedded within a web page like HTML and gets executed when the page is served by the server. After the page is served, the dynamic content is sent to the client. With the script, the page is run on the server before it reaches to the client. JSP are used to invoke the built-in functionalities. In addition to the HTML , the JSP technology adds a tag library which provides a platform independent extending capabilities of a web server.

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
A JSP is transformed into a(n):

Java servlet.

4
Explain the categories of JSP tags - Directives, Scripting elements, Actions

Directives
The directive tags are used for simple java programming calls like importing packages, specifying the error handling pages or to handle session in a JSP page.

The directive tags are:
page: To provide information about the type of page.
include: To include another JSP.
taglib: To specify / utilizes the custom tags [ user defined tags ] in the JSP .

Scripting Elements:
A scriplet tag is used to place a valid java code. This code is placed in the jspService() method by the JSP engine.

Actions:
Actions are the core part of a JSP. An action can be printing an expression from the scriplet or even creating and persisting a bean. Scriplets, declarations, expressions, forwarding a page, handling bean properties, creating a bean for the page comprises all the actions that are part of the JSP.

5
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:

  1. response
  2. exception
  3. application
  4. request
  5. session
  6. page
  7. out
  8. config
  9. pageContext
6
What programming language(s) or scripting language(s) does Java Server Pages (JSP) support?

Java only

7
Explain how a JSP is compiled into servlets by the container.

JSPs are compiled into JavaServlets by the JSP container. A JSP compiler may generate a servlet in java code that is compiled by the java compiler or the bytecode may be generated directly for the servlet. JSP directives control the compilation process of a JSP that generates a servlet. Tomcat web server provides the JSP compilation as a task of ANT tool. The JSP compiled servlets are stored by ANT in work folder of CATALINA_HOME

8
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 filled by the user is submitted to the server using 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.
9
What servlet processor was developed by Apache Foundation and Sun?

Apache Tomcat

10
Is JSP technology extensible?

Yes, JSP is easily extensible by use and modification of tags, or custom actions, encapsulated in tag libraries.

11
What is invoked via HTTP on the Web server computer when it responds to requests from a user's Web browser?

A Java servlet

12
What is Tag extension in JSP model?

JSP supports the authors to add their own custom tags that perform custom actions. This is performed by using JSP tag extension API. A java class is written by developers which implements the Tag interface and provides a tag library XML description file. This file specifies the tags and java classes that are used to implement the tags.

13
How many copies of a JSP page can be in memory at a time?

One

14
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.

15
How does Tomcat execute a JSP?

By one of Tomcat's threads

16
Explain the Java packages which contains JDBC classes and interfaces, Java.SQL, Javax.SQL

java.sql:
java.sql is an API to access and process the data stored in a database, typically a relational database using the java. Different drivers can be installed dynamically for the access of various databases, using a framework which in-built in this JDBC API.

javax.sql:
javax.sql is a JDBC API for the server side for accessing and processing the data from the databases typically a relational database using java. It is the essential part for J2EE. This API provides the facilities such as connection pooling, distributed transactions and row sets for the enterprise applications. An interface by name DataSource is provided in this API as an alternative to DriverManager to establish the connection.

DataSource and RowSet usage is direct, for the applications, where as connection pooling, distributed transactions are implemented by an infrastructure called middle-tier.

17
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.

18
Describe how to open a database connection

Opening a database connection:
The database connection should be established after registering the drivers. The getConnection is used to open a database connection. The following code snippet illustrates this:

Connection con;
con = DriverManager.getConnection(url,"scott","tiger");// returns the connection object

where url specifies the database server port, ip address,domain name etc. For ex:
jdbc:oracle:thin:@192.137.63.230:1521:MyCompany

the ip address is an imaginary one and not intended to specify any specific system. The “scott” and “tiger” are the user name and passwords respectively.

19
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.

20
Describe with an example how to send SQL statements to databases for execution.

The following illustrates with examples, to send SQL statements to databases for execution:

  • The SQL statements are to be created as objects. These objects are to be assigned to Statement reference.
    Ex: Statement stmts = con.createStatement();//creates a statement object
  • The specific SQL statements are to be executed and the result should be captured
    Ex: ResultSet rsltSet = stmts.executeQuery(“select * from emp”);
  • To make a row inserted into the database and committed:
    stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

    ResultSet rs=stmt.executeQuery("select * from emp");
    rs.moveToInsertRow();
    rs.updateInt(1,15); // first attribute with value 15
    rs.updateString(2,"Vismay”); // second value with Vismay
    rs.updateFloat(3,4500); // third attribute with 4500
    rs.insertRow();
    con.commit();

21
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.

22
What is a “dirty read”?

In typical database transactions, one transaction reads changes the value while the other reads the value before committing or rolling back by the first transaction. This reading process is called as ‘dirty read’. Because there is always a chance that the first transaction might rollback the change which causes the second transaction reads an invalid value.

23
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.

Copyright ©2015 Hub4Tech.com, All Rights Reserved. Hub4Tech™ is registered trademark of Hub4tech Portal Services Pvt. Ltd.
All trademarks and logos appearing on this website are the property of their respective owners.
FOLLOW US