Java Server Pages: Summer Internship - 2012 (Indian Institute of Technology Bombay)
Java Server Pages: Summer Internship - 2012 (Indian Institute of Technology Bombay)
Java Server Pages: Summer Internship - 2012 (Indian Institute of Technology Bombay)
Rajavel D
(Clicker Software)
IITB
- JSP
JSP Environment
IITB
- JSP
JSP Tags
<%= expression %>
IITB
- JSP
Example JSP
<HTML>
<BODY>
Hello! The time is now <%= new java.util.Date() %>
</BODY>
</HTML>
Notes:
The <%= ... %> tag is used, because we are computing a
value and inserting it into the HTML
The fully qualified name (java.util.Date) is used, instead of
the short name (Date), because we havent yet talked
about how to do import declarations
IITB
- JSP
Scriptlets
Scriptlets are enclosed in <% ... %> tags
Scriptlets are Java code that may write into the HTML
Scriptlets are inserted into the servlet exactly as written
Example:
<% if (Math.random() < 0.5) { %>
Have a <B>nice</B> day!
<% } else { %>
Have a <B>good</B> day!
<% } %>
IITB
- JSP
Declarations
Use <%! ... %> tag for declarations
If declared with <% ... %>, variables are local
Example:
<%! int accessCount = 0; %>
:
<%= ++accessCount %>
You can use <%! ... %> to declare methods as easily as to
declare variables
IITB
- JSP
JSP Comments
Different from HTML comments.
HTML comments are visible to client.
IITB
- JSP
Directives
Directives affect the servlet class itself
A directive has the form:
<%@ directive attribute="value" %>
The most useful directive is page, which lets you import
packages
Example: <%@ page import="java.util.*" %>
IITB
- JSP
IITB
- JSP
Actions tags
Actions are XML-syntax tags used to control the servlet engine
<jsp:include page="URL " />
Inserts the relative URL at execution time (not at compile
time, like the include directive does)
This is great for rapidly changing data
<jsp:forward page="URL" />
<jsp:forward page="www.google.co.in" />
Forward the page to specified URL
IITB
- JSP
Actions
<jsp: forward page="ssParameters.jsp">
<jsp: param name="myParam" value="Amar Patel"/>
<jsp: param name="Age" value="15"/>
</jsp: forward>
Name: <%= request.getParameter("myParam") %>
IITB
- JSP
IITB
- JSP
Response :
response.sendRedirect("http://www.google.co.in);
response.setHeader("Cache-Control","no-cache");
response.setContentType("text/html");
IITB
- JSP
Session in jsp
In session management whenever a request comes for any
resource, a unique token is generated by the server and
transmitted to the client by the response object and stored
on the client machine as a cookie.
Session management
(i) Session Object
(ii) Cookies
(iii) Hidden Form Fields
(iv) URL Rewriting
IITB
- JSP
Session in jsp
Set Session Attribute
String svalue = request.getParameter("sesvalue_txt");
if(svalue!=null)
{
session.setAttribute("sesval",request.getParameter("sesvalue
_txt"));
}
IITB
- JSP
Session in jsp
Using Session Attribute
<% if(session.getAttribute("sesval")==null){ %>
<%session.removeAttribute("sesval");%>
IITB
- JSP
IITB
- JSP
References
http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPIntro.html
www.roseindia.net/jsp/jsp.htm
www.jsptutorial.net/
www.jsptut.com/
www.tutorialspoint.com/jsp/index.htm
IITB
- JSP