0% found this document useful (0 votes)
2K views2 pages

JSP Cheat Sheet

This document provides a summary of key elements in JSP including commenting, directives, scripting elements, action elements, the JSTL core tag library for control flow and data retrieval, and accessing databases. It outlines common tags like <jsp:include>, <c:forEach>, <c:if>, <c:choose>, <sql:query> and how they can be used to embed Java code, iterate over results and make decisions similarly to control structures in Java. It also discusses variable scopes for sharing data across pages, requests and sessions.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views2 pages

JSP Cheat Sheet

This document provides a summary of key elements in JSP including commenting, directives, scripting elements, action elements, the JSTL core tag library for control flow and data retrieval, and accessing databases. It outlines common tags like <jsp:include>, <c:forEach>, <c:if>, <c:choose>, <sql:query> and how they can be used to embed Java code, iterate over results and make decisions similarly to control structures in Java. It also discusses variable scopes for sharing data across pages, requests and sessions.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

JSP Cheat Sheet Commenting <%--calculate the sum of x and z here --%> Directives Specify set up in about JSP

P page <%@ page .. %> e.g. <%@page contentType ="text/html" %> <%@ include %> e.g. <%@ include file="relativeURL" %> <%@ taglib .. %> e.g. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> Scripting Elements Enable Java to be imbedded in JSP page Expressions <%=.%> e.g. <%= a + b + c %> Scriptlets <%....%> e.g. <% for(int i=1;i<=10;i++) {%> <%=i%> <br/> <%}%> Declarations <%!....%> e.g. <%! int a, b, c; %> Action Elements These allow developers to code in tags rather than scriptlet programming <jsp:forward> e.g. <jsp:forward page="errorpage.jsp"/> <jsp:include> e.g. <jsp:include page="hello.jsp"/> <jsp:param> e.g. <jsp:param name="username" value="jsmith" /> JSTL C:OUT, C:FOREACH <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <c:forEach var="i" begin="1" end="10" step="1"> // SAME AS FOR LOOP <c:out value="${i}" /><br /> // Same as cout << </c:forEach> C:IF <c:if test="${param.p == "someValue"}"> // SIMILAR IF OR DO WHILE Generate this template text if p equals someValue </c:if> C:CHOOSE <c:choose> // LIKE IF ELSE OR SWITCH <c:when test="${param.p == "0"}"> <c:out value = "zero recorded"/> </c:when> <c:when test="${param.p == "1"}"> <c:out value = "single value"/> </c:when> <c:otherwise> <c:out value = " ${param.p}"/>

</c:otherwise> </c:choose> Empty Operator <c:if test="${empty param.name}"> Please specify your name. </c:if> Error Processing statement <input type = "hidden" name = "alreadySent" value = "true> Scopes Page only available within the JSP page. Page scoped data is destroyed when the page has finished generating its output. Request available in all pages processing the same client request. Once the request from that has completed, the request-scope data is destroyed. Session available to all requests made from the same user/client. Application available to all users of that application, while the application is running. e.g. <c:set var="sessionCounter" scope="session" value="${sessionCounter + 1}" />

Database <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %> Set Database Source <sql:setDataSource var="shopDb" scope = "session" driver = "sun.jdbc.odbc.JdbcOdbcDriver" url ="jdbc:odbc:dsnname"/> Query Database <sql:query var = "productResult"> dataSource = "${productsdb}" SELECT * FROM Products WHERE ProductName = ? <sql:param value = "${param.productNAME}"/> </sql:query> Check for results <c:when test ="${productResult.rowcount == }"> No results found </c:when> Output <c:foreach item ="${productresult.rows}" var="row"> <tr> <td> <c:out value="${row.productId}}" /></td> <td> <c:out value="${row.description}}" /></td> </tr>

You might also like