Last active
September 15, 2024 15:11
-
-
Save splacentino/672b3a32ca1877d68d05 to your computer and use it in GitHub Desktop.
A JSP cheat sheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Java expression evaluated at run time. | |
## Prototype | |
```jsp | |
<%= %> | |
``` | |
## Example | |
```jsp | |
Current date : <%= new java.util.Date() %> | |
``` | |
# Scriptlets | |
## Prototype | |
```jsp | |
<% %> | |
``` | |
## Example | |
```jsp | |
<% | |
System.out.println("Write in console log"); | |
out.println("write in : javax.servlet.jsp.JspWriter \"out \" variable <br>"); | |
java.util.Date date = new java.util.Date(); | |
%> | |
<% | |
if ( hello ) { | |
%> | |
<P>Hello, world at : <%= date %> | |
<% | |
} else { | |
%> | |
<P>Goodbye, world at : <%= date %> | |
<% | |
} | |
%> | |
``` | |
# Directives | |
## Prototype | |
```jsp | |
<%@ %> | |
``` | |
## Example with a "page directive" | |
**Java library includes** | |
```jsp | |
<%@ page import="java.util.Date, java.util.ArrayList" %> | |
Current date : <%= new Date() %> | |
``` | |
## Example with a "include directive" | |
**Physically include the contents of another file, HTML or JSP** | |
```jsp | |
<html> | |
<body> | |
include other.jsp...<BR> | |
<%@ include file="other.jsp" %> | |
</body> | |
</html> | |
``` | |
# Declarations | |
**You can also add variable and method declarations to this class. You can then use these variables and methods from your scriptlets and expressions.** | |
## Prototype | |
```jsp | |
<%! %> | |
``` | |
## Example | |
**The date will be the same, no matter how often you reload the page. This is because these are declarations, and will only be evaluated once when the page is loaded** | |
```jsp | |
<%! | |
Date theDate = new Date(); | |
Date getDate() | |
{ | |
System.out.println( "In getDate() method" ); | |
return theDate; | |
} | |
%> | |
Hello! The time is now <%= getDate() %> | |
``` | |
# Tags | |
## Prototype | |
```jsp | |
<some:tag> | |
body | |
</some:tag> | |
``` | |
If the tag does not require a body, the start and end can be conveniently merged together, as | |
```jsp | |
<some:tag/> | |
``` | |
## Example |
Thank you. But I have to say, my.big.fat.package
was a questionable choice.
This material is very helpful.
Thanks for this sheet nicely explain
Thanks!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks you.
It's a nice cheatsheet. Can you provide usage of filter ?