Jsp : How to pass dynamic variable using a hyperlink - java

I want to pass a (dynamic) variable through a hyperlink.
sname = "my image location";
out.println("hyperlink targeted_url name = sname ");
on targeted_url :
myvalue = request.getParameter("name").toString();
But whenever I tried to output that location, the output show me sname.
Is there any solution? I don't want to use session, cookie or form.
Is it possible within hyperlink?

You're not evaluating sname in your println. This will do that:
out.println("hyperlink targeted_url name = " + sname);
You haven't shown where you are creating your link, but when doing that you need to do the same thing - append the sname variable instead of including it as a string literal.

Related

Need to get value after Domain name in url using java

We are getting url from JSON Response and which we open in in Chrome.The page loads , there is submit button which we click then it redirect to url as :-
https://www.google.com/AB1234
We need the need to retrieve only "AB1234" value from url.
tried following code to get value ="AB1234"
String url = driver.getCurrentUrl();
int index=url.lastIndexOf("/");
String result = url.substring(0,index);
but here getting initial part of url:https://www.google.com/
You need to call substring function with index +1 .
Try below code :
String url = driver.getCurrentUrl();
int index = url.lastIndexOf("/");
String result = url.substring(index + 1);
To parse a URI, it's likely a good idea to use a URI parser.
Given http://example.com/bar
String path = URI.create(driver.getCurrentUrl()).getPath();
will get you '/bar'.
Given http://example.com/bar/mumble the same code gets '/bar/mumble'. It's unclear from your question whether this is what you want. Nevertheless, you should at least start the parse as above.

String Variable with Find Element Commands Java

Hello all the following codes works here it is.
String content = FileUtils.readFileToString(new File("C:\\PayrollSync\\prepayroll.txt"));
String [] Arrayclients = content.split("\n");
// begin for loop
for(String client : Arrayclients) {
PP_OBJ_CycleData.verifysearch(driver);
PP_OBJ_CycleData.searchbox(driver).clear();
PP_OBJ_CycleData.searchbox(driver).sendKeys(client);
PP_OBJ_CycleData.searchbox(driver).sendKeys(Keys.BACK_SPACE);
Thread.sleep(4000);
//WebElement dropdown = driver.findElement(By.xpath(".//*[#title="+client+"]"));
//dropdown.click();
//driver.findElement(By.xpath(".//*[(text(),"+client+"]")).click();
driver.findElement(By.xpath("//*[contains(text(),"+client+")]")).click();;
Thread.sleep(2000);
PP_OBJ_CycleData.practitioner(driver).click();
The Problem:
None for the driver.findElements are working such as:
//WebElement dropdown = driver.findElement(By.xpath(".//*[#title="+client+"]"));
//dropdown.click();
//driver.findElement(By.xpath(".//*[(text(),"+client+"]")).click();
driver.findElement(By.xpath("//*[contains(text(),"+client+")]")).click();;
Thread.sleep(2000);
My failure traces says cannot find element By.xpath("//*[contains(text(),"+client+")]")). However I am waiting for the element to be visible when i go to the next page. My wait is below.
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(20, TimeUnit.SECONDS);
element = driver.findElement(By.id("toolbarQuickSearch"));
wait.until(ExpectedConditions.or(
ExpectedConditions.visibilityOf(element),
ExpectedConditions.elementToBeClickable(element)
));
The problem from what I can see if that after the variable that contains the string is entered into the search field and the string name is visible selenium cant find the string name to click on for example below:
I could put this in and it will click on it all the time:
driver.findElement(By.xpath("//*[text()='midrfrate']"]"));
However if i do something like this it cant find it:
driver.findElement(By.xpath(".//*[(text(),"+client+"]")).click();
driver.findElement(By.xpath("//[contains(text(),"+client+")]")).click();
Please help me I have tried the following with no success:
How to use variables in XPath?
Your xpath there:
driver.findElement(By.xpath("//*[contains(text(),"+client+")]")).click();:
Should contain apostrophes
Text in contains block should contain exact String (you have trailing whitespace in client variable 'midrfrate ').
Final xpath is:
driver.findElement(By.xpath("//*[contains(text(),'"+client.trim()+"')]")).click();
where trim() method removes leading and trailing whitespace from client variable.
The string ".//*[(text(), " + client + "]" is equals to ".//*[(text(), midrfrate]"
You need to put the variable in apostrophes
".//*[contains(text(), '" + client + "']"

How to get values custom metadata properties

I'm trying to get the metadata for an asset in the DAM. However, it seems that the metadata comes back as empty for properties that don't have "dc:" in front of them.
Resource rs = getResourceResolver().getResource(fileReference);
Asset asset = rs.adaptTo(Asset.class);
//this works
title = asset.getMetadataValue("dc:title").toString();
//this does not work.
//I have ensured that "mine.title" is a property and has string value assigned to it.
customTitle = asset.getMetadataValue("mine.title").toString():
//this does not work either
customTitle = asset.getMetadata("mine.title").toString():
Is there a way to get the value from a custom metadata property?
Assets at the end are simple nodes, so to get some property you can do something like this (depending on actual path of variable fileReference):
Resource metadataResource = rs.getChild("jcr:content/metadata");
ValueMap properties = ResourceUtil.getValueMap(metadataResource);
customTitle = properties.get("mine.title", "defaultValue")
"dc:title" comes with a registered namespace "dc" (Dublin Core) whereas "mine.title" does not.
That's the reason of title = asset.getMetadataValue("dc:title").toString(); giving you proper value than customTitle = asset.getMetadataValue("mine.title").toString()
You can attack this problem in many ways.
Change the property name to "dc:myTitle" and retrieve it in the same way you are retrieving "dc:title" [0]
You can retrieve the value of "mine.title" in the way Alex has described.
Resource rs = getResourceResolver().getResource(fileReference + "/jcr:content/metadata");
ValueMap damAssetValueMap = damResource.adaptTo(ValueMap.class);
String shortName = damAssetValueMap.get("shortName", String.class);
Register a new namespace and define options (in your case , its "mine").
A look at "/libs/dam/nodetypes" and "/libs/dam/options/metadata" might be helpful.
[0] Check "/libs/dam/options/metadata"

sending multiple parameters through query string in servlet

I want to send multiple parameters to a servlet named cart using this code.but it isending null.plz help.rs is a reference to resultset here
while(rs.next())
{
String name=rs.getString("name");
int cost=rs.getInt("cost");
out.println("\n");
out.println("Name: "+name);
out.println("\nPrice: "+cost);
out.println("\n<a href=\'cart\'?n=name&&c=cost>Add to Cart</a>");
}
There are several problems with the way the link is constructed.
This should work:
out.println("\n<a href='cart?n=" + name + "&c=" + cost + "'>Add to Cart</a>");
In addition, it may be necessary to URL encode the content of name if it can contain characters with special meaning in an URL.

using hyperlinks in java file

I am trying to add a hyperlink in java file.
TestHyperlink.java
class TestHyperlink.java {
String url = "stackoverflow.com/questions/ask";
String someVariable = "testUrl";
Html entryLink = new Html("<a target=\"_blank\" href=url>someVariable</a>");
}
I am trying to use two string variables url and someVariable but I am not sure how to do it. My hyper link appears as 'someVariable' and on click leads to a broken page.
What I seek is a hyperlink which appears as testUrl and on click leads to a desired url page, stackoverflow.com/questions/ask in this case.
Thanks,
Sony
Java doesn't interpolate variables inside Strings. You need to change to new Html("<a target=\"_blank\" href=\"" + url + "\">" + someVariable + "</a>");

Categories