save_my_name,jsp
<%
String name = request.getParameter( "username" );
session.setAttribute( "theName", name );
%>
<HTML>
<BODY>
<A HREF="next_page.jsp">Continue</A>
</BODY>
</HTML>
String name = request.getParameter( "username" );
session.setAttribute( "theName", name );
%>
<HTML>
<BODY>
<A HREF="next_page.jsp">Continue</A>
</BODY>
</HTML>
save_my_name.jsp saves the user's name in the session, and puts a link to another page, next_page.jsp.
next_page.jsp shows how to retrieve the saved name.
next_page.jsp
// printf( "a.out");
/*
printf( "b.out");
*/
/*
printf( "b.out");
*/
Above code have no bug, but no code will be compiled, because it's alldeclarations.
In JSP, you can make declarations as well. In some documents, a declarations is also called asremark.
To add a declaration, you must use the <%! and %> sequences to enclose your declarations, as shown below.
<%@ page import="java.util.*" %>
<HTML>
<BODY>
<%!
Date theDate = new Date();
Date getDate()
{
System.out.println( "In getDate() method" );
return theDate;
}
%>
Hello! The time is now <%= getDate() %>
</BODY>
</HTML>
<HTML>
<BODY>
<%!
Date theDate = new Date();
Date getDate()
{
System.out.println( "In getDate() method" );
return theDate;
}
%>
Hello! The time is now <%= getDate() %>
</BODY>
</HTML>
Above code is actuallydead code, because it;s surrounded with <%! and %>.
Here's the simple example.
InJSP, you can print out text strings by out.println().
Here's the example.
<HTML> <BODY> <%
// This scriptlet declares and initializes "date"
System.out.println( "Evaluating date now" );
java.util.Date date = new java.util.Date(); %> Hello! The time is now <%
// This scriptlet generates HTML output
out.println( String.valueOf( date )); %> </BODY> </HTML>
But above example can be expressed as below.
<HTML>
<BODY>
<%
// This scriptlet declares and initializes "date"
System.out.println( "Evaluating date now" );
java.util.Date date = new java.util.Date();
%>
Hello! The time is now <%= date%>
</BODY>
</HTML>
<BODY>
<%
// This scriptlet declares and initializes "date"
System.out.println( "Evaluating date now" );
java.util.Date date = new java.util.Date();
%>
Hello! The time is now <%= date%>
</BODY>
</HTML>
Both ways shows the same result, but the second is much faster and have good readability than the first. code,


Prev

Rss Feed