1. The character sequences <%= and %> enclose Java expressions, which are evaluated at run time.
Write this in jsp file: <%= new java.util.Date() %>
You’ll get current time.
Only good for simple java code.
2. You do Scriptlets by placing your larger Java code between <% and %> characters (just like expressions, but without the = sign at the start of the sequence.)
<HTML><BODY><% // This is a scriptlet. Notice that the "date" // variable we declare here is available in the // embedded expression later on. System.out.println( "Evaluating date now" ); java.util.Date date = new java.util.Date();%>Hello! The time is now <%= date %></BODY></HTML> Notice the output from the "System.out.println" will be on the server log.
3. Using “out” variable in Scriptlet, to generate HTML.
<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> The "out" variable is of type javax.servlet.jsp.JspWriter.
4. Using “request” variable of type javax.servlet.http.HttpServletRequest
<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<% out.println( date ); out.println( "<BR>Your machine's address is " ); out.println( request.getRemoteHost());%></BODY></HTML>
Note: request variable is sent by client, processed by server, and return the result of the method to client.
5. Generate a dynamic table, by mixing HTML, Scriptlet and Java-Expression
<HTML><BODY> <% int n = 120; %> <TABLE BORDER=1> <% for ( int i = 0; i < n; i++ ) { %> <TR> <TD>Number</TD> <TD><%= i+1 %></TD> </TR> <% } %> </TABLE></BODY></HTML>
Note: declare a variable n =120, the <TR> … </TR> will run 120 times. With two columns.
Just another example:
<% boolean hello = false; %>
<%
if ( hello ) {
%>
<P>Hello, world
<%
} else {
%>
<P>Goodbye, world
<%
}
%>
To get system’s properties:
<%=System.getProperties().toString() %>
6. import (a directive)
<%@ page import="java.util.*" %><HTML><BODY><% System.out.println( "Evaluating date now" ); Date date = new Date();%>Hello! The time is now <%= date %></BODY></HTML>
Note: The first line in the above example is called a “directive“. A JSP “directive” starts with <%@ characters.
This one is a “page directive“.
To import more than one item:
<%@ page import="java.util.*,java.text.*" %>
7. include directive
<HTML><BODY>Going to include hello.jsp...<BR><%@ include file="hello.jsp" %></BODY></HTML>
8. Tag (jsp tags and non-jsp tags)
jsp:include
usage
<HTML><BODY>Going to include hello.jsp...<BR><jsp:include page="hello.jsp"/></BODY></HTML>
Note: another way of doing include. Tags have XML style.
do forward:
<jsp:forward page=”zxx.html”/>
8. use session
in first page, use a “Form” and a “Text Input” to accept user’s input
<FORM METHOD=POST ACTION=”SaveName.jsp”>
What’s your name? <INPUT TYPE=TEXT NAME=username SIZE=20>
<P><INPUT TYPE=SUBMIT>
</FORM>
in second page, save the “name” to a variable.
<%
String name = request.getParameter( “username” );
session.setAttribute( “theName”, name );
%>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>JSP Page</title>
</head>
<body>
<h1>JSP Page</h1>
<A HREF=”NextPage.jsp”>Continue</A>
</body>
</html>
then in the third page, user can still retrieve the value
<%= session.getAttribute( “theName” ) %>
9. use bean to correspond HTML form.
i. design a HTML form
<HTML><BODY><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></BODY></HTML> ii. define a Java class with fields "username", "email" and "age"class: UserData package: me iii. use this class in the file "SaveName.jsp"<jsp:useBean id="user" class="me.UserData" scope="session"/><jsp:setProperty name="user" property="*"/>
These two lines of code will handle all the properties.
It will save the user’s data in the bean instance.
iv. in “NextPage.jsp“, retrieve the properties
<jsp:useBean id=”user” class=”me.UserData” scope=”session”/>
in <body>, use <%= user.getUsername() %>
Powered by ScribeFire.
how do we write to html file from jsp page
Comment by Bala — March 25, 2008 @ 4:24 am
Hello webmaster
I would like to share with you a link to your site
write me here preonrelt@mail.ru
Comment by Alexwebmaster — March 3, 2009 @ 7:02 am