JSP Request Handling

A JSP works using http request – response model. Jsp Request handling is necessary to understand while using Jsp in any web application. Client browser sends a http request to the web container which is handled by the servlet from JSP. The request is executed by jspService() method and generated response is sent back. This transaction involves many data objects, which are used to encapsulate specific data. These objects may contain business data or environment data. It is important to define the extent of data sharing allowed by these objects. Defining scope of these object puts restriction on sharing of these objects in this request-response cycle. In addition to this, the request itself can be forwarded or redirected before generating final response. In this article, we will look as these request processing aspects. Following four main points are discussed in detail.

Aspects of Jsp Request Handling:

  • Scope
  • Include
  • Forward
  • Redirect


Scope attribute defines the context in which any object will be available. Based on this availability information, we can use different objects. In a Jsp, there are following four scopes identified.

  • page: page maps to the jspService() method of generated servlet. Hence an object with page scope is available in service method only.
  • request: Request originates from a client browser and is executed in the jspService() method of generated servlet, to result in a response. Objects with this scope are available as long as the request object is available. These objects can be retrieved from the request object using getAttribute method.
  • session: This scope is larger than the request scope. Session scope lasts as long as the user session exists. Similar to request scope, these objects can also be retrieved using getAttribute method.
  • application: In this scope, the object is bound to ServletContext object. This scope is higher than the session scope and the object will get shared amongst different sessions of same user or sessions of different users. Here also ‘getAttribute’ method can be used to retrieve the object.

The scope goes on increasing in sequence from page, request, session to application.


Jsp components are included to increase reuse of code. We can include these components using either static include or using dynamic include. Static include is implemented using <%@include file=”filename”%> syntax. This include inserts the contents of included file in the jsp. Thus a new jsp is created after all inclusions. Requests are handled by the servlet generated from this new jsp. There is no other change in request-response model. Here the file can be html, jsp, image, etc.

Dynamic include is different from static include in many aspects. It is implemented using <jsp:include page=”filename”/> action element. While translating the jsp, container converts this include into a method call. During runtime, the method will be executed to render the page. This included page will have access to all implicit objects of the parent jsp. Only html and jsp pages can be included using this method.


<jsp:forward page=”url”/> is used to forward request to another page. Target page will execute the request and send response to client. Attributes to be forwarded to target page can also be included in this tag.


In this scenario, the request will be redirected to a new resource. HttpServletResponse object’s sendRedirect() method is used to implement this. In redirect, the response of first request is returned to client browser and browser issues a new request all together.

<<Previous   Home   Next>>

1 Comment

Leave a Reply

Your email address will not be published.