JSP 2.0 SEO friendly links encoding
Asked Answered
S

2

8

Currently I have something like this in my JSP

<c:url value="/teams/${contact.id}/${contact.name}" />

The important part of my URL is the ID, I just put the name on it for SEO purposes (just like stackoverflow.com does).

I was just wondering if there is a quick and clean way to encode the name (change spaces per +, latin chars removal, etc). I'd like it to be like this:

<c:url value="/teams/${contact.id}/${supercool(contact.name)}" />

Is there a function like that out there or should I make my own?

Swann answered 2/5, 2010 at 9:8 Comment(0)
P
6

Nothing like that is available in JSTL functions. You'll need to create your own. I'd by the way rather replace spaces by -.

To the point, you want to perform the following steps:

  1. Lowercase the string.

    string = string.toLowerCase();
    
  2. Normalize all characters and get rid of all diacritical marks.

    string = Normalizer.normalize(string, Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
    
  3. Replace all remaining non-alphanumeric characters by - and collapse when necessary.

    string = string.replaceAll("[^\\p{Alnum}]+", "-");
    

You can wrap this in an EL function:

package com.example;

import java.text.Normalizer;
import java.text.Normalizer.Form;

public final class Functions {
     private Functions() {}

     public static String prettyURL(String string) {
         return Normalizer.normalize(string.toLowerCase(), Form.NFD)
             .replaceAll("\\p{InCombiningDiacriticalMarks}+", "")
             .replaceAll("[^\\p{Alnum}]+", "-");
     }
}

Which you register in a /WEB-INF/functions.tld like follows:

<?xml version="1.0" encoding="UTF-8" ?>
<taglib 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    version="2.1">

    <display-name>Custom Functions</display-name>    
    <tlib-version>1.0</tlib-version>
    <uri>http://example.com/functions</uri>

    <function>
        <name>prettyURL</name>
        <function-class>com.example.Functions</function-class>
        <function-signature>java.lang.String prettyURL(java.lang.String)</function-signature>
    </function>
</taglib>

Which you can use in JSP as follows:

<%@taglib uri="http://example.com/functions" prefix="f" %>
...
<a href="teams/${contact.id}/${f:prettyURL(contact.name)}">Permalink</a>
Philippopolis answered 2/5, 2010 at 12:53 Comment(2)
By the way, is there any doc regarding using - instead of + for spaces?Swann
No, someone else did :) The downvote was a while after my update. The reason that the - is preferred is because the + is actually the "URL-safe" character for a space, not intended for SEO purposes. Look how Stackoverflow and another major sites do it.Philippopolis
B
2

Look for server.urlencode, all the major server side languages today have them.

Blinker answered 2/5, 2010 at 22:15 Comment(1)
This only applies percent encoding. That's not really exactly what he's asking for. It would otherwise indeed have been a very straightforward answer :) It's by the way called java.net.URLEncoder. Not server.urlencode or something C-ish like that.Philippopolis

© 2022 - 2024 — McMap. All rights reserved.