31. Write a JSP page which uses tags availabe from the standard...
<%-- GTU31 --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<BR><B>C:OUT TAG</B><BR>
<c:out value="Hello World"/>
<BR>
<BR><B>C:FOREACH TAG</B><BR>
<UL>
<c:forEach var="i" begin="1" end="5" step="1">
<LI> ${i}
</c:forEach>
</UL>
<BR><B>C:FORTOKENS TAG</B><br>
<UL>
<c:forTokens var="color"
items="(red (orange) yellow)(green)((blue) violet)"
delims="()">
<LI>${color}
</c:forTokens>
</UL>
<BR><B>C:IF TAG</B><BR>
<UL>
<c:forEach var="i" begin="1" end="10">
<LI>${i}
<c:if test="${i > 7}">
(greater than 7)
</c:if>
</c:forEach>
</UL>
<BR><B>C:CHOOSE TAG</B><BR>
<UL>
<c:forEach var="i" begin="1" end="10">
<LI>${i}
<c:choose>
<c:when test="${i < 4}">
(small)
</c:when>
<c:when test="${i < 8}">
(medium)
</c:when>
<c:otherwise>
(large)
</c:otherwise>
</c:choose>
</c:forEach>
</UL>
<BR><B>C:SET,C:REMOVE TAGS</B><BR><BR>
<c:set var="map" value="<%= new java.util.HashMap() %>"
scope="request"/>
<c:set target="${map}" property="partialTitle"
value="<read-it>Core</read-it>"/>
<c:set target="${map}" property="fullTitle">
<c:out value="${map.partialTitle}"/> <BR> Servlets and
JSP Volume 2
</c:set>
${map.fullTitle}
<c:set var="authors"
value="Marty Hall, Larry Brown, Yaakov Chaikin"
scope="request"/>
<c:set var="authors">Authors</c:set>
<br>
${authors}: ${requestScope.authors}
<br><c:remove var="authors"/>
${pageScope.authors}: ${requestScope.authors}
<BR>
<BR><B>C:IMPORT TAG </B><BR><BR>
<c:import url="http://localhost:8081/CNP/first.jsp" var="dat"/>
${dat}
<BR><BR><B>C:URL,C:PARAM TAGS</B><BR><BR>
<c:url value="/BGColor.jsp" var="inputUrl">
<c:param name="bgColor" value="yellow"/>
</c:url>
${inputUrl}
<BR><BR><B>C:REDIRECT TAG</B><BR><BR>
<%--<c:redirect url="/BGColor.jsp">--%>You can check this by removing comment tag.
<%--<c:param name="bgColor" value="papayawhip"/>--%>
<%--</c:redirect>--%>
<BR><BR><B>C:CATCH TAG</B><BR><BR>
<c:catch var="myException">
<% int x = 1 / 0; %>
</c:catch>
${myException.message}
<%-- first.jsp --%>
<html>
<head>
<title>Simple JSP Program </title>
</head>
<body>
<%out.println("Hello World");
%>
</body>
</html>
<%-- BGColor.jsp --%>
<HTML>
<HEAD>
<TITLE>Color Testing</TITLE>
</HEAD>
<%
String bgColor = request.getParameter("bgColor");
if ((bgColor == null) || (bgColor.trim().equals(""))) {
bgColor = "YELLOW";
}
%>
<BODY BGCOLOR="<%= bgColor %>">
<H2 ALIGN="CENTER">Testing a Background of "<%= bgColor %>"</H2>
</BODY></HTML>
32. Write a Servlet which uses the concept of Request forwarding...
The new GetName.jsp is
<%@ taglib prefix="blx" uri="/blx.tld" %>
<jsp:useBean id="user" class="user.UserData" scope="session"/>
<HTML>
<BODY>
<blx:getProperty name="user" property="*">
<FORM METHOD=POST ACTION="SaveName.jsp">
What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20><BR>
What's your e-mail address? <INPUT TYPE=TEXT NAME=email SIZE=20><BR>
What's your age? <INPUT TYPE=TEXT NAME=age SIZE=4>
<P><INPUT TYPE=SUBMIT>
</FORM>
</blx:getProperty>
</BODY>
</HTML>
Following is a version of SaveName.jsp that processes any errors, and either shows the user GetName.jsp again to user can enter the data correctly, or automatically forwards to NextPage.jsp.
<%@ taglib prefix="blx" uri="/blx.tld" %>
<%!
boolean haveError;
StringBuffer errors;
public void errorHandler( String field,
String value,
Exception ex )
{
haveError = true;
if ( errors == null )
errors = new StringBuffer();
else
errors.append( "<P>" );
errors.append( "<P>Value for field \"" +
field + "\" is invalid." );
if ( ex instanceof java.lang.NumberFormatException )
errors.append( " The value must be a number." );
}
%>
<%
// Variables must be initialized outside declaration!
haveError = false;
errors = null;
%>
<HTML>
<BODY>
<jsp:useBean id="user" class="user.UserData" scope="session"/>
<blx:setProperty name="user"
property="*"
onError="errorHandler"/>
<%
if ( haveError ) {
out.println( errors.toString());
pageContext.include( "GetName.jsp" );
} else
pageContext.forward( "NextPage.jsp" );
%>
</BODY>
</HTML>
<%
if ( haveError ) {
request.setAttribute( "errors",
errors.toString());
pageContext.forward( "GetName.jsp" );
} else
pageContext.forward( "NextPage.jsp" );
%>
We can then do a "request.getAttribute" in the GetName.jsp,
and if the returned value is non-null, display the error
33. Develop a JSP Page to display the personal information...
Share this program...
Send it to gtumca@gmail.com
with your Name - College name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z
34. Develop a JSP Page to perform database driven operations...
Share this program...
Send it to gtumca@gmail.com
with your Name - College name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z
35. Write a JSP Page to use JSP's Page directives.
<html>
<head>
<title>Page Directives </title>
</head>
<body>
<h2>
<%@ page import="java.util.*"%>
<% Date d = new Date();
out.println("Today is : "+d);
%>
</h2>
</body>
</html>
36. Write a JSP Page to use JSP scripting.
<html>
<head>
<title>JSP Scripting </title>
</head>
<%
String bgColor = null;
if ((bgColor == null)) {
bgColor = "LIGHTGRAY";
}
%>
<BODY BGCOLOR="<%= bgColor %>">
<H1>A Random Number</H1>
<%= Math.random() %>
<H1>JSP Expressions</H1>
Current time: <%= new java.util.Date() %>
</body>
</html>
37. Write a JSP Page to which uses Session Tracking...
First we have a form, let us call it GetName.html
<HTML>
<BODY>
<FORM METHOD=POST ACTION="SaveName.jsp">
What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>
The target of the form is "SaveName.jsp", which saves the user's name in the session. Note the variable "session". This is another variable that is normally made available in JSPs, just like out and request variables. (In the @page directive, you can indicate that you do not need sessions, in which case the "session" variable will not be made available.)
<%
String name = request.getParameter( "username" );
session.setAttribute( "theName", name );
%>
<HTML>
<BODY>
<A HREF="NextPage.jsp">Continue</A>
</BODY>
</HTML>
The SaveName.jsp saves the user's name in the session, and puts a link to another page, NextPage.jsp.
NextPage.jsp shows how to retrieve the saved name.
<HTML>
<BODY>
Hello, <%= session.getAttribute( "theName" ) %>
</BODY>
</HTML>