13

I want to pass a parameter 'A1B2C3' to a GWT application based on Google App Engine. I do it like www.example.com/index.html?key=A1B2C3. Although it's working, I'd like to use pretty URLs. Is it possible to do URL rewriting on Google App Engine? I couldn't find out how.

www.example.com/A1B2C3

instead of

www.example.com/index.html?key=A1B2C3

I'm using Google App Engine and GWT. All in Java.

Mike Brecht
  • 151
  • 1
  • 4
  • It is possible, I did it, But using Python instead of Java and Flask as framework. – DDS Nov 08 '17 at 13:42

7 Answers7

7

This is a cool question. I figured out how to do it for python as well.

app.yaml:

- url: /test/(.*)
  script: test.py \1

test.py:

#!/usr/bin/env python

import sys

def main():           
  for arg in sys.argv:
     print arg

if __name__ == '__main__':                               
  main()
Kousha
  • 1,575
  • 14
  • 18
  • 1
    He's asking about java. Maybe this answer would be better on a python based question? However I do agree this would work in python. – Joshua Patton Feb 21 '10 at 19:47
  • 4
    Yes. I do realize this was a java related question, but I was looking for this similar thing when searching on the internet. I found this article, but I wanted to know how to do it with python. When I figured it out, I thought I should put it up here so other people wouldn't have to search as far as I did. – Kousha Feb 22 '10 at 00:09
  • 1
    I'll second that - Thanks Kousha! – Neil Kodner Nov 16 '10 at 01:37
6

You need to configure the application (see here). In other words, you need to "wire" the patterns you want.

From the manual, an example:

<servlet-mapping>
    <servlet-name>redteam</servlet-name>
    <url-pattern>/red/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>blueteam</servlet-name>
    <url-pattern>/blue/*</url-pattern>
</servlet-mapping>
jldupont
  • 93,734
  • 56
  • 203
  • 318
  • Thanks for your answer. I've tried this before, but I only see how this works with Java servlets. However, I programmed the application in GWT and there I don't have the abstraction of servlets. Any idea how this can work with GWT? – Mike Brecht Feb 19 '10 at 15:01
  • Look at URL rewriting in your HTTP server - like in Apache (http://httpd.apache.org/docs/2.0/misc/rewriteguide.html) or nginx (http://wiki.nginx.org/NginxConfiguration#Rewrite_examples). – Igor Klimer Feb 19 '10 at 15:14
  • Thanks. But seems like this doesn't work on Google App Engine. – Mike Brecht Feb 19 '10 at 15:18
  • 1
    with GWT, you go from "page to page" usually through an "#page" URL pattern - that's how the GWT application doesn't get reloaded on each page change. If you really want to navigate away from a page, then you'll end up having to reload the GWT application on each new page... that's not a very efficient way of using GWT. – jldupont Feb 19 '10 at 15:31
  • 2
    Hm not this is not how I navigate within the application. However, users need to be able to get links to the forms they've created, and they need to be able to distribute these URLs (e.g. www.questionform.com/form.html?key=123 - however, I'd like to make this nicer by giving them URLs like www.questionform.com/123 – Mike Brecht Feb 19 '10 at 15:48
  • Then distribute the URL to the form using the pattern "/#/formKey". The usual practice with GWT is to perform AJAX calls to the server. – jldupont Feb 19 '10 at 16:00
  • @Mike Brecht: I see you are new around here. Have Fun! (BTW, have you read the FAQ? For the questions you ask, it is customary to "accept" the answer that is "best" to your liking. Cheers. – jldupont Feb 19 '10 at 17:03
3

Try UrlRewriteFilter: http://tuckey.org/urlrewrite/ (or github repo) it is a plain ol' Java EE filter, so it should work.

konqi
  • 5,137
  • 3
  • 34
  • 52
Jeroen
  • 441
  • 2
  • 4
1

Save yourself some time and use Restlet. You can do exactly this and I've done this in two different projects. It's quite straight forward. If you need some help, let me know.

JP Richardson
  • 38,609
  • 36
  • 119
  • 151
1

I would probably use PrettyFaces, http://ocpsoft.com/prettyfaces/ which allows you to do URL-mappings directly on top of an existing application.

You just configure something like this in the pretty-config.xml file:

<url-mapping>
   <pattern value="/my/pretty/url" />
   <view-id value="/my/existing/url" />
</url-mapping>

Or if you want to rewrite parameters, you can do this:

<url-mapping>
   <pattern value="/my/pretty/url/#{param}" />
   <view-id value="/my/existing/url" />
</url-mapping>

And this means that any urls (inbound) now become:

/my/pretty/url/value -> /my/existing/url?param=value

And outbound your URLs will look like this in HTML pages and in redirects:

/my/existing/url?param=value -> /my/pretty/url/value

So it's easy to add on to your current apps.

Lincoln
  • 3,151
  • 17
  • 22
  • 1
    PrettyFaces did not work on app-engine + GWT. Maybe there is a way, but did not work for me. Got a "Failed startup of context" "javax.servlet.UnavailableException: Configuration problem" – eddyparkinson Feb 25 '13 at 03:33
  • Please post the exception - you can post on the OCPsoft support forums: http://ocpsoft.org/support/ – Lincoln Feb 27 '13 at 17:24
  • Im gonna try this. Thank you. –  Nov 09 '13 at 08:44
0

Here is another project that I think may really help you:

It's called restful-gwt... it's pretty slick too: http://code.google.com/p/restful-gwt/

Good luck!

JP Richardson
  • 38,609
  • 36
  • 119
  • 151
0

This is the best approach I found so far for implementing URL rewrite GAE Python

Sim
  • 803
  • 1
  • 6
  • 10