scriptlet tag of jsp

In this post, you will learn:

  • What is scriptlet tag in JSP?
  • What is the role of scriptlet tag in the auto generated servlet?
  • Which implicit objects are made available in the scriptlet tag?

The scriptlet tag of JSP:

Scriptlet is the main tag of JSP. It is used to add request processing logic to a JSP page. A JSP page may contain any number of scriptlet tags. It has following syntax:

<% request processing logic %>

At the time of translation, contents of the scriptlet tags are copied as it is to the _jspService() method of the auto generated servlet as demonstrated by the following diagram:

scriptlet

In the diagram, the package name & class name are not specified because they are server dependent. Usually the servlet name has the JSP name as part of it. Different server store the auto generated servlet in different folder e.g. the Tomcat server stores them in work->catalina->localhost folder.

Within a scriptlet, following implicit objects are made available to the JSP programmers i.e. they can be directly used in a scriptlet tag.

1. out: Is a character oriented output stream which is used by the JSP programmers to write response. It is of type JspWriter which is a sub class of PrintWriter.

2. request: – Is HttpServletRequest object which contains request parameters and attributes.

3. response – Is HttpServletResponse object.

4. config: – Is the ServletConfig object of the servlet.

5. session: – Is the HttpSession object of the user.

6. application: – Is the ServletContext object of the application.

7. page: – Is the current servlet object of the JSP.

8. pageContext: – Is an object of type PageContext. It act as a container of all the other servlet & JSP API objects which participate in the request processing. Role of this object will be discussed in detail in a separate post.

9. exception: – Is an optional object which is available only when an exception occurs in the processing. It represents the exception which occurred in the processing. With the help of this object, exception can be managed in some other page.

Lets understand the magic behind these implicit objects. As explained by the above diagram that the scriptlet contents are copied as it is to the _jspService() method of the auto generated servlet. In this method, these objects are declared before pasting the contents of the scriptlets. A JSP isn’t executed. It is just a means to get a servlet generated which is actually used to process requests. By this mechanism, A JSP programmer gets the illusion that he is using the objects without declaring them.

Practical example of the scriptlet tag:

First, the index.html :

<form method="post" action="adder.jsp">
First No: <input type="text" name="num1"><br/>
Second No: <input type="text" name="num2"><br/>
<input type="submit" value="add">
</form>

Second, the adder.jsp :

<%
int a=Integer.parseInt(request.getParameter("num1"));
int b=Integer.parseInt(request.getParameter("num2"));
int c=a+b;
out.println("sum is: "+c);
%>

With the help of this example, you may have understood the simplification which is facilitated by the JSP over servlet. No class and method need to be defined, no object is declared or obtained, no content type is set, no entry is required in web.xml, no compilation is required. Only the required processing logic is written in a JSP which is used by the translator to define the request processing logic of the generated servlet.


Its time to recap the main concepts:

  1. Scriptlet is the main tag of JSP, it is used to define request processing logic of the _jspService() method.
  2. Any number of scriptlet tags can be used in a JSP page.
  3. Nine implicit objects are made available in the scriptlet tag to the JSP programmers.

Example Source Code

If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

3 thoughts on “scriptlet tag of jsp

  1. Hi Sir,
    thanks for learning pad but one thing I want to say please upload more and more documents to read and also please provide docs for struts1,2 and JSF learning.

    regards,
    rocky

    1. We will provide tutorial on all topics but It will take some time. It is a process in which we are contributing daily.

Leave a Reply

Your email address will not be published. Required fields are marked *