Thursday, February 17, 2011

JSP Declaratives



Syntax for a JSP Declarative is:
<%!
//your java code here
%>
JSP Declarative begins with the sign “<%!” and ends with this “%>” . Variables and functions defined in the declarative tag are class-level i.e they define functions and variable at the global level and they can be used anywhere in the JSP page. The code in the declaration tag is not available inside the service method (_jspService()) of JSP.
<%!
         public String add(int a, int b)
         {   
             int c=a+b;

            return c+””;
         }

%>

<%
 int firstNum=2;
 int secondNum=5;
%>
<html>
<head>
<title>Declaration in JSP</title>
</head>

<body>
This sum is : <%=add(firstNum,secondNum)%>
</body>
</html>

Add Dynamic Content to your JSP page

As in the previous post, I showed that any HTML file can easily be turned into a JSP file i.e. by changing the file's extension to .jsp. So what is it that makes JSP useful that a simple html page? It is its ability to embed java code inside of JSP page. Following example shows how.
<HTML>
<BODY>
Hello World! The time is: <%= new java.util.Date() %>
</BODY>
</HTML>
Save the above file with .jsp extension. Now load it multiple times. You will see that every time you reload the page, it shows the current time.
We have already learned about java expressions <%= and %>
So that is what which makes JSP useful i.e. generating dynamic content that will change itself according to the client's request.

Tuesday, February 15, 2011

Writing your first JSP

JSP is used to simply put Java code inside the HTML pages.  This means that you can actually take any of the existing HTML pages that you have and change their extensions to ".jsp" which are now ".html" or “.htm”. 
So open notepad in windows. Write this

<HTML>
<BODY>
Hello, world
</BODY>
</HTML>

Save it with the extension of “.jsp".  Now open this file in your browser.
The page will take a bit of time when it is loaded for the first time. After first time execution it will load normally.
So what basically happens when you try to load your page for the fist time?
JSP is turned into a Java file, which is then compiled and loaded.  As you know well compilation for java only happens once, so loading it again and again does not take much time now. Remember, every time you’ll change or edit the JSP file, the same process will repeat itself i.e. first time compilation.
Next tutorial ill show what makes JSP useful.

Tuesday, February 8, 2011

JSP Scriptlets

Syntax of JSP Scriptlet is:
<%
//your java code here
%>
A JSP Scriptlet begins with the sign <% and ends with the sign%>. Java code can be embedded in the JSP Scriptlet. JSP Engine places the code present in the scriptlet tag in the _jspService() method of the JSP page. Variables that are available to the JSP Scriptlet are:
  • request:
    request is used to represent the client's request and is a subclass of HttpServletRequest. This variable is used to retrieve the data which was submitted along the request.
    Example:
    <%
    //java code
    String name=null;
    name=request.getParameter("user_name");
    %>
  • response:
    response is subclass of HttpServletResponse. This is used to send a response.
    Example:
    <% response.sendRedirect(“nextPage.jsp”);%>
  • session:
    session represents the HTTP session object associated with the request.
    Example:
    <%session.getAttribute(“attribute_name”);%>
  • out:
    out is an object of output stream and is used to send any output to the client.
    Example:
    <%out.println(“Hello World”);%>
    This will print Hello World on the web page.

Other variables that are available to the scriptlet are pageContext, application,config and exception.