imScript Manual


JSP

« Back  |  Next »
Introduction
General information
Text field
Image field
Page information
Links & Menu
Include
Data tags
User data
Date tags
Velocity
API samples
JSP
imScript tags list
RSS feed
In JSP tags can be devided into 4 different types. These are:

Directives
In the directives we can import packages, define error handling pages or the session information of the JSP page.

Declarations
This tag is used for defining the functions and variables to be used in the JSP.

Scriplets
In this tag we can insert any amount of valid java code and these codes are placed in _jspService method by the JSP engine.

Expressions
We can use this tag to output any data on the generated page. These data are automatically converted to string and printed on the output stream.
 

Syntax of JSP directives is: <%@directive attribute="value" %>

Where directive may be:
  1. page: page is used to provide the information about it.
    Example: <%@page language="java" %>
  2. include: include is used to include a file in the JSP page.
    Example: <%@ include file="/header.jsp" %>
  3. taglib: taglib is used to use the custom tags in the JSP pages (custom tags allows us to defined our own tags).
    Example: <%@ taglib uri="tlds/taglib.tld" prefix="mytag" %>

and attribute may be:
  1. language="java"
    This tells the server that the page is using the java language. Current JSP specification supports only java language.
    Example: <%@page language="java" %>
  2. extends="mypackage.myclass"
    This attribute is used when we want to extend any class. We can use comma(,) to import more than one packages.
    Example: <%@page language="java" import="java.sql.*,mypackage.myclass" %>
  3. session="true"
    When this value is true session data is available to the JSP page otherwise not. By default this value is true.
    Example: <%@page language="java" session="true" %>
  4. errorPage="error.jsp"
    'errorPage' is used to handle the un-handled exceptions in the page.
    Example: <%@page language="java" session="true" errorPage="error.jsp" %>
  5. contentType="text/html;charset=ISO-8859-1"
    Use this attribute to set the mime type and character set of the JSP.
    Example: <%@page language="java" session="true" contentType="text/html;charset=ISO-8859-1" %>
Syntax of JSP Declaratives are:

<%!
//java codes
%>

JSP Declaratives begins with <%! and ends %> with .We can embed any amount of java code in the JSP Declaratives. Variables and functions defined in the declaratives are class level and can be used anywhere in the JSP page.

Example:

<%@page contentType="text/html" %>
<html>
<body>

<%!
int cnt=0;
private int getCount(){
//increment cnt and return the value
cnt++;
return cnt;
}
%>

<p>Values of Cnt are:</p>
<p><%=getCount()%></p>
<p><%=getCount()%></p>
<p><%=getCount()%></p>
<p><%=getCount()%></p>
<p><%=getCount()%></p>
<p><%=getCount()%></p>

</body>
</html>

example prints the value of variable 'cnt'.

Syntax of JSP Scriptles are:
<%
//java codes
%>

JSP Scriptlets begins with <% and ends %> .We can embed any amount of java code in the JSP Scriptlets. JSP Engine places these code in the _jspService() method. Variables available to the JSP Scriptlets are:

  • request
    request represents the clients request and is a subclass of HttpServletRequest. Use this variable to retrieve the data submitted along the request.Example:
<%
//java codes
String userName=null;
userName=request.getParameter("userName");
%>
  • response
    response is subclass of HttpServletResponse.
  • session
    session represents the HTTP session object associated with the request.
  • out
    out is an object of output stream and is used to send any output to the client.
Other variable available to the scriptlets are pageContext, application,config and exception.



Syntax of JSP Expressions are: <%="Any thing" %>

Syntax of JSP Scriptles are with <%= and ends with %>. Between these this you can put anything and that will converted to the String and that will be displayed. Example: <%="Hello World!" %> This code will display 'Hello World!'.

Retreiving data from posted forms.

Consider an html page that prompts the user to enter his/her name, let's call it getname.htm. Here is the code of the html file:

<html>
<head>
<title>Enter your name</title>
</head>
<body>
<p>&nbsp;</p>
<form method="POST" action="showname.jsp">
<p><font color="#800000" size="5">Enter your name:</font><input type="text" name="username" size="20"></p>
<p><input type="submit" value="Submit" name="B1"></p>
</form>
</body>
</html>


The target of form is "showname.jsp", which displays the name entered by the user.
To retrieve the value entered by the user we uses the request.getParameter("username"); code.


Here is the code of "showname.jsp" file:
<%@page contentType="text/html" %>

<html>
<body>
<p><font size="6">Welcome :&nbsp; <%=request.getParameter("username")%></font></p>
</body>
</html>

Tracking the session the session between different JSP pages.

In any web application user moves from one page to another and it becomes necessary to track the user data and objects throughout the application. JSP provide an implicit object "session", which can be use to save the data specific the particular to the user.

Here is the code of the JSP file (savenameform.jsp) that takes the input from user:

<%@ page language="java" %>
<html>
<head>
<title>Name Input Form</title>
</head>
<body>
<form method="post" action="savenametosession.jsp">
<p><b>Enter Your Name: </b><input type="text" name="username"><br>
<input type="submit" value="Submit">
</form>
</body>

The above page prompts the user to enter his/her name. Once the user clicks on the submit button, savenametosession.jsp is called. The JSP savenametosession.jsp retrieves the user name from request attributes and saves into the user session using the function session.setAttribute("username",username);. Here is the code of savenametosession.jsp:

<%@ page language="java" %>
<%
String username=request.getParameter("username");
if(username==null) username="";

session.setAttribute("username",username);
%>

<html>
<head>
<title>Name Saved</title>
</head>
<body>
<p><a href="showsessionvalue.jsp">Next Page to view the session value</a><p>
</body>

The above JSP saves the user name into the session object and displays a link to next pages (showsessionvalue.jsp). When user clicks on the "Next Page to view session value" link, the JSP page showsessionvalue.jsp displays the user name to the user. Here is the code of showsessionvalue.jsp:

<%@ page language="java" %>
<%
String username=(String) session.getAttribute("username");
if(username==null) username="";
%>
<html>
<head>
<title>Show Saved Name</title>
</head>
<body>
<p>Welcome: <%=username%><p>

</body>

The function session.getAttribute("username") is used to retrieve the user name saved in the session.


Disabling the session in some pages will improve the performance of your JSP container.

Every time a JSP is requested, JSP creates an HttpSession object to maintain state for each unique client. The session data is accessible in the JSP as the implicit session object. In JSPs, sessions are enabled by default.

Session object uses the server resources. Each session object uses up a small amount of system resources as it is stored on the server side. This also increases the traffic as the session ID is sent from server to client. Client also sends the same session ID along with each request. If some of the JSP pages on your web site are getting thousands of hits from internet browser and there is not need to identify the user, so its better to disable the session in that JSP page.

You can tell the container to disable session in the JSP file by setting the session attribute to false. Set the session attribute of the page directive to false, as shown in the following example:

<%@ page session="false" %>

Here is the full code of jsp file in which session is disabled:

<%@ page language="java" session="false"%>
<html>
<head>
<title>Session Disabled</title>
</head>
<body>
<p>Session is Disabled in this page
</body>
</html>

[Help for the whole system | imScript manual | All documentation]

Suggestions or comments? Please send us a mail: info@imcode.com