Here is my code:
Elements parents = doc.select("input[value]");
for (Element parent : parents) {
System.out.println(
parent.attr("value")
.replace("X70xAkOaaAeWGxNgWnTJolmy6/FFoFaBD47IzyBYWf4=", "Ranjan")
.replace("17572418", "17572418123")
.replace("200", "199")
.replace("2018-09-13T16:28:28Z", "2018-09--5T16:28:28Z")
.replace("2018-09-17", "2018-09-25")
);
}
But when I print System.out.println(doc); it is printing the same old value instead I should get the modified one. How to modify the specific value which are under input tag?
EDIT:
I have the following HTML:
<input type="hidden" name="sessionValidity" value="2018-09-13T16:28:28Z">
<input type="hidden" name="shipBeforeDate" value="2018-09-17">
<input type="hidden" name="merchantReturnData" value="">
<input type="hidden" name="shopperLocale" value="en_GB">
<input type="hidden" name="skinCode" value="Ce0xkMuQ">
<input type="hidden" name="merchantSig" value="X70xAkOaaAeWGxNgWnTJolmy6/FFoFaBD47IzyBYWf4=">
I am not familiar with Jsoup but it seems like you do not change the value of the attributes. Element.attr(String s) returns a String. I guess you meant to use public Element attr​(String attributeKey, String attributeValue).
Then you use public String replace(CharSequence target, CharSequence replacement) which does not modify the String itself (String is immutable, replace returns a new String)
I think you want to do this way instead:
private static String html =
"<input type=\"hidden\" name=\"sessionValidity\" value=\"2018-09-13T16:28:28Z\">\n" +
"<input type=\"hidden\" name=\"shipBeforeDate\" value=\"2018-09-17\"> \n" +
"<input type=\"hidden\" name=\"merchantReturnData\" value=\"\"> \n" +
"<input type=\"hidden\" name=\"shopperLocale\" value=\"en_GB\"> \n" +
"<input type=\"hidden\" name=\"skinCode\" value=\"Ce0xkMuQ\"> \n" +
"<input type=\"hidden\" name=\"merchantSig\" value=\"X70xAkOaaAeWGxNgWnTJolmy6/FFoFaBD47IzyBYWf4=\">";
public static void main(String[] args) {
Document doc = Jsoup.parse(html);
doc.select("input[name$=merchantSig]").attr("value", "Ranjan");
// and the other ones
System.out.println(doc.html());
}
that prints out
<html>
<head></head>
<body>
<input type="hidden" name="sessionValidity" value="2018-09-13T16:28:28Z">
<input type="hidden" name="shipBeforeDate" value="2018-09-17">
<input type="hidden" name="merchantReturnData" value="">
<input type="hidden" name="shopperLocale" value="en_GB">
<input type="hidden" name="skinCode" value="Ce0xkMuQ">
<input type="hidden" name="merchantSig" value="Ranjan">
</body>
</html>
You can see that the merchantSig value has been modified
Related
Suppose I have 3 text boxes on a page defined as below.
<input id="input" type="search" autocomplete="off" role="combobox" placeholder="Search">
<input id="input" type="open" autocomplete="off" role="combobox" placeholder="Open">
<input id="input" type="close" autocomplete="off" role="combobox" placeholder="Close">
I will pass the value 'Open' as a parameter to JSoup and JSoup should return me the data as below (which are the details of the middle textbox).
<input id="input" type="open" autocomplete="off" role="combobox" placeholder="Open">
Can JSoup do this?
Thank You
-Anoop
You need to select tag by attribute:
document.select("input[placeholder=Open]")
UPD:
To select tag has any attribute with value "Open" you need to iterate over all attribute values:
List<Element> result = document.select("input").stream()
.filter(input -> hasAttrValue(input, "Open"))
.collect(Collectors.toList());
hasAttrValue method:
private boolean hasAttrValue(Element element, String targetValue) {
for (Attribute attribute : element.attributes()) {
if (targetValue.equals(attribute.getValue())) {
return true;
}
}
return false;
}
Why is my method does not work?
My Java code:
#POST
#Path("/request=PostStage")
#Produces(MediaType.APPLICATION_JSON)
public String getStagePOST(#QueryParam("fn")String fn,
#QueryParam("tn")String tn,
#QueryParam("stat")String stat,
#QueryParam("length")String length,
#QueryParam("lon")String lon,
#QueryParam("lat")String lat,
#QueryParam("crgw")String crgw,
#QueryParam("lane")String lane) throws SQLException{
return "Lat: " + lat + " lon: " + lon + " crgw: " + crgw;
}
My HTML code:
<form action="http://localhost:9090/services/stage/request=PostStage" method="POST">
<p>Localization:</p>
<p> fn : <input name="fn" /></p>
<p> tn : <input name="tn" /></p>
<p>stat : <input name="stat" /></p>
<p>length : <input name="length" /></p>
<p>Geoposition:</p>
<p>lon : <input name="lon" /></p>
<p>lat : <input name="lat" /></p>
<P> Other:</P>
<p>crgw : <input name = "crgw" /></p>
<p> lane : <input name="lane" /></p>
<input type="submit" value="Searchh" />
</form>
I give examples of parameters in a html page: lon - 12, lat - 12 etc.
As a result, I get:
Lat: null lon: null crgw: null
Why?
I can not find the problem :(
Very thanks for all answers .
Shouldn't there be types on the input-tags, like <input name="lon" type="text" />?
Also verify that the POST from the form really contains the parameters, the browser debugging IDE in both Chrome and Firefox is probably able to see how the POST-request looks.
Working! My mistake was to use "#QueryParam" instead "#FormParam".
I am trying to overwrite a text file which contains 6 lines of data by inserting 6 different lines of data.
Here is my form.
<form method="POST" action="write.jsp">
Name: <input type="text" name="name1" size="20" /><br>
Price: <input type="text" name="price" size="20" /><br>
Due time: <input type="text" name="DueTime" size="20" /><br>
Location: <input type="text" name="Location" size="20" /><br>
Photo Url: <input type="text" name="url" size="20" /><br>
Description: <input type="text" name="desc" size="20" /><br>
<input type="submit" name="submit" onclick="window.close()"/>
</form>
After user clicks on submit, it will go to write.jsp, which is shown below.
String name = request.getParameter("name1");
String price = request.getParameter("price");
String DueTime = request.getParameter("DueTime");
String Location = request.getParameter("Location");
String url = request.getParameter("url");
String desc = request.getParameter("desc");
String paths="somepath/hotel1.txt";
FileWriter filewriter = new FileWriter(paths, true);
filewriter.write(name + System.getProperty("line.separator"));
filewriter.write(price + System.getProperty("line.separator"));
filewriter.write(DueTime + System.getProperty("line.separator"));
filewriter.write(Location + System.getProperty("line.separator"));
filewriter.write(url + System.getProperty("line.separator"));
filewriter.write(desc + System.getProperty("line.separator"));
filewriter.close();
But somehow it does work, can anyone help me to fix this?
Here's the scenario. The user fill a form with a certain number of parameters in an html page, then I need to invoke my applet with this specific parameters, possibly within the same page. What's the easiest way to perform this pass?
I'm using the runApplet function, but it's not working. No messages in the JS console and in the Java console.
<html>
<head>
<title>Compila i dati</title>
</head>
<body>
<script src=
"http://www.java.com/js/deployJava.js"></script>
<script>
function runApplet(){
var attributes = { id:'anID', code:'Test', width:1, height:1, codebase: '.'} ;
var parameters = {width:'100', height:'100', code:'Test', archive: 'applet.jar, xyz.jar, abc.jar',
posX: document.forms["form1"]["posX"].value , posY: document.forms["form1"]["posY"].value , heightSign: '300' , widthSign: '600' ,
PDFUrl: 'http://anurl' ,
type:'application/x-java-applet' , scriptable:'false' } ;
deployJava.runApplet(attributes, parameters, 1.6);
}
</script>
<form name = "form1" onsubmit="runApplet()">
<div>
<label for="name">Nome:</label>
<input type="text" name="nome" />
</div>
<div>
<label for="mail">Cognome:</label>
<input type="text" name="cognome" />
</div>
<div>
<label for="msg">X:</label>
<input type="text" name="posX" />
</div>
<div>
<label for="msg">Y:</label>
<input type="text" name="posY" />
</div>
<div class="button">
<button type="submit">Invia</button>
</div>
</form>
</body>
I could try to create an dinamic applet contanier via javascript in this way:
$('#idappletv').empty();
$('#idappletv').hide();
var _xmhlcode = " <script> function javafxEmbed() {" +
"dtjava.embed( " +
"{" +
" id: 'myBrApplet'," +
" url : 'pages/applet/myApplet.jnlp'," +
" placeholder : 'javafx-app-placeholder'," +
" width : 890 ," +
" height : 200," +
" jnlp_content : 'bmFtZT0iQXBwbGV0RnhCcm93c2VyIiAvPg0KICA8dXBkYXRlIGNoZWNrPSJhbHdheXMiLz4NCjwvam5scD4NC=='" +
", params: {param1:'" + param1 + "',param2:'" + param2 + "'}" +
"}," +
"{" +
" javafx : '2.2+'" +
"}," +
"{}" +
");" +
"}" +
"dtjava.addOnloadCallback(javafxEmbed); </" + "script> ";
$('#idappletv').append("<div id='javafx-app-placeholder'></div>");
$('#idappletv').append(_xmhlcode);
dtjava.addOnloadCallback(javafxEmbed);
$('#idappletv').show();
Then when the applet starts you can read the parameter using:
#Override
public void start(Stage primaryStage) {
Parameters params = getParameters();
String param1 = params.getNamed().get("param1");
String param2 = params.getNamed().get("param2");
You can read the parameter "param1 and param2" from your html page.
It should be works.
I used JavaFX with java-webstart (http://docs.oracle.com/javafx/2/deployment/deploy_swing_apps.htm).
I have created two jsp pages in which I am able to get the response of my input. Below are my 2 jsp pages. Also I would like to get a response from android java class to the same Index1.jsp.(For eg: If I am sending one string to Index1.jsp I would to get the response as same string).
Index.jsp
<form action="index1.jsp" method="POST">
<input type="checkbox" name="maths" checked="checked" /> Maths
<input type="checkbox" name="physics" /> Physics
<input type="checkbox" name="chemistry" checked="checked" /> Chem
<input type="submit" value="Select Subject" />
</form>
Index1.jsp
<%
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
out.print("<tr><td>" + paramName + "</td>\n");
String paramValue = request.getParameter(paramName);
out.println("<td> " + paramValue + "</td></tr>\n");
}
%>