jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes URL has # included; want to send the url including # to the server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "URL has # included; want to send the url including # to the server" Watch "URL has # included; want to send the url including # to the server" New topic
Author

URL has # included; want to send the url including # to the server

Tilak Gorantala
Greenhorn

Joined: Jan 18, 2013
Posts: 21
Hi,

let suppose there are 2 applications say A & B.

I have my Servlet code in application A and I need to send a url to application B, which validates my credentials and returns me to the url which I gave firstly(single sign on authentication is happening here).

Now, when i am giving url which includes # in it. (let suppose for ex: http://google.com/DOC-68650#test_anchor ). and when I am giving this url, the total url has to go to application B, but only http://google.com/DOC-68650 is going to application B. I want to include #test_anchor also while passing a url from A application to B appliaction.

Can anyone please tell me how to achieve this. Appreciate your help on this.

Thankyou,
Gopi.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

You'll have to rethink this. The hash anchor is a client-side concept and is not passed to the server as part of the request.

Inspecting the raw text of the request reveals that the hash anchor is not included.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tilak Gorantala
Greenhorn

Joined: Jan 18, 2013
Posts: 21
Hi Bear,

But if I encode the url and then send it to server. If suppose I write encode & decode statements like below,

try {

String url = servletRequest.getRequestURL();

String encodedUrl = URLEncoder.encode(url, "UTF-8");

log.debug("Encoded URL " + encodedUrl);

String decodedUrl = URLDecoder.decode(url, "UTF-8");

log.debug("Dncoded URL " + decodedUrl);

} catch (UnsupportedEncodingException e) {

System.err.println(e);

}

I tried above code and i'm getting an error as -

[javac] found : java.lang.StringBuffer
[javac] required: java.lang.String
[javac] String url = servletRequest.getRequestURL();
[javac] ^
[javac] 1 error

Tilak Gorantala
Greenhorn

Joined: Jan 18, 2013
Posts: 21
Bear,

I have put the encode and decode statements and I could compile them and I have even built the JAR file successfully.

try {
//String url = (servletRequest.getRequestURL()).toString();

log.debug("Given URL " + servletRequest.getRequestURL());

String encodedUrl = URLEncoder.encode((servletRequest.getRequestURL()).toString(),"UTF-8");

log.debug("Encoded URL " + encodedUrl);

String decodedUrl = URLDecoder.decode((servletRequest.getRequestURL()).toString(),"UTF-8");

log.debug("decoded URL " + decodedUrl);

//final HttpServletRequest servletRequest = (ServletRequest) decodedUrl;

}
catch (UnsupportedEncodingException e) {

System.err.println(e);

}

I am using doFilter method in my program, I think it is a safe way to encode. Is it?
Tilak Gorantala
Greenhorn

Joined: Jan 18, 2013
Posts: 21
How to check a url whether it is valid or not, Please include # symbol while validating the url using Java
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41046
    
  43
The fact that you're battling the Servlet API should be an indicator that you're going about whatever problem you're trying to solve the wrong way.

The proper approach is to take a step back and ask yourself *why* you want to do this. To me it sounds as if URL parameters might work as well, and would not make you jump through hoops to get it done.


Ping & DNS - my free Android networking tools app
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

Ulf Dittmer wrote:The fact that you're battling the Servlet API ...


Not only the Servlet API, but HTTP itself.

Start over. This is not the approach you seek.
Arun Giridhar
Ranch Hand

Joined: Mar 10, 2012
Posts: 145

Using Referer just like the Coderanch is done (they make it hidden in Login page). By using referer they pull back to same page after Login. For # included , you got to deal with javascript >> onsubmit then >>encodeURIComponent(). Is this correct ranchers ?


hate Professionalism
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

It could likely be kludged with some JavaScript, but the point we're making is that kludges should not be necessary. The OP should rethink (and post) what he is actually trying to accomplish.
Naveen Kumar Kumar
Greenhorn

Joined: Jul 28, 2010
Posts: 19
Gopi Gorantala wrote:How to check a url whether it is valid or not, Please include # symbol while validating the url using Java


Gopi, not sure if you are still searching for validating the url, however one of the existing Apache class: "UrlValidator" can be of some help for you to start validating the urls...check the below link:

http://commons.apache.org/validator/apidocs/org/apache/commons/validator/routines/UrlValidator.html


Push the limits to push yourself..!!
 
Don't get me started about those stupid light bulbs.
 
subject: URL has # included; want to send the url including # to the server
 
Similar Threads
how to handle browser back button in jsf application
After logout application is opened using jsp
Using sendRedirect and requestDispatcher
JQuery multiple GET request URL
Sent data to different Application servlet