Java Server Pages

JavaServer Pages (JSP) is a technology for developing Webpages that supports dynamic content. This helps developers insert java code in HTML pages by making use of special JSP tags, most of which start with <% and end with %>. A JavaServer Pages component is a type of Java servlet that is designed to fulfill the role of a user interface for a Java web application. Web developers write JSPs as text files that combine HTML or XHTML code, XML elements, and embedded JSP actions and commands.

Using JSP, you can collect input from users through Webpage forms, present records from a database or another source, and create Webpages dynamically. JSP tags can be used for a variety of purposes, such as retrieving information from a database or registering user preferences, accessing JavaBeans components, passing control between pages, and sharing information between requests, pages etc.

Advantages of JSP

  • JSP is written in Javaand that makes it more powerful and easier to use. It is portable to other operating systems and non-Microsoft Web servers.
  • It is more convenient to write (as compared to Servlet) regular HTML than to have plenty of println statements that generate the HTML.

JSP Scripting Element
JSP Scripting element are written inside <% %> tags. These code inside <% %> tags are processed by the JSP engine during translation of the JSP page. Any other text in the JSP page is considered as HTML code or plain text.
Example:
<html>
    <head>
        <title>My First JSP Page</title>
    </head>
   <%
       int count = 0;
   %>
  <body>
        Page Count is  <% out.println(++count); %>
  </body>
Just to experiment, try removing the <% %> scriplet tag from the above code and run it as JSP. You will see that everything is printed as it is on the browser, because without the scriplet tag, everything is considered plain HTML code.

There are five different types of scripting elements
Scripting Element Example
Comment                 <%-- comment --%>
Directive                 <%@ directive %>
Declaration         <%! declarations %>
Scriptlet                 <% scriplets %>
Expression         <%= expression %>

No comments:

Post a Comment