saleemkhan
asked on
How to use(include) javascript variable into jsp tag?
hi all,
i would like to know how to include javascript varaiable inside jsp tag
here is the code
public class Str
{
public static String Replace(String sSource, String sFrom, String sTo)
{
int index =0;
System.out.println("1");
String dest = "";
System.out.println("2");
index = sSource.indexOf(sFrom);
System.out.println("3");
while (index >= 0)
{
System.out.println("4");
dest = sSource.substring(0, index);
System.out.println("5");
dest += sTo + sSource.substring(index + sFrom.length());
System.out.println("6");
sSource = dest;
System.out.println("7");
index = sSource.indexOf(sFrom, index + sTo.length());
System.out.println("8");
}
if (dest == "")
{
System.out.println("9");
dest = sSource;
System.out.println("10");
}
return dest;
}
}
<%
Str s =new Str();
%>
<a href="#" onclick="javascript:locati on.href='< %=s.Replac e(location .href,"-1" ,"-11");%> '">clickhe re</a>
s is my java class and i have Replace method.
this Replace method takes the path and replaces strings.
value of javascript location.href i want to pass to Replace method of my java class.
waiting for expert solution.
i would like to know how to include javascript varaiable inside jsp tag
here is the code
public class Str
{
public static String Replace(String sSource, String sFrom, String sTo)
{
int index =0;
System.out.println("1");
String dest = "";
System.out.println("2");
index = sSource.indexOf(sFrom);
System.out.println("3");
while (index >= 0)
{
System.out.println("4");
dest = sSource.substring(0, index);
System.out.println("5");
dest += sTo + sSource.substring(index + sFrom.length());
System.out.println("6");
sSource = dest;
System.out.println("7");
index = sSource.indexOf(sFrom, index + sTo.length());
System.out.println("8");
}
if (dest == "")
{
System.out.println("9");
dest = sSource;
System.out.println("10");
}
return dest;
}
}
<%
Str s =new Str();
%>
<a href="#" onclick="javascript:locati
s is my java class and i have Replace method.
this Replace method takes the path and replaces strings.
value of javascript location.href i want to pass to Replace method of my java class.
waiting for expert solution.
you can't, javascript is run on the client, whereas jsp is run on the server.
replace
<a href="#" onclick="javascript:locati on.href='< %=s.Replac e(location .href,"-1" ,"-11");%> '">clickhe re</a>
with
<a href="#" onclick="javascript:locati on.href='< %=s.Replac e( request.getRequestURL(), "-1", "-11" );%>'">clickhere</a>
<a href="#" onclick="javascript:locati
with
<a href="#" onclick="javascript:locati
(ie: replace location.href with the JSP equivalent)
ASKER
hi,
i already tried request.getRequestURL() like below
<a href="#" onclick="javascript:locati on.href='< %=s.Replac e( request.getRequestURL(), "-1", "-11" );%>'">clickhere</a>
output of request.getRequestURL is below
http://abc.com/webapp/wcs/stores/Store1/StoreCatalogDisplay.jsp
i used javascript alert location.href out put is
http://abc.com/webapp/wcs/stores/servlet/StoreCatalogDisplay?param1=100¶m2=401¶m3=-1
i want to replace param3=-1 to param3=-11
so how can we fix my problem.
i already tried request.getRequestURL() like below
<a href="#" onclick="javascript:locati
output of request.getRequestURL is below
http://abc.com/webapp/wcs/stores/Store1/StoreCatalogDisplay.jsp
i used javascript alert location.href out put is
http://abc.com/webapp/wcs/stores/servlet/StoreCatalogDisplay?param1=100¶m2=401¶m3=-1
i want to replace param3=-1 to param3=-11
so how can we fix my problem.
Ahhh... Ok, so you need the query string too:
So try this:
<%
Str s =new Str();
String url = request.getRequestURL() ;
String params = req.getQueryString() ;
if( params != null )
url += "?" + params ;
%>
<a href="#" onclick="javascript:locati on.href='< %=s.Replac e( url, "-1", "-11" ) %>'">clickhere</a>
So try this:
<%
Str s =new Str();
String url = request.getRequestURL() ;
String params = req.getQueryString() ;
if( params != null )
url += "?" + params ;
%>
<a href="#" onclick="javascript:locati
Sorry:
<%
Str s =new Str();
// Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.
String url = request.getRequestURL() ;
// Returns the query string that is contained in the request URL after the path. This method returns null if the URL does not have a query string.
String params = request.getQueryString() ;
if( params != null )
url += "?" + params ;
%>
<a href="#" onclick="javascript:locati on.href='< %=s.Replac e( url, "-1", "-11" ) %>'">clickhere</a>
<%
Str s =new Str();
// Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.
String url = request.getRequestURL() ;
// Returns the query string that is contained in the request URL after the path. This method returns null if the URL does not have a query string.
String params = request.getQueryString() ;
if( params != null )
url += "?" + params ;
%>
<a href="#" onclick="javascript:locati
ASKER
hi
i tried your comment.
i am using websphere commerce .
out put of your comment is
url = http://abc.com/webapp/wcs/stores/Storename/StoreCatalogDisplay.jsp?param1=10001¶m2=10001¶m3=-11
where as url in the address bar is below
http://abc.com/webapp/wcs/stores/servlet/StoreCatalogDisplay?param1=100¶m2=211¶m3=-1
i tried your comment.
i am using websphere commerce .
out put of your comment is
url = http://abc.com/webapp/wcs/stores/Storename/StoreCatalogDisplay.jsp?param1=10001¶m2=10001¶m3=-11
where as url in the address bar is below
http://abc.com/webapp/wcs/stores/servlet/StoreCatalogDisplay?param1=100¶m2=211¶m3=-1
bizzare!
What does:
<%
Str s =new Str();
// Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.
String url = request.getRequestURL() ;
// Returns the query string that is contained in the request URL after the path. This method returns null if the URL does not have a query string.
String params = request.getQueryString() ;
if( params != null )
{
url += "?" + params ;
out.println( "Query: " + params + "<br>" ) ;
}
// debug the parameters in this request:
for( java.util.Enumeration e = request.getParameterNames( ) ; e.hasMoreElements() ; )
{
String param = (String)e.nextElement() ;
out.print( param + "=" + request.getParameter( param ) + "<br>" ) ;
}
%>
show? That should list the parameters in the request...
What does:
<%
Str s =new Str();
// Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.
String url = request.getRequestURL() ;
// Returns the query string that is contained in the request URL after the path. This method returns null if the URL does not have a query string.
String params = request.getQueryString() ;
if( params != null )
{
url += "?" + params ;
out.println( "Query: " + params + "<br>" ) ;
}
// debug the parameters in this request:
for( java.util.Enumeration e = request.getParameterNames(
{
String param = (String)e.nextElement() ;
out.print( param + "=" + request.getParameter( param ) + "<br>" ) ;
}
%>
show? That should list the parameters in the request...
And why don't you just do (rather than using your Str class):
<%
Str s =new Str();
// Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.
String url = request.getRequestURL() ;
// Returns the query string that is contained in the request URL after the path. This method returns null if the URL does not have a query string.
String params = request.getQueryString() ;
if( params != null )
url += "?" + params ;
url = url.replaceAll( "-1", "-11" ) ;
%>
<a href="#" onclick="javascript:locati on.href='< %= url %>'">clickhere</a>
<%
Str s =new Str();
// Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.
String url = request.getRequestURL() ;
// Returns the query string that is contained in the request URL after the path. This method returns null if the URL does not have a query string.
String params = request.getQueryString() ;
if( params != null )
url += "?" + params ;
url = url.replaceAll( "-1", "-11" ) ;
%>
<a href="#" onclick="javascript:locati
ASKER
hi,
The problem is not in the params
just observe the difference between two outputs
(StoreCatalogDisplay.jsp and StoreCatalogDisplay)
url = http://abc.com/webapp/wcs/stores/Storename/StoreCatalogDisplay.jsp?param1=10001¶m2=10001¶m3=-11
StoreCatalogDisplay.jsp
where as url in the address bar is below
http://abc.com/webapp/wcs/stores/servlet/StoreCatalogDisplay?param1=100¶m2=211¶m3=-1
StoreCatalogDisplay without jsp extension.
in websphre commerece jsp page cannot be accessed directly.
we have to use viewreg or urlreg that embeds the jsp.
The problem is not in the params
just observe the difference between two outputs
(StoreCatalogDisplay.jsp and StoreCatalogDisplay)
url = http://abc.com/webapp/wcs/stores/Storename/StoreCatalogDisplay.jsp?param1=10001¶m2=10001¶m3=-11
StoreCatalogDisplay.jsp
where as url in the address bar is below
http://abc.com/webapp/wcs/stores/servlet/StoreCatalogDisplay?param1=100¶m2=211¶m3=-1
StoreCatalogDisplay without jsp extension.
in websphre commerece jsp page cannot be accessed directly.
we have to use viewreg or urlreg that embeds the jsp.
Ahhhhh!
WHat does:
request.getRequestURI() ;
return? Does that omit the ".jsp" bit?
WHat does:
request.getRequestURI() ;
return? Does that omit the ".jsp" bit?
ASKER
hi,
output of request.getRequestURI()
is
/webapp/wcs/stores/Storena me/StoreCa talogDispl ay.jsp
output of request.getRequestURI()
is
/webapp/wcs/stores/Storena
right, so it looks like you're gonna need a javascript solution then...
<script type="text/javascript" language="javascript">
function coolReplace( str, a, b )
{
while( str.indexOf( a ) > -1 )
str = str.replace( a, b ) ;
return str ;
}
</script>
then, do:
<a href="#" onclick="javascript:locati on.href=co olReplace( location.href, "-1", "-11" )">clickhere</a>
<script type="text/javascript" language="javascript">
function coolReplace( str, a, b )
{
while( str.indexOf( a ) > -1 )
str = str.replace( a, b ) ;
return str ;
}
</script>
then, do:
<a href="#" onclick="javascript:locati
sorry:
<a href="#" onclick="javascript:locati on.href=co olReplace( location.href, '-1', '-11' )">clickhere</a>
<a href="#" onclick="javascript:locati
ASKER
hi
i tried the javascript solution i think its going to infinete loop.
i tried the javascript solution i think its going to infinete loop.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
the other one was an infinite loop, as it searched for the 1st -1, and replaced it with -11, then searched again, and found the same bit of text (the "-1" bit of "-11")
Doh!
:-(
Doh!
:-(
THat new example uses the Regular Expression bit of javascript
basically, it sets up a regular expression that matches "a" (the first parameter to the function), and the "g" bit means "apply to all matches, not just the first one"
Then we call replace
basically, it sets up a regular expression that matches "a" (the first parameter to the function), and the "g" bit means "apply to all matches, not just the first one"
Then we call replace
ASKER
hi timyates,
Thats a great solution.
now i will use two links
<a href="#" onclick="javascript:locati
<a href="#" onclick="javascript:locati
i think the solution is working fine.Let me test everything and then i will accept ur comment.
location.href uses will be same window or different window?
> location.href uses will be same window or different window?
Not sure what you mean? You want the link to open in a new window?
<a href="#" onclick="javascript:locati on.href=co olReplace( location.href, '-11', '-1' )" target="_blank">revereit</ a>
Not sure what you mean? You want the link to open in a new window?
<a href="#" onclick="javascript:locati
ASKER
Excellent solution.
Thanks a million.
sorry for delay in accepting the answer.
Thanks a million.
sorry for delay in accepting the answer.
No worries :-)
Glad we got it sorted eventually :-)
Good luck!!
Tim
Glad we got it sorted eventually :-)
Good luck!!
Tim