Archive for the ‘Java’ Category

Search Engine Friendly URLs for Java Web Application

Wednesday, March 28th, 2007

Static and Dynamic URLs

Any web-application has static and dynamic resources. As the names imply static resources are those that are never changed.

For example, html pages with no dynamic data, static download files etc.

Dynamic resources are those that can change their content from time to time: html pages that contain dynamic data (such as results of search queries), dynamic reports, download files that may change their content depending on the visitor’s preference and so on.

Usually we may easily distinguish static and dynamic URLs:

http://www.mysite.com/pictureOfMyDog.jsp - static URL.

http://www.mysite.com/picture.jsp?id=234&operation=show - dynamic URL.

URLs and Search Engines

Search engines like static URLs. When the search engine spider comes across a dynamic URL it will or will not (may and may not) follow it. Depending on the internal algorithms, the search engine will choose the most optimal way to go.

If your site is popular and the search engine knows about it, it can index your dynamic resources.

If later the search engine finds out an old dynamic URL does not work any more or leads to a different page it can stop indexing such URLs.

The more parameters your URL has the less are the chances the search engine will follow such URLs. The spider can follow them to check out what kind of content the pages contain and if there is any useful content at all.

URLs and Internet Surfers

What if someone likes the picture of your dog located at this URL:

http://www.mysite.com/picture.jsp?id=234&operation=show.

The poor guy bookmarks the page as he will never remember such an awful URL and thus will never show that picture to his girlfriend at her place. Later on when they come to the Poor Guy’s place and he wants to show that picture and he clicks the bookmark they miserably stare at a 404 page.

It happens because you decided to do a major rewrite of your code and now the picture of your beautiful dog is located here:

http://www.mysite.com/picture.jsp?photoId=234&operation=show

Moreover, if they do not find the picture, the night is spoiled! Only because the webmaster has never thought about static URLs.

URL Friendliness and Intranet Applications

As the value of static URLs is clear for the web application exposed to public in the Internet, some may think they are useless for the Intranet applications. The search engines do not index those applications. The URLs usually do not change. Well, I though that too. However, if you think about it a little more you will find out that it can be of great use for your application as well.

First of all this is just a plain aesthetic pleasure. If you do not care about nice URLs, think about the developers who can save time and probably someone’s money by typing shorter URLs and by making fewer mistakes in those URLs.

On my day job, it happens that I have to browse the application we are developing on the computers of other developers through the network. In addition, every time I have to type the URL of the login page with the developer’s machine network address. Moreover, every time this is a lot of pain. This URL is long and complicated. And this is the URL that everyone has to type pretty often, as there is no way to save all of them in an ugly IE bookmarking facility. This is the first example I can think of but I am sure that if you think about that a little bit you will find many reasons to have static URLs in your applications.

Java and Search Engine Friendly URLs

The users of Apache HTTP server are happy to have to have such functionality almost out-of-box. There is no standard solution for J2EE platform. Fortunately, there is a project that let you have the desired functionality. This is called Url Rewrite Filter.

“Based on the popular and very useful mod_rewrite for apache, UrlRewriteFilter is a Java Web Filter for any J2EE compliant web application server (such as Resin, Orion or Tomcat), which allows you to rewrite URLs before they get to your code. It is a very powerful tool just like Apache's mod_rewrite.
URL rewriting is very common with Apache Web Server (see mod_rewrite's rewriting guide) but has not been possible in most java web application servers”.

Visit their web site and download the filter code, documentation and manuals.

The documentation of this filter is great and the usage is pretty simple, however I will show you an example:

I have a long ugly URL:

http://www.mysite.com/SoftwareList.do?operation=showList&chapterId=X

I want it to look nice and to be search engine friendly:

http://www.mysite.com/category-programs/audio-and-video-/X

We compose a rule:

XML:

  1. <rule>
  2.         <from>^/category-programs/(.*)/([0-9]+).*$</from>
  3.         <to>/SoftwareList.do?operation=showList&amp;chapterId=$2</to>
  4.     </rule>

And place that rule to urlrewrite.xml.

Remember, you have to encode all relative URLs, otherwise the paths to css, js, images and all other paths will be corrupted. So use jstl’s tag for all your relative paths.

Alternatively, you may add ‘redirect’ attribute to the rule:

XML:

  1. <rule>
  2.         <from>^/category-programs/(.*)/([0-9]+).*$</from>
  3.             <type="redirect">/SoftwareList.do?operation=showList&amp;chapterId=$2</to>
  4.     </rule>

But then the user will see this ‘ugly’ URL in the address line of the browser.

Conclusion

Url Rewrite Filter is a great way for J2EE developers to add static url functionality to their applications. It is simple and easy, the configuration file is automatically reloaded occasionally (you define the interval). That is it for now. I have written an URLAbstractor class and a custom tag to make clean URLs out of any String.

URL Beautifier

I have written a small class to convert a string to a pretty URL :

JAVA:

  1. package com.leadercode.tag.url;
  2. import java.io.UnsupportedEncodingException;
  3.  
  4. /**
  5. • URLAbstractor
  6. • @author Sergey Nechaev
  7. *
  8. */
  9. public class URLAbstractor {
  10. public static String encode(String url) throws UnsupportedEncodingException {
  11. StringBuffer out = new StringBuffer(url.length());
  12. for (int i = 0; i <url.length(); i++) {
  13. int c = (int) url.charAt(i);
  14. switch © {
  15. case ‘ ‘:
  16. case ‘&’:
  17. case ‘,’:
  18. case ‘.’:
  19. case ‘:’:
  20. c = ‘-‘;
  21. break;
  22.             }
  23.  
  24. if (c == ‘-‘ && i> 0 && out.charAt(out.length() - 1) == ‘-‘) {
  25. continue;
  26.             }
  27.  
  28. out.append((char) c);
  29.         }
  30.  
  31. return out.toString();
  32.     }
  33. }
  34.  
  35. SEF Tag
  36. package com.leadercode.tag.url;
  37. import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyTagSupport;
  38.  
  39. /**
  40. • The URL abstractor tag
  41. • @author Sergey Nechaev
  42. *
  43. */
  44. public class SefLink extends BodyTagSupport {
  45. private String url;
  46. public String getUrl() {
  47. return url;
  48.     }
  49.  
  50. public void setUrl(String url) {
  51. this.url = url;
  52.     }
  53.  
  54. public int doStartTag() throws JspException {
  55. try {
  56. pageContext.getOut().print(URLAbstractor.encode(url));
  57. } catch (Exception e) {
  58.  
  59.         }
  60.  
  61. return SKIP_BODY;
  62.     }
  63. }

Software Development Can Be Fun

Monday, March 19th, 2007

Now the developer has to know a bunch of different technologies, systems and libraries. And each day you start your favorite IDE, HTML editor, XML editor, a couple of utilities along with Outlook Express program and run a server. The computer is as slow as hell and you start cursing the developers and the computer.

I am in the development of the presentation tier for the large contract management application writing some java code, composing some HTML & JSP pages and writing some crazy client-side JS components, writing and fixing long XML and .property files.

We use an excellent Eclipse IDE along with a set of plug-ins known as MyEclipse. This is a very powerful system, however most of time (I mean always) the only plug-in we use is the nice syntax highlighting of JSP, JS & XML files.

Once I asked the colleague to help me fix some annoying bug in JSP code. He logged in to my server, opened the page in IE, right-clicked it and opened the HTML source in a notepad. No, it was not a standard grey ugly windows notepad. A nice editor highlighted the source code. We used its folding option to find the bug – I forgot to insert the closing JSP tag. The HTML source looked great!

Notepad++

I asked to give me this editor (Notepad++) and installed it on my computer. A couple of seconds to create proper file associations using the Notepad++’s convenient menu and it is ready to go!
Notepad++ highlights many file formats, it starts very fast as it was written in C++ and is very small (less than 1 Mb). Each new file is opened in a new tab, and when you close the notepad and reopens it – it loads the files that had been opened!

There is a number of text processing plug-ins, some TextFX plug-ins. I did not look into it, but at a glance, I liked the HTML/XML auto-closing feature.

Now I use it on daily basis and let me tell you this is the only program I really like 

Here are the features of Notepad++ (taken from the web site):

Syntax Highlighting and Syntax Folding
User Defined Syntax Highlighting
Auto-completion
Multi-Document
Multi-View
Regular Expression Search/Replace supported
Full Drag ‘N' Drop supported
Dynamic position of Views
File Status Auto-detection
Zoom in and zoom out
Multi-Language environment supported
Bookmark
Brace and Indent guideline Highlighting

View Screenshot

Day 1

Friday, March 16th, 2007

So, I have just come across a Cas’s “The Alien Flux Development Diary”. And as he and his team are the creators of LWJGL engine, and as I am using it to write my next 2D game I thought maybe I should start my diary of my own.

I am planning to write a simple, so-called ‘casual’ game – a remake of ‘Dr.WEB’ game. I really loved that game; I played it on ‘dandy’ game console (a Chinese clone of NES console). Moreover, I want to use Java.

I like Java. I have thought a lot about the technologies to use, I know many languages, platforms and tools. It is really a tough task to make a choice, a right choice. However, after a long and painful thinking I had a small set:

1. BlitzMax
2. C/C++ with multiplatform library (GTK, SDL or Torque)
3. Java (OpenGL binding)

BlitzMax

Blitzmax is great for a newbie game developer, who has started writing some scripts. It has a fairly small price (~80 dollars), a large community and an as a result many libraries. This is wonderful. What I do not like is its editor (which really sucks, at the times of Eclipse I cannot look at BlitzMax without tears). Its debugger is lame as well. I think for any 21st century professional developer this ‘IDE’ is just an ‘IDE’ in quotes. It is basically a bad-programmed notepad. No refactoring abilities, even no normal text search.

C/C++

I just did not want to go C/C++ way. I like programming a lot. I never really liked C/C++. That probably would be the last tool I would have chosen.

Java

I do J2EE stuff at my day job, writing some complicated application for some US pharmaceutical company. J2EE is not my pair of shoes but I have to do it to get paid. But I love Java a lot. Java gives you so many out-of-box libraries that would make any programmer happy. Rich GUI, great Java2D drawing library, net classes, easy file handling – all you need to write a game and much more. Moreover, for free. And great free tools such as Eclipse, NetBeans, Ant, the greatest community of developers, the greatest number of libraries and frameworks. Speed? Forget about it. I am programming on a Celeron-940 machine with GeForce 5600 installed. Such configuration is very common or even a bit old.

LWJGL is a mature framework. There are a few released games based on LWJGL. And it is rather intuitive and transparent – that is what I like in libraries. And it is free - I like it too. I have had too many projects so I know that I should stay away from paying too much for tools and domains or any other stuff - I gave up these projects pretty often as well :(

So, by now I have written a small scene manager to render sprites with animators, a font manager to render strings in TTF fonts (by using Java2D to create glyphs and to generate OpenGL display lists later).

This is the recent screenshot: 50 fade-ins rotating sprites (sprite node with two animators: rotating animator and fade-in animator). And some strings from the windows “Arial” font and one from the loaded “eclipse.ttf” font. Yoshi is a Sprite class, with different positions, transparency and size. Check out the code that displays the dinos:

Load the sprite:

JAVA:

  1. BSprite sprYoshi = new BSprite("resources/test/yoshi.png");

Draw Yoshi sprites:

JAVA:

  1. sprYoshi.setAlpha(1.0f);
  2. sprYoshi.setSize(256,256);
  3. sprYoshi.setPosition(screen.getWidth() - 256, screen.getHeight() - 256);
  4. sprYoshi.render();     
  5.            
  6. sprYoshi.setSize(512, 512);
  7. sprYoshi.setPosition(screen.getWidth() - 512, screen.getHeight() - 156);
  8. sprYoshi.render();
  9.            
  10. sprYoshi.setSize(128, 128);
  11. sprYoshi.setPosition(screen.getWidth() - 712, screen.getHeight() - 106);
  12. sprYoshi.render();     
  13.            
  14. sprYoshi.setSize(96, 96);
  15. sprYoshi.setPosition(screen.getWidth() - 762, screen.getHeight() - 106);
  16. sprYoshi.render();       
  17.            
  18. sprYoshi.setSize(96, 96);
  19. sprYoshi.setAlpha(0.5f);
  20. sprYoshi.setPosition(screen.getWidth() - 812, screen.getHeight() - 106);
  21. sprYoshi.render();

I could have added many sprites and add them to the SceneManager class to render automatically.

I want to get as close as possible to the simplicity of such languages as BlitzMax. So far so good.

Now I am planning to write a particle manager. I love particles, particles are my passion. Hope to create an extendible and powerful particle engine. I want a lot of great effects for this and my future games.

A Few Words on Synth

Friday, February 23rd, 2007

If you are into heavy server-side development, you probably do not pay much attention at other Java features, such as GUI. However, I like to dig everything around this great language and the platform. Some time ago, I came across “Synth” thing in Java. This is a way to customize and change look’n feel of your Swing application with no ugly code tweaks, but with a single XML file, which is great for designers and other not-programming folks.

I played with it for some time, came across some hard to find and fix issues, read about exciting and promising Synth features in JDK 6 and left it for the future.

The future is now but still there is not so much information on the subject and that is surprising. With the ‘new’ JDK 6, I hoped there would be more information, more tutorials; people would start looking at it. However, unfortunately the only information about Synth I can find on Google dates back to 2005. Strange.

Well, maybe today I will look into it again and try to get something done, like to build a ‘Javazing’ look’n feel that will resemble the design of that blog.