Monday, February 27, 2012

JSP

index



JSP: JavaServer Pages
Java Servlet
BEA WebLogic Application Server (JSP capable web-server)
----------------------
hello.jsp
<HTML><BODY>
Hello!  The time is now  <%= new java.util.Date() %>
</BODY></HTML>
------------------------
<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>
------------------------
<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>
-----------------------
<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>
---------------------
<TABLE BORDER=2>
<% for ( int i = 0; i < n; i++ ) { %>
<TR> <TD> Number </TD> <TD> <%= i+1 %> </TD> </TR>
<% } %>
</TABLE>
-----------------------
<% if ( hello ) { %>
<P>Hello, world
<% } else { %>
<P>Goodbye, world
<% } %>
-----------------------
<%@ page import="java.util.*" %>
<HTML><BODY>
<%
System.out.println( "Evaluating date now" );
Date date = new Date();
%>
Hello!  The time is now <%= date %>
</BODY></HTML>
-----------------------
<HTML> <BODY>
Going to include hello.jsp...<BR>
<%@ include file="hello.jsp" %>
</BODY> </HTML>
-------------------
<%@ 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>
Going to include hello.jsp...<BR>
<jsp:include page="hello.jsp"/>
</BODY></HTML>
-------------------
A session is an object associated with a visitor.  Data can be put in the session and retrieved from it, much like a Hashtable.  A different set of data is kept for each visitor to the site.
--------------
<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>
-----------------------
SaveName.jsp
<%
String name = request.getParameter( "username" );
session.setAttribute( "theName", name );
%>
<HTML><BODY>
<A HREF="NextPage.jsp">Continue</A>
</BODY></HTML>
---------------------
NextPage.jsp
<HTML><BODY>
Hello, <%= session.getAttribute( "theName" ) %>
</BODY></HTML>


---------------------
GetName.html
<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>
------------------------
package user;

public class UserData
{
String username;
public void setUsername(String value){username = value;}
public String getUsername(){return username;}

String email;
public void setEmail(String value){email=value;}
public String getEmail(){return email;}

int age;
public void setAge(int value){age=value;}
public int getAge(){return age;}

}//class UserData
--------------------
SaveName.jsp
<jsp:useBean id="user" class="user.UserData" scope="session"/>
<jsp:setProperty name="user" property="*"/>

<HTML><BODY>
<A HREF="NextPage.jsp">Continue</A>
</BODY></HTML>
------------------------
NextPage.jsp
<jsp:useBean id="user" class="user.UserData" scope="session"/>
<HTML><BODY>
You entered<BR>
Name: <%= user.getUsername() %><BR>
Email: <%= user.getEmail() %><BR>
Age: <%= user.getAge() %><BR>
</BODY></HTML>
------------------------
tld tag library description
------------------
GetName.jsp
<%@ 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>
-------------------
<jsp:forward>
Forwards a client request to an HTML file, JSP file, or servlet for processing.

JSP Syntax
<jsp:forward page="{ relativeURL | <%= expression %> }" />

Examples
<jsp:forward page="/servlet/login" />
----------------
SaveName.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." );
}//errorHandler
%>

<%
// 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" );
%>
----------------
GetName.jsp
<jsp:useBean id="user" class="user.UserData" scope="session"/>
<HTML><BODY>

<FORM METHOD=POST ACTION="SaveName.jsp">
What's your name?
<INPUT TYPE=TEXT NAME=username SIZE=20
  VALUE="<%= user.getUsername() %>"><BR>
What's your e-mail address?
<INPUT TYPE=TEXT NAME=email SIZE=20
  VALUE="<%= user.getEmail() %>"><BR>
What's your age?
<INPUT TYPE=TEXT NAME=age SIZE=4
  VALUE=<%= user.getAge() %>>
<P><INPUT TYPE=SUBMIT>
</FORM>

</BODY></HTML>
---------------------
<FORM METHOD=POST ACTION=j_security_check>

The target j_security_check is provided by the application server, and does not need to be coded.

The form must contain two <INPUT> fields, named j_username and j_password respectively for the username and password.  Typically, the username field will be a TEXT input field, and the password field will be a PASSWORD input field.
------------------
Output Comment
Generates a comment that is sent to the client in the viewable page source.

JSP Syntax
<!--    comment [ <%= expression %> ] -->

Example 1
<!-- This file displays the user login screen -->

Displays in the page source:
<!-- This file displays the user login screen -->

Example 2
<!-- This page was loaded on
<%= (new java.util.Date()).toLocaleString() %> -->

Displays in the page source:
<!-- This page was loaded on January 1, 2000 -->
---------------
Hidden Comment
Documents the JSP page but is not sent to the client.

JSP Syntax
<%-- comment --%>

Examples
<%@ page language="java" %>
<html><head><title>A Comment Test</title></head><body>
<h2>A Test of Comments</h2>
<%-- This comment will not be visible in the page source --%>
</body></html>
-----------------------
Declaration
Declares a variable or method.
JSP Syntax
<%! declaration; [ declaration; ]+ ... %>

Examples
<%! int i = 0; %>
<%! int a, b, c; %>
<%! Circle a = new Circle(2.0); %>

You must declare the variable or method before you use it in the JSP file.
---------------
Expression
JSP Syntax   <%= expression %>

