Kaveri College of Arts, Science and Commerce: Tybsc (CS) Java JSP
Kaveri College of Arts, Science and Commerce: Tybsc (CS) Java JSP
Kaveri College of Arts, Science and Commerce: Tybsc (CS) Java JSP
Response
Send
Response
Scripting Elements
Types
There are three kinds of scripting elements
Declarations
Scriptlets
Expressions
Declarations
Basics
Declarations are used to define methods & instance variables
Do not produce any output that is sent to client
Embedded in <%! and %> delimiters
Example:
<%!
Public void jspDestroy() {
System.out.println(“JSP Destroyed”);
}
Public void jspInit() {
System.out.println(“JSP Loaded”);
}
int myVar = 123;
%>
The functions and variables defined are available to the JSP Page as well as
to the servlet in which it is compiled
Scriptlets
Basics
Used to embed java code in JSP pages.
Contents of JSP go into _JSPpageservice() method
Code should comply with syntactical and semantic constuct of java
Embedded in <% and %> delimiters
Example:
<%
int x = 5;
int y = 7;
int z = x + y;
%>
Expressions
Basics
Used to write dynamic content back to the browser.
If the output of expression is Java primitive the value is
printed back to the browser
If the output is an object then the result of calling
toString on the object is output to the browser
Embedded in <%= and %> delimiters
Example:
<%=“Fred”+ “ “ + “Flintstone %>
prints “Fred Flintstone” to the browser
<%=Math.sqrt(100)%>
prints 10 to the browser
Java Implicit Objects
Scope
Implicit objects provide access to server side
objects
e.g. request, response, session etc.
There are four scopes of the objects
Page: Objects can only be accessed in the page where
they are referenced
Request: Objects can be accessed within all pages that
serve the current request.
(Including the pages that are forwarded to and included
in the original jsp page)
Session: Objects can be accessed within the JSP pages
for which the objects are defined
Application: Objects can be accessed by all JSP pages in
a given context
Java Implicit Objects
List
request: Reference to the current request
response: Response to the request
session: session associated woth current request
application: Servlet context to which a page belongs
pageContext: Object to access request, response, session and
application associated with a page
config: Servlet configuration for the page
out: Object that writes to the response output stream
page: instance of the page implementation class (this)
exception: Available with JSP pages which are error pages
Java Implicit Objects
Example
<html> <p>
<head> Storing a string to the application...<br>
<title>Implicit Objects</title> <% application.setAttribute("name", "Meeraj"); %>
</head> Retrieving the string from application...<br>
<body style="font-family:verdana;font-size:10pt"> <b>Name:</b>
<p> <%= application.getAttribute("name") %>
Using Request parameters...<br> </p>
<b>Name:</b> <%= request.getParameter("name") %> <p>
</p> Storing a string to the page context...<br>
<p> <% pageContext.setAttribute("name", "Meeraj"); %>
<% out.println("This is printed using the out implicit Retrieving the string from page context...</br>
variable"); %> <b>Name:</b>
</p> <%= pageContext.getAttribute("name") %>
<p> </p>
Storing a string to the session...<br> </body>
<% session.setAttribute("name", "Meeraj"); %> </html>
Retrieving the string from session...<br>
<b>Name:</b> <%= session.getAttribute("name") %>
</p>
Example Implicit Objects
Deploy & Run
Save file:
$TOMCAT_HOME/webapps/jsp/Implicit.jsp
Access file
http://localhost:8080/jsp/Implicit.jsp?name=Sanjay
Results of the execution