• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

JSP FAQ

January 4, 2011 By j2eereference Leave a Comment

What is a scriptlet?

A scriptlet contains Java code that is executed every time a JSP is invoked. When a JSP is translated to a servlet, the scriptlet code goes into the service() method. Hence, methods and variables written in scriptlets are local to the service() method. A scriptlet is written between the  <% and %> tags and is executed by the container at request processing time.

What are JSP declarations?

JSP declarations are used to declare class variables and methods in a JSP page. They are initialized when the class is initialized. Anything defined in a declaration is available for the whole JSP page. A declaration block is enclosed between the <%! and %> tags. A declaration is not included in the service() method when a JSP is translated to a servlet.

What is a JSP expression?

A JSP expression is used to write an output without using the out.print statement. It can be said as a shorthand representation for scriptlets. An expression is written between the  <%= and %>tags. It is not required to end the expression with a semicolon, as it implicitly adds a semicolon to all the expressions within the expression tags.

Can we implement an interface in a JSP?

No.We cannot implement an interface in JSP

What is the difference in using request.getRequestDispatcher() and context.getRequestDispatcher()?

request.getRequestDispatcher(path): We need to give the relative path of the resource.

context.getRequestDispatcher(path): We need to give the absolute path of the resource.

How to implement a thread-safe JSP page?

JSPs can be made thread-safe by having them implement the SingleThreadModel interface by using <%@ page isThreadSafe=”false” %> directive in the page.

Difference between the session and cookie?

Sessions are always stored in the server side whereas cookies are always stored in the client side.

Difference between <%@include:> and <jsp:include> ?

Both are used to insert files into a JSP page.
<%@include:> is a directive which statically inserts the file at the time the JSP page is translated into a servlet.
<jsp:include> is an action which dynamically inserts the file at the time the page is requested.

Difference between forward and sendRedirect ?

Both requestDispatcher.forward() and response.sendRedirect() is used to redirect to new url. forward is an internal redirection of user request within the web container to a new URL without the knowledge of the user. The request object and the http headers remain same.  But sendRedirect is normally an external redirection of user request outside the web container. sendRedirect sends response header back to the browser with the new URL. The browser send the request to the new URL with fresh http headers.

How can we open excel documents from JSP ? 

Need to add Page directive with attribute contentType:
<%@ page contentType=”application/vnd.ms-excel” %>

How can we open word documents from JSP ? 

Add Page directive with attribute contentType:
<%@ page contentType=”application/msword” %>

How does JSP handle run-time exceptions?

 You can use the errorPage attribute of the page directive to handle run-time exceptions which automatically get  forwarded to the error page.

 For example:

<%@ page errorPage=”error.jsp” %>
If JSP page catch any run time exception redirects the browser to the JSP page which we have defined as error.jsp

How to prevent the output of the JSP from being cached by the browser?

You need to set the HTTP header attributes to prevent the output by the JSP page from being cached by the browser

<%
response.setHeader(“Cache-Control”,”no-store”); //HTTP 1.1
response.setHeader(“Pragma”,”no-cache”); //HTTP 1.0
%>

What are the implicit objects?

Implicit objects are objects that are created by the web container and contain information related to a particular request, page, or application. They are:

–request
–response
–pageContext
–session
–application
–out
–config
–page
–exception

 What are the JSP scripting elements ? 

There are three scripting language elements:

declarations.

scriptlets.

 expressions.

How to prevent creating Session in the JSP page ?

<%@ page session=”false”>
By default, a JSP page will automatically create a session.

How to include static files within a JSP page ?

You can include static pages in the JSP page using the below code.
<%@ include file=”header.jsp” %>
or
<%@ include file=”footer.html” %>

Filed Under: JSP

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.