Examples
The map file has <font color="blue"> <%= map.size() %> </font> entries.
Good guess, but nope. Try <b> <%= numguess.getHint() %> </b>.

Description
An expression element contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file. Because the value of an expression is converted to a String, you can use an expression within a line of text, whether or not it is tagged with HTML, in a JSP file.
You cannot use a semicolon to end an expression.
---------------------
Scriptlet: contains a code fragment.

JSP Syntax
<% code fragment %>

Examples
<%
String name = null;
if (request.getParameter("name") == null) {
%>

<%@ include file="error.html" %>

<%
} else {
foo.setName(request.getParameter("name"));
if (foo.getName().equalsIgnoreCase("integra"))
name = "acura";
if (name.equalsIgnoreCase( "acura" )) {
%>

Description
A scriptlet can contain any number of language statements, variable or method declarations, or expressions.
You can use any of the implicit objects or any object declared with a <jsp:useBean> element.
Any text, HTML tags, or JSP elements you write must be outside the scriptlet.
Scriptlets are executed at request time, when the JSP container processes the client request. If the scriptlet produces output, the output is stored in the out object.
---------------
Include Directive
Includes a static file in a JSP file.

JSP Syntax
<%@ include file="relativeURL" %>

Examples

include.jsp:
<html>
<body bgcolor="white">
<font color="blue">
The current date and time are
<%@ include file="date.jsp" %>
</font>
</body> </html>

date.jsp:
<%@ page import="java.util.*" %>
<%= (new java.util.Date() ).toLocaleString() %>

Displays in the page:
The current date and time are
Aug 30, 1999 2:38:40
---------------
Page Directive
Defines attributes that apply to an entire JSP page.

JSP Syntax
<%@ page
[ language="java" ]
[ extends="package.class" ]
[ import= "{ package.class | package.* }, ..." ]
[ session="true|false" ]
[ buffer="none|8kb|sizekb" ]
[ autoFlush="true|false" ]
[ isThreadSafe="true|false" ]
[ info="text" ]
[ errorPage="relativeURL" ]
[ contentType="mimeType [ ;charset=characterSet ]" | "text/html ; charset=ISO-8859-1" ]
[ isErrorPage="true|false" ]
%>

Examples
<%@ page import="java.util.*, java.lang.*" %>
<%@ page buffer="5kb" autoFlush="false" %>
<%@ page errorPage="error.jsp" %>
---------------
You can use the <%@ page %> directive more than once in a translation unit, but you can only use each attribute, except import, once.

language="java"
The scripting language used in scriptlets, declarations, and expressions in the JSP file and any included files. In this release, the only allowed value is java.

extends="package.class"
The fully qualified name of the superclass of the Java class file this JSP file will be compiled to. Use this attribute cautiously, as it can limit the JSP container's ability to provide a specialized superclass that improves the quality of the compiled file.

import="{package.class | package.* }, ..."
A comma-separated list of Java packages that the JSP file should import. The packages (and their classes) are available to scriptlets, expressions, and declarations within the JSP file. If you want to import more than one package, you can specify a comma-separated list after import or you can use import more than once in a JSP file.
The following packages are implicitly imported, so you don't need to specify them with the import attribute:
java.lang.*
javax.servlet.*
javax.servlet.jsp.*
javax.servlet.http.*
You must place the import attribute before the element that calls the imported class.

session="true | false"
Whether the client must join an HTTP session in order to use the JSP page. If the value is true, the session object refers to the current or new session.
If the value is false, you cannot use the session object or a <jsp:useBean> element with scope=session in the JSP file.
The default value is true.

buffer="none | 8kb | sizekb"
The buffer size in kilobytes used by the out object to handle output sent from the compiled JSP page to the client Web browser. The default value is 8kb. If you specify a buffer size, the output is buffered with at least the size you specified.

autoFlush="true | false"
Whether the buffered output should be flushed automatically when the buffer is full. If set to true (the default value), the buffer will be flushed. If set to false, an exception will be raised when the buffer overflows. You cannot set autoFlush to false when buffer is set to none.

isThreadSafe="true | false"
Whether thread safety is implemented in the JSP file. The default value is true, which means that the JSP container can send multiple, concurrent client requests to the JSP page. You must write code in the JSP page to synchronize the multiple client threads. If you use false, the JSP container sends client requests one at a time to the JSP page.

info="text"
A text string that is incorporated verbatim into the compiled JSP page. You can later retrieve the string with the Servlet.getServletInfo() method.

errorPage="relativeURL"
A pathname to a JSP file that this JSP file sends exceptions to. If the pathname begins with a /, the path is relative to the JSP application's document root directory and is resolved by the Web server. If not, the pathname is relative to the current JSP file.

isErrorPage="true | false"
Whether the JSP file displays an error page. If set to true, you can use the exception object in the JSP file. If set to false (the default value), you cannot use the exception object in the JSP file.

contentType="mimeType [ ;charset=characterSet ]" | "text/html;charset=ISO-8859-1"
The MIME type and character encoding the JSP file uses for the response it sends to the client. You can use any MIME type or character set that are valid for the JSP container. The default MIME type is text/html, and the default character set is ISO-8859-1.

Tip
If you need to include a long list of packages or classes in more than one JSP file, you can create a separate JSP file with a <%@ page %> directive that contains the import list and include that file in the main JSP file.
------------------

No comments:

Post a Comment