JSP (JavaServer Pages)
Why JSP Technology?
•Servlets are good at running logic
–Not so good at producing large amounts of output
–out.write() is ugly
•JSP pages are great at producing lots of textual output
–Not so good at lots of logic
–<% %> is ugly
How does it Work
•“JSP page”
–Mixture of text, Script and directives
–Text could be text/ html, text/ xml or text/ plain
•“JSP engine”
–‘Compiles’ page to servlet
–Executes servlet’s service() method
•Sends text back to caller
•Page is
–Compiled once
–Executed many times
Anatomy of a JSP
<%@ page language=“java” contentType=“text/html” %>
<html>
<body bgcolor=“white”>
<jsp:useBean id=“greeting” class=“com.pramati.jsp.beans.GreetingBean”>
<jsp:setProperty name=“greeting” property=“*”/>
</jsp:userBean>
The following information was saved:
User Name:
<jsp:getProperty name=“greeting” property=“userName”/>
Welcome!
</body>
</html>
JSP Elements
•Directive Elements : –Information about the page
–Remains same between requests
–E.g., scripting language used
•Action Elements : –Take action based on info required at request-time
•Standard
•Custom (Tags and Tag Libraries)
•Scripting Elements
–Add pieces of code to generate output based on conditions
Directives
•Global information used by the “JSP engine”
•Of form <%@ directive attr_ list %>
•Or <jsp: directive. directive attr_ list />
–Directive could be
•Page
•Include
•Taglib
–E. g.,
<%@ page info=“ written by DevelopMentor” %>
<jsp: directive. page import=“ java. sql.*” />
<%@ include file =“\ somefile. txt” %>
<%@ taglib uri = tags prefix=“ foo” %>
Actions Within a JSP Page
•Specifies an action to be carried out by the “JSP engine”
•Standard or custom
–Standard must be implemented by all engines
–Custom defined in tag libraries
•Standard actions ‘scoped’ by ‘jsp’ namespace
•Have name and attributes
<jsp: useBean id=“ clock” class=“ java.util.Date” />
<ul> The current date at the server is:
<li> Date: <jsp: getProperty name=“clock” property=“date” />
<li> Month: <jsp: getProperty name=“clock” property=“month” />
</ul>
Standard JSP Actions :
•jsp:useBean
•jsp:getProperty
•jsp:setProperty
•jsp:include
•jsp:forward
•jsp:param
•jsp:plugin
Scriptlets
•Of form <% /* code goes here*/ %>
–Gets copied into _ jspService method of generated servlet
•Any valid Java code can go here
CODE: OUTPUT
<% int j; %> <value> 0</ value>
<% for (j = 0; j < 3; j++) {%> <value> 1</ value>
<value> <value> 2</ value>
<% out. write(""+ j); %>
</ value><% } %>
Declarations (<%! … %>)
•Used to declare class scope variables or methods
<%! int j = 0; %>
•Gets declared at class- level scope in the generated servlet
•
public class SomeJSP extends HttpServlet implements HttpJspPage {
…
int j = 0;
void _jspService(…) {}
}
Declarations (<%! … %>)
•Used to declare class scope variables or methods
<%! int j = 0; %>
•Gets declared at class- level scope in the generated servlet
•
public class SomeJSP extends HttpServlet implements HttpJspPage {
…
int j = 0;
void _jspService(…) {}
}
JSP to Servlet Translation
<%@ page import="javax.ejb.*,javax.naming.*,java.rmi.* ,java.util.*" %>
<HTML><HEAD><TITLE>Hello.jsp</TITLE></HEAD><BODY>
<% String checking = null;
String name = null;
checking = request.getParameter("catch");
if (checking != null) {
name = request.getParameter("name");%>
<b> Hello <%=name%>
<% } %>
<FORM METHOD='POST' action="Hello.jsp">
<table width="500" cellspacing="0" cellpadding="3" border="0">
<caption>Enter your name</caption>
<tr><td><b>Name</b></td><td><INPUT size="20" maxlength="20" TYPE="text" NAME="name"></td></tr>
</table>
<INPUT TYPE='SUBMIT' NAME='Submit' VALUE='Submit'>
<INPUT TYPE='hidden' NAME='catch' VALUE='yes'>
</FORM></BODY></HTML>
Generated Servlet…
public void _jspService(HttpServletRequest request ,
HttpServletResponse response)
throws ServletException ,IOException {
out.write("<HTML><HEAD><TITLE>Hello.jsp</TITLE></HEAD><BODY>" );
String checking = null;
String name = null;
checking = request.getParameter("catch");
if (checking != null) {
name = request.getParameter("name");
out.write("\r\n\t\t<b> Hello " );
out.print(name);
out.write("\r\n\t\t" );
}
out.write("\r\n\t\t<FORM METHOD='POST' action="
+"\"Hello.jsp\">\r\n\t\t\t<table width=\"500\" cell“……………………………..
}
}
Tags & Tag Libraries
What Is a Tag Library?
•JSP technology has a set of pre- defined tags
–<jsp: useBean …/>
•These are HTML like but…
•… have limited functionality
•Can define new tags
–Look like HTML
–Can be used by page authors
–“Java code” is executed when tag is encountered
–Allow us to keep Java code off the page
•Better separation of content and logic
May Have Tags To…
•Process an SQL command
•Parse XML and output HTML
•Automatically call into an “EJB component” (EJB ™ technology- based component)
•Get called on every request to initialize script variables
•Iterate over a ResultSet and display the output in an HTML table
Primary Tag Classes (javax.servlet.jsp.tagext.Tag)
Simple Tag Example :
<%@ taglib uri=“/WEB-INF/mylib.tld” prefix=“test” %>
<html><body bgcolor=“white”>
<test:hello name=“Robert” />
</body> </html>
public class HelloTag extends TagSupport {
private String name = “World”;
public void setName(String name) { this.name = name; }
public int doEndTag() { pageContext.getOut().println(“Hello “ + name); }
}
mylib.tld
<taglib> ……
<tag><name>hello</name>
<tagclass>com.pramati.HelloTag</tagclass>
<bodycontent>empty</bodycontent>
<attribute><name>name</name></attribute>
</tag>
</taglib>
How Tag Handler methods are invoked :
<prefix:tagName
attr1=“value1” ------------ setAttr1(“value1”)
attr2=“value2” ------------ setAttr2(“value2”)
> ------------ doStartTag()
This tags's body
</ prefix:tagName>------------ doEndTag()
•Implementation of JSP page will use the tag handler for each ‘action’ on page.
Summary
•The JSP specification is a powerful system for creating structured web content
•JSP technology allows non- programmers to develop dynamic web pages
•JSP technology allows collaboration between programmers and page designers when building web applications
•JSP technology uses the Java programming language as the script language
•The generated servlet can be managed by directives
•JSP components
can be used as the view in the MVC architecture
•Authors using JSP technology are not
necessarily programmers using Java technology
•Want to keep “Java code” off a “JSP Page”
•Custom actions (tag libraries) allow the
use of elements as a replacement for Java code
No comments:
Post a Comment