Sie sind auf Seite 1von 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>

Das könnte Ihnen auch gefallen