UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » Java
  • » Help to understand 2 small piece of java code...

#1 2008-02-14 11:15 PM

felix
Member
Registered: 2003-12-04
Posts: 171

Re: Help to understand 2 small piece of java code...

Hi guys,

How are you?

Long time I does not post my dumb questions, I'm here again... hehe :)

I looked at a java (jsp) source code compiled using BEA WebLogic, and I have doubts about what 2 small piece of codes do, if someone can help me.

A) I couldn't understand exactly what does this substring(requestURL.indexOf("://") + 3) and substring(requestURL.lastIndexOf(":")), see the full snip of code below:

String requestURL = httpRequest.getRequestURL().toString();
   String redirect =  "http://" + requestURL.substring( (requestURL.indexOf("://") + 3 ), ( requestURL.lastIndexOf(":"))) + "/SUS";

   // Some irrelevant code.

   httpResponse.sendRedirect( redirect );


The method httpRequest.getRequestURL() retrive the current URL, right? However it obtain it from where? The host header used in HTTP requests?

I understand that substring() cut a piece of the paramter, but in this case I can't figure out what it's cutting. What should be the output of this snip of code (if for example, the requested URL was http://www.10.0.0.1/SUS/index.jsp)?


B) Other small piece of code that look interesting for me, is...

<jsp:include page="<%= window.getHeaderUri() %>"/>

The doubt it, what is this window.getHeaderUri()? For me, this appear javascript, however it's inside java tag code <jsp:/>. So I belive it's a specifc java bea weblogic functions, my doubt is, what it reads (returns)? Is it really a server side function and not javascript? From where he get this information (HTTP Headers)? What header?

The window.getHeaderUri() sometimes is window.getContentUri().

Thnak you a lot.

Regards,

Offline

#2 2008-02-19 07:40 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: Help to understand 2 small piece of java code...

Ack... servlets... hopefully the following is correct...

The getRequestURL call is going to return the URL excluding any query parameters tacked onto the end... e.g. http://foo.com:80/bar?abc=123 passed to the function should return a buffer containing http://foo.com:80/bar

For the first substring call... requestURL.indexOf("://") + 3 - given the same  'parsed' out request URL as above ( http://foo.com:80/bar ) will return the index to the letter 'f' in the string...

The second substring call... requestURL.lastIndexOf(":") would return the index of the ':' between the 'm' in 'com' and the '80'...

The final substring call ( that uses the first and second one to generate indexes ) would simply return "foo.com"...

This leads me to question where this code originated... see the problem - unless I have mis-read the code fragment or confused my Java functions - is that if you had the ( original ) URL of just http://foo.com ( note - I have removed the explicit port specifier from the URL ) then that final substring call would be requestURL.substring( 7, 4 ) ... the 4 is the location of the ':' character after the 'http' string... which would result in an IndexOutOfBounds exception being throw since the start index is larger than the end index...

?!


Michael

P.S. ignore the fact that the board turns those into actual links... it did it automatically... :(


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#3 2008-02-19 09:47 PM

felix
Member
Registered: 2003-12-04
Posts: 171

Re: Help to understand 2 small piece of java code...

Hi mlampkin,

Thank you for reply, was very useful! All this job, to only return the exact hostname of the URL... hehehe

I don't know if it cause a IndexOutOfBounds, but this code works well.

Any idea about the other java snipp of code ? :)

Thank you,

Regards

Offline

#4 2008-02-19 10:06 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: Help to understand 2 small piece of java code...

Alas... sorry but I'm not really that familiar with jsp or the BEA stuff...

Anyone else out there know what that fragment is doing ?


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#5 2008-12-27 04:58 AM

tony47ch
Guest

Re: Help to understand 2 small piece of java code...

nice to meet you all.

  • Index
  • » Java
  • » Help to understand 2 small piece of java code...

Board footer

Powered by FluxBB