Note: This approach is very old; Java/JSP scriptlets were deprecated a long time ago. I don't have time to update this article to the correct, modern approach, but I hope this JSP session example will point you in the right direction.
Every once in a while I'm asked something like, "How can I tell if I have a valid user session in my JSP code?"
I've posted a free calendar based on Java and JSP technology online here. As you'll see, it's a very simple calendar, displaying the current month initially, then letting you scroll through the months with previous and next links.
JSP URI/URL FAQ: How do I get a URI or URL from a JSP (the request URI or request URL)?
I was just working with a JSP, and trying to remember how to get information that can be very helpful inside of a JSP, specifically how to determine the Context, URI, and URL from within a JSP.
To that end, here's the Java source code for a JSP I wrote that will display the Context, URI, and URL for this JSP when it is accessed from a browser:
It's been a long time since I worked on an old-school JSP (without the benefit of a framework like JSF or Struts), but I've wanted to add alternating row stripes ("zebra stripes") to some HTML tables in some old JSP's for a long time. I like tables with zebra stripes because they're easier to read, so now that I have some free time, I finally got this done.
Now that I have all of my JSP/CSS code working properly, I thought I'd share it all here in the form of a "recipe".
A lot of times when you're working on a Java web application you only need to deploy your JSP files. This happens, for instance, when you're just editing the JSP files to modify the look and feel of your web application. In cases like this there's no need to rebuild your entire application, deploy it, then restart your application server (Tomcat, Glassfish, JBoss, whatever).
When you need to forward from one JavaServer Page (JSP) to another JSP, here's all you have to do:
<jsp:forward page="/faces/success.jsp" />
In the example shown I happen to be working on a small JavaServer Faces (JSF) application, so the URL in this example includes the string
You'll probably have to know a little bit about Java, Ant, and build files for this sample
build.xml file to be any use to you, but if you're looking for a sample Ant build script that can be used to create a
war file, or one that simple uses a
war task, this example might work for you.
I'm not going to provide any explanation of any of this right now, but if I have time in the future I'll come back here and add a few comments about how this build script works.
I created a JSP this morning that prints out the equivalent of most traditional CGI parameters. Sometimes I use these to debug a problem, other times I use them within JSP/servlet code for other non-debug purposes.
Here's the Java source code for my JSP CGI page, which I named
cgiParams.jsp. As you can see, most of these variables come from the JSP request object:
Java servlet redirect FAQ: Can you provide an example of how to perform a Java servlet redirect?
Some time ago I wrote about forwarding from a servlet to a JSP. Depending on the circumstance I might rather redirect from a servlet to a JSP. When I need to to do a redirect instead of a forward I use this code: