Java Server Pages: JSP Syntax
Java Server Pages: JSP Syntax
Java Server Pages: JSP Syntax
JSP syntax
Declaration Tag :-declare variables.
<%! Dec var %>
<%! int var=10; %>
Java Scriplets :- add any number of JAVA
code, variables and expressions.
<% java code %>
JSP Expression :- evaluates and convert the
expression to a string.
<%= expression %>
<% num1 = num1+num2 %>
JSP Comments :- contains the text that is
added for information which has to be ignored
<% -- JSP Comments %>
JSP components
Directives
Declarations
Scriptlets
Comments
Expressions.
getQueryString() method
used to fetch the 'query string'
The query string contains the attribute & the
value of the html or JSP form, which sends with
the POST /GET method to Servlet or JSP page
for any request.
<% if(request.getQueryString() != null)
out.println(request.getQueryString()); %>
JSP Beans
reusable software component
A bean encapsulates many objects into one
object so that we can access this object from
multiple places
easy maintenance
The JavaBean properties and methods can be
exposed to another application
Java class
no-arg constructor
does not have public variables
defined in a named package, cannot be kept in
the default no-name package
it implements Serializable interface, so that its
state can be stored and retrieved to and from
external storage, for persistent
methods to set and get the values of the
properties, as getter and setter methods
JavaBeans are mutable. So, it can't take
advantages of immutable objects
package mypack;
public class Employee implements
java.io.Serializable
{
private int id;
private String name;
public Employee()
{}
public void setId(int id)
{
this.id=id;
}
public int getId()
{
return id;
}
public void setName
(String name)
{
this.name=name;
}
public String getName()
{
return name;
} }
To access the JavaBean class, we should use
getter and setter methods.
package mypack;
public class Test
{
public static void main(String args[])
{
Employee e=new Employee();
e.setName("Arjun");
System.out.println(e.getName());
}}
JSP Session Tracking
In a web application, server may be
responding to several clients at a time.
Hence, so session tracking is a way by
which a server can identify the client
uniquely.
HTTP protocol is stateless which means
client needs to open a separate connection
every time it interacts with server and server
treats each request as a new request.
In order to identify the client, server needs to
maintain the state and to do so, there are
several session tracking techniques.
Cookies
small piece of information which is stored
in user’s computer.
The web server uses a cookie to identify
the user in the next time visit.
A cookie has attributes such as domain,
path, and timeout.
JSP provides API to allows you to work
with cookie effectively through the object of
the class javax.servlet.http.Cookie.
If cookie is associated with the client
request, server will associate it with
corresponding user session otherwise it will
create a new unique cookie and send it back
with response.
Cookie objects are can be created using a
name value pair.
For example, we can create a cookie with
the name sessionId and with a unique value for
each client.
Later this can be added it in a response so
that it will be sent back to the client.
Syntax of creating Cookie in a JSP is:
Cookie cookie = new Cookie(“sessionID”, “some
unique value”);
response.addCookie(cookie);
Creating cookies
1) Creating a Cookie object-
1. <%@page import="javax.servlet.http.Cookie"%>
2. <html>
3. <head>
4. <title>JSP Create Cookie</title>
5. </head>
6. <body>
7. <%
8. Cookie cookie = new Cookie("ClientID","username");
9. cookie.setMaxAge(3600);
10. response.addCookie(cookie);
11. %>
12. </body>
13. </html>
Reading Cookies
To read a cookie from an HTTP request,
first call the method getCookies() of
the request object.
This method returns a list of available
cookies in the request header, then walk
through all of them.
1. <%@page import="javax.servlet.http.Cookie"%>
2. <html>
3. <head>
4. <title>JSP Read Cookie</title>
5. </head>
6. <body>
7. <%
8. Cookie[] list = request.getCookies();
9. if (list != null){
10. for (int i = 0; i < list.length;i++){
11. out.println(list[i].getName() + ":" +
list[i].getPath());
12. }
13. }
14. %>
15. </body>
16. </html>