Java Server Pages
Java Server Pages
Introduction
• What is JSP?
• My first JSP
• JSP Fundamentals
• JSP Directives and APIs
• JSP, Java Beans and Tags
What is JSP
• Because it is needed!
In fact, there are several JSP like technologies both
in the Java (as well as non-Java) world
• Servlet that generate HTML output hide the
HTML inside Java
Makes it hard for an HTML expert (!= Java expert)
to fix stuff
Lock the content in Java
Makes look and feel changes to the site
problematic
cont…
Hello World
Helloworld.jsp – Code
<HTML>
<HEAD>
<%@ page language="java"
contentType="text/html; charset=US-ASCII"
pageEncoding="US-ASCII" %>
<TITLE>helloworld.jsp</TITLE>
</HEAD>
<BODY>
<P> <% out.print("This is a sample JSP file"); %> </P>
</BODY>
</HTML>
Helloworld.jsp – Output
Helloworld.jsp – Observations
<HTML>
<HEAD>
<%@ page language="java"
contentType="text/html” import="java.util.*" %>
<TITLE>JSP snoop page</TITLE>
</HEAD>
<BODY>
…
Snoop.jsp – Code
<% Enumeration e = request.getHeaderNames();
if(e != null && e.hasMoreElements()) { %>
<H2>Request headers</H2>
<TABLE>
<TR>
<TH align=left>Header:</TH>
<TH align=left>Value:</TH>
</TR>
<% while(e.hasMoreElements()) {
String k = (String) e.nextElement(); %>
<TR>
<TD><%= k %></TD>
<TD><%= request.getHeader(k) %></TD>
</TR>
<% } %>
</TABLE>
<% } %>
Snoop.jsp – Code
<% e = request.getParameterNames();
if(e != null && e.hasMoreElements()) { %>
<H2>Request parameters</H2>
<TABLE>
<TR valign=top>
<TH align=left>Parameter:</TH>
<TH align=left>Value:</TH></TR>
<% while(e.hasMoreElements()) {
String k = (String) e.nextElement();
String val = request.getParameter(k); %>
<TR valign=top>
<TD><%= k %></TD>
<TD><%= val %></TD>
</TR>
<% } %>
</TABLE>
<% } %>
</BODY></HTML>
Snoop.jsp – Output
Directives and APIs
JSP Directives and APIs
Initializing a bean.
Setting a bean attribute
• jsp:setProperty
<BODY>
<H1>Print my name using JavaBeans</H1>
• Introduction
• Why custom tags?
• Basics of Tag Libraries
• Simple Custom Tag
Introduction
Zubair Shaikh
[email protected]
Presentation Version 1.2