HOME

JSP reference


5 kinds of things:

Scriplet, Expressions (returning a string), declarations, actions, directives.

Scriptlets:

<% code here %>
or
<jsp:scriptlet> code here </jsp:scriptlet>

Expressions:

<%= var_name %>
or
<% out.print(var_name) %>
or
<jsp:expression>var_name</jsp:expression>

Declarations:

<%! var_name=constant (no output) %>
or
<jsp:declaration>delcarative code</jsp:declaration>

Actions:

<jsp:useBean id="var_name" class="sub_classpath"
scope="page|request|session|application" type="class" beanName="var_name">
<jsp:setProperty|getProperty>
</jsp:useBean>

<jsp:include>   (inserts text "as is" AFTER the parsing)
or
<%@ include file="filename" %>   (inserts code into the page BEFORE parsing)
or
<jsp:include page="somepage.htm" flush="true" />

<jsp:forward page="somepage.htm"/>

<jsp:plugin type="applet" code="x.class" codebase="/java" vspace="0" hspace="0" width="60" height="80" jrevision="1.2" iepluginurl="" nspluginurl="">
<jsp:param name="z" value="dummy"/>
<jsp:fallback>Unable to initialize java...</jsp:fallback>
</jsp:plugin>

Directives:

<%@ directive_name %>
or
<jsp:directive.directive_name/>
<%@page attribute="value" import="name" language="java"
autoflush="true" buffer="none" buffered="false"
contentType="text/plain" errorPage="error.jsp" isErrorPage="true"
info="whatever" isThreadSafe="true" session="true" %>
(autoflush sends the page incomplete once the buffer is full)

<jsp:directive.include file="name"/>
(affects the rest of the page, but cannot do dynamic includes)
(Action allows dynamic includes (<%=foo%>) but cannot affect the rest of the page)

<%@ taglib uri="http://www...jar" prefix="my" %>
<my:subtag>my parameters</my:subtag>

comments:

<%-- comment --%>
<!-- comment -->
<% code... //comment %>

Other:

All code is in Java (obviously).
To precompile a script enter the URL: my_file.jsp?jsp_precompile="true"
All <%! ... %> tags are persistent across threads
isThreadSafe="false" turns off multithreading.
synchronized(object){statements} assures that the statements are single threaded.
<%@page errorPage="errors.htm" %>
out.print("hello"); is the default form for JSP
void jspInit() and void jspDestroy() are the built-in start and stop procedures.
The proper form of JSP is XML compliant.
Get details on custom tags if interested...

Request:

getParameter(parameter);
getServerName();
getRequestURI();
getQueryString();
getReader();
getInputStream();
getMethod();
String getAttribute(String);
Enumeration getAttributeNames();
getParameterNames();
getParameterValues();
cookie[] getCookie();
getHeader("cookie");

new Cookie (String name, String value);
void cookie.setMaxAge(int expire);
void cookie.setSecure(boolean flag); (only with SSL)
void cookie.setDomain(String pattern);
void cookie.setPath(String URI);

Response:

Void addCookie(Cookie cookie);
void setContentType(String type);
addHeader(String name, String value)
Location=URL
Expires=dateValue
setHeader(...);
addIntHeader(String name, int value);
setIntHeader(...);
addDateHeader(String name, long date);
setDateHeader(...);�� (add adds a new header line, set replaces the header line)
sendError(int code, String Msg);(HTTP response codes)
void sendRedirect("http://...");

Session:

putValue(String key, object value);
getValue(String key);
removeValue(String key);
String[] getValueNames();
getMaxInteractiveInterval();
setMaxInteractiveInterval(int Seconds);
long getCreationTime();
long getlastAccessedTime(); (seconds since Jan 1 1970)
invalidate(); (destroys the session)
boolean isnew(); (was the session used before - first page seen)
getID(); (security risk - don�t use)

Application:

getServerInfo();
getRealPath(request.getRequestURI());
log();

JavaBean design:

  1. Constructor requires no parameters.
  2. All access to the bean is through accessor methods
  3. Public {object type} get {property}
  4. Public void set {property}({object})
  5. Public Boolean is{Property}();
  6. Distributed in jar file with "Java-Bean! True" in manifest
  7. Implementing the "serializeable" interface allows the bean to be stored as text with live data.
Beans must be separately compiled before use.

Enterprise Java Beans are more restrictive: There are two kinds - session beans and entity beans. Session beans are transient with only onw client each. Entity beans represent persistent storage (possibly from a database).

JDBC:

Class.forName("sun.jdbc.odbc.jdbcOdbcDriver");
DriverManager.RegisterDriver(new Oracle.jdbc.driver.OracleDriver());
URL="jdbc:oracle:thin:@"+host+":1521:ORCL":
connection conn=DriverManager.getConnection(URL, login, password);
statement st=conn.CreateStatement();
String SQL="Select * from sometable";
resultSet rset st.executeQuery(SQL);
(the result set starts before the first row).
int returnValue=st.executeUpdate(otherSQL);
st.close;
conn.close;