-2

Suppose the following http request is sent:

http://example.com#!&parm1=123

I want to access the value for param1 in Java in a Servlet. How can I do this? I know that #! is used for Ajax parameters, but in this case the request goes to a Java Servlet, and the Servlet should be able to grab the value for param1.

Note: Most people are confused by this. The ! in hashbang, meaning in #!, cancels out the function of the hash (#). Thus the param1 does get sent, but my problem is accessing it in a Java Servlet

Thanks, Cyrus

  • Could be a little more specific? What framework are you using on the server side? What code have you written so far and where are you stumped? – markspace Jul 24 '14 at 20:05
  • I've updated the post. Basically, I want to access the parameter in a servlet. I'm working on a big company website, so posting the code would be useless here. But how do I access it in a simple Servlet? –  Jul 24 '14 at 20:16
  • 1
    @user2295633 - you're mistaken. The `!` is just a signal to google that the same content is available through a typical url. If you're using a client-side framework, it may be listening to hash changes and triggering an ajax request. – Sam Dufel Jul 24 '14 at 20:23
  • You might check this out: http://www.smashingmagazine.com/2011/09/27/searchable-dynamic-content-with-ajax-crawling/ smashing magazine uses a javaservlet to handle ajax crawlable urls. I'm pretty certain you never end up actually sending the url with the #! to the server. You have one for google and then your normal ajax request though the site map. – scrappedcola Jul 24 '14 at 20:28
  • 1
    Please inspect your network panel, locate the actual request being sent, and post that. The actual format of that request will vary depending on your JS. – Sam Dufel Jul 24 '14 at 20:33
  • `#` makes a url never hit the server if its been visited already. That's a way of telling the browser, hey, just go to a particular spot in the page, don't even bother reading it from the server again. – developerwjk Jul 24 '14 at 20:35
  • 2
    `#!` is NOT used for Ajax parameters – developerwjk Jul 24 '14 at 20:40
  • 2
    `Note: Most people are confused by this.` No, you are confused by this. `#!` means NOTHING, it was made up by Google to try to solve a problem that has been fixed with modern browsers with pushState. In no way is it sent to the server. As a developer, your clientside framework is supposed to handle it. – epascarello Jul 25 '14 at 02:35

1 Answers1

2

The part of the url after the # is not necessarily related to AJAX. It is the document hashcode or fragment identifier. Modern AJAX patterns do use this identifier as a place to store parameters and other information.

The important fact is that this is never sent to the server. The server can send urls with fragments, but the client never sends the fragment to the server.

You need to parse the url and extract the values after the #. Then you can send them to the server using a GET or POST request or you can make an ajax request and pass the parameters in. This question has some examples of code that can be used to parse the hashcode, assuming it is formatted like a typical GET query (that is, ?p1=v1&p2=v2). If you are using a framework for your app development, there may be a built-in function you can use for this, such as jquery BBQ.

The #! syntax is a convention defined by Google: see here. When a crawler encounters this syntax it replaces the !# in the url with a parameter: _escaped_fragment_. So a url like

http://stackoverflow.com#!p1=v1&p2=v2

becomes

http://stackoverflow.com?_escaped_fragment_=p1%3Dv1%26p2%3Dv2

On your server you can then access the _escaped_fragment_ parameter as you would normally access any other parameter.

Note: the important thing about this particular convention is that it is the web-crawling user-agent (i.e. Googlebot) that rewrites urls to use the _escaped_fragment_. If you use these kinds of urls, and want to make your site crawlable, you should support reading the _escaped_fragment_ parameter. If you don't care about Googlebot for these particular pages, this whole convention is useless to you. Either way, it is the client-side responsibility to handle the parameters in the hashcode.

Community
  • 1
  • 1
Mr. Shiny and New 安宇
  • 13,822
  • 6
  • 44
  • 64
  • Thanks for your reply; but we are talking about 2 different things. Most people are confused by this. The ! in hashbang, meaning in #!, cancels out the function of the hash (#). Thus the param1 gets sent, but my problem is accessing it in a Java Servlet. –  Jul 24 '14 at 20:13
  • 2
    But it **DOESN'T** cancel out the hash tag. There's nothing in the URL spec that says it does. So Java interprets the # correctly as a start of an anchor, and that's what you see in your servlet. – markspace Jul 24 '14 at 20:20
  • @markspace, Java doesn't interpret the `#`, the browser does. – developerwjk Jul 24 '14 at 20:37
  • @user2295633 I've edited my answer to expand on the particular #! convention. The initial part that I wrote still stands. The server (i.e. Java) does not see any part of the #hashcode at any time. – Mr. Shiny and New 安宇 Jul 24 '14 at 21:56