How to replace XML servlet mappings with Scalatra mounts

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 15.6, “How to replace XML servlet mappings with Scalatra mounts.”


You want to add new servlets to your Scalatra application, and need to know how to add them, including defining their URI namespace.

How to forward from a Java servlet to a JSP alvin November 11, 2009 - 9:05am

Here's a quick example that shows a complete method that I use in a Java servlet to forward to a JSP (JavaServer Page).

Just pass the method an HttpServletRequest, an HttpServletResponse, and a URL, and you're in business. Note that my JSP url string typically looks something like "/myPage.jsp".

Java servlet session - how to test for a valid session in a servlet

When you're writing Java web applications -- for example, an application like a discussion forum -- you end up writing Java servlets where you need to know if the user accessing your servlet has a valid Java servlet session established. If they have a valid servlet session established, you might do one thing in your servlet code, but if they don't have a valid servlet session, you might handle their request differently.

JSF HttpSession - How to access the servlet session from JSF alvin October 16, 2009 - 9:08am

JSF HttpSession FAQ - How do I access the traditional HttpSession from my JSF code?

I've been working with JavaServer Faces (JSF) a lot lately, and one thing I was curious about was how I can get back to the old-fashioned Java session (HttpSession) if and when I need to, while also wondering what was in the session.

How to put an object on the request in a servlet alvin June 1, 2009 - 9:00am

Many times when you're working with Java servlets and JSP's, you'll want to forward some piece of information from your servlet to your JSP without having to put that piece of information into the session. For instance, in many applications you may not have a user session, and in other cases where you do have a session, you may just not want to put a bunch of junk in there.

How to test for a valid user session in a JSP

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?"

JSP URI/URL - How to get the request URI, URL, and Context from a JSP

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:

A Hello World servlet alvin April 9, 2009 - 5:06pm

Last week I was working with OpenSSO quite a bit, and at one point I just needed a really simple Java servlet that I could first try to run, and then try to secure, using OpenSSO. All I really needed for this purpose was a "Hello world servlet", and that's when I realized I've never put one out here.

So, to fix that problem, here is the source code for a very simple Java servlet that will print "Hello, world" when you access it through a browser. To get started, here's the source code for the servlet:

Java servlet example - HttpServletRequest, getPathInfo, and getRequestURI

Working on tag support for this blog, I've cut a little sample code out of a Java servlet, and I'm showing it below. I was looking at the HttpServletRequest, and was curious about the difference between request.getPathInfo(), request.getPathTranslated(), and request.getRequestURI(), so I created this demo code. Here's the example code from the servlet: