Strip HTML Tags in Java

I came upon a situation in which I needed to find out if String was "really" empty (not just spaces and junk).  The problem was that the String might have empty HTML tags, like <p></p>.  This handy function removes all the HTML tags, leaving any real content.

function removeHtmlTags(String original) {
    return original.replaceAll("\<.*?>", "");
}

The results?

removeHtmlTags("<p></p>")  returns the empty string, but

removeHtmlTags("<p>foo</p>")  returns "foo"

Note: This article originally appears in my Java blog (javanature.com).  I’m shutting that one down, so I’m moving the good stuff that is left over here!

Using the @PathVariable annotation in Spring MVC 3.0

Let’s say that you are creating an user account-based web application in Spring 3.0.  You would like to create a controller that would allow users to see each other’s profile by using a URL such as http://mysite/profile/username.  For example:

http://mysite/profile/Adam  would show user Adam’s profile.

http://mysite/profile/Baker would show user Baker’s profile.

Spring 3.0’s annotated controllers make this kind of mapping easy with the new @PathVariable mapping.

package com.technologicaloddity.hellowebapp;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ShowProfileController {

    @RequestMapping("/profile/{username}")
    public String showProfile(Model model, @PathVariable("username") String username) {
        model.addAttribute("username", username);
        return "showProfile"; // the view name
    }
    
}

As you can see, the RequestMapping for the showProfile method has something a bit unusual: {username}.  This is a PathVariable, and it means that this method will serve any request of the format "/profile/someUserName".  We capture the actual username in the next line using the @PathVariable annotation, and store it in the String username.  Now we can use it however we want!

Hello World Web App using Spring Annotations Part 6

In part 6, the final part of this series, we’ll create the View to display our message to the end-user.  We’ll also run the whole app under Tomcat and Sysdeo, and even show a bit of Java class hot-swapping.  Lastly, we will create a WAR file from our project.

If you have missed the previous parts of this series, you can find them here: part 1, part 2, part 3, part 4, part 5.  You’ll need to setup your environment from parts 1-4 if you want to follow along with the examples here.

You can download the full source of this project from http://technologicaloddity.com/code/general/HelloWebApp.zip.

Continue reading Hello World Web App using Spring Annotations Part 6

Hello World Web App using Spring Annotations Part 5

In Part 5 of this series, we finally get to write some Java!  We’ll create a simple Spring controller using Annotations, using the web application requirements from part 1.

If you have missed the previous parts of this series, you can find them here: part 1, part 2, part 3, part 4.  You’ll need to setup your environment from parts 1-4 if you want to follow along with the examples here.

Continue reading Hello World Web App using Spring Annotations Part 5

Hello World Web App using Spring Annotations Part 4

In part 4 of this series, we’ll finish off the configuration for the web application in Eclipse with Spring and Log4J configuration files.  We’ll also start the Tomcat server with Sysdeo and our dependencies loaded for the first time, and verify that Spring is working correctly.

Be sure you have set up Sysdeo Tomcat plugin and have your workspace setup as described if you want to follow along with the examples.  If you missed the previous parts, you can find them here: part 1, part 2, part 3.

Continue reading Hello World Web App using Spring Annotations Part 4