5. JSP Interview Questions

 

|| JSP Interview Questions ||


1.  What is JSP?

A) JSP stands for Java Server Pages, it is a server side technology which is used for creating dynamic web pages. It is the extension of servlets.


 Q) What does dynamic web page means here? 

  - The web pages that are generated based on user’s response and may be different for each user are called dynamic web pages unlike the static web pages that are same for every user no matter how they interact with the application.


Q) What does Server side technology means? 

- There are basically two types of technologies: client-side and Server-side. Client-side means that the action takes place on the user’s (the client’s) computer. Server-side means that the action takes place on a web server (the place where you have stored all your JSP pages).



2. What are JSP life cycle phases?

A) A JSP page goes through the below phases:

1) Compilation: In this phase the JSP code gets converted into the equivalent servlet code.

2) Initialization: The converted JSP code gets loaded into the memory. jspInit() method gets called in this phase.

3) Request Handling : _jspService() method gets called in this phase. In this step a response is generated for the user based on the request made by them.

4) Destroy: jspDestroy() method gets called in this phase to unload the JSP from the memory. This is also known as cleanup step.



3. What all JSP lifecycle methods can you override in your JSP application?

- We can only override jspInit() and jspDestroy(), you cannot override the _jspService() method within a JSP page. By overriding jspInit() method you can initialize things like database connections, network connections etc. Whatever you initialize in jspInit() method can be freed up (released) in jspDestroy() method.



4. How many implicit objects you have in JSP, name them?

The objects that can directly be used on any JSP page without the need of being declared first are known as implicit objects. In JSP we have total 9 implicit objects, they are as follows:

1) out

2) request

3) response

4) session

5) config

6) exception

7) page

8) pageContext

9) application





5. What is the difference between include directive and include action tag?


The output got from both is same, however there are few noticeable differences among them.


1) Include directive includes the file at translation time (the phase of JSP life cycle where the JSP gets converted into the equivalent servlet) whereas the include action includes the file at runtime.


2) If the included file is changed but not the JSP which is including it then the changes will reflect only when we use include action tag. The changes will not reflect if you are using include directive as the JSP is not changed so it will not be translated (during this phase only the  file gets included when using directive) for request processing and hence the changes will not reflect.


3) Syntax difference: Include directive: <%@ include file="file_name" %> whereas include action has like this <jsp:include page="file_name" />



6. What is the purpose of scriptlets in JSP? What’s the syntax of it?

-  A scriptlet is used for including java code in a JSP page.


Syntax:

<% Java Code %>



7. What is JSP declaration tag?

-  A JSP declaration tag is used for declaring variables and methods so that you can use them later on a JSP page based on the requirement.


Syntax:

<%! Declare variables /Methods %>



8. What all directives available in JSP?

- There are three types of directives available in JSP

1) Page directive: This directive is used for setting up the attributes of a JSP page. 

Syntax:

<%@ page attribute=”value”%>


2) Include directive: Include a JSP file into another JSP file during the translation phase of JSP life cycle. 

Syntax:

<%@ include attribute=”value”%>


3) Taglib directive: This directive is basically used for custom tags.

- User-defined tags are known as custom tags.

- To create a custom tag we need three things:

1) Tag handler class: In this class we specify what our custom tag will do when it is used in a JSP page.

2) TLD file: Tag descriptor file where we will specify our tag name, tag handler class and tag attributes.

3) JSP page: A JSP page where we will be using our custom tag.



9. How to handle an exception in JSP?

- Methods of handling exceptions:


We can handle exceptions using the below two methods.


1. Exception handling using exception implicit object

2. Exception handling using try catch blocks within scriptlets


10. What is expression language in JSP?

- The main purpose of it to simplify the process of accessing data from bean properties and from implicit objects. EL includes arithmetic, relational and logical operators too.


Synatx of EL:

${expression}


whatever present inside braces gets evaluated at runtime and being sent to the output stream.




11. How do you disable a session on a particular JSP page?

-  By using the session attribute of page directive, we can disable the session on a particular JSP page. 

- This is how we can do it: <%@ page session=”false”> . By default the session attribute is set to true.



12. Is it possible to import a package in a JSP page?

-  Yes, we can import a package using import attribute of page directive.












Comments

Popular posts from this blog

2. JSP Scripting elements & Directives

1. JSP Introduction And Life-Cycle

3. JSP Action Tag and Expression Language