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).
Related
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
I have some html data like
<div class="bs-example">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">ABC</h3>
</div>
<div class="panel-body">
<div class="slimScroller" style="height:280px; position: relative;" data-rail-visible="1" data-always-visible="1">
<strong>Name:</strong>
<br />
<strong>ID No:</strong> XXXXX<br />
<strong>Status:</strong> ACTIVE<br />
<strong>Class:</strong> 5<br />
<strong>Category:</strong> A<br />
<strong>Marks:</strong> 500<br />
</div>
</div>
</div>
</div>
I want output as (multiple students data):
Name: ABC
ID No.: XXXXX
Status: Active
Class: 5
Category: A
Marks: 500
How to get this data using jsoup or any other way? Please help.
You can use Element.nextElementSibling() or/and Element.nextSibling() to get the output you need.
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Exam {
public static void main(String[] args) {
String html = "<div class=\"bs-example\">" +
" <div class=\"panel panel-primary\">" +
" <div class=\"panel-heading\">" +
" <h3 class=\"panel-title\">ABC</h3>" +
" </div>" +
" <div class=\"panel-body\">" +
" <div class=\"slimScroller\" style=\"height:280px; position: relative;\" data-rail-visible=\"1\" data-always-visible=\"1\">" +
" <strong>Name:</strong>" +
" <br />" +
" <strong>ID No:</strong> XXXXX<br />" +
" <strong>Status:</strong> ACTIVE<br />" +
" <strong>Class:</strong> 5<br />" +
" <strong>Category:</strong> A<br />" +
" <strong>Marks:</strong> 500<br />" +
" </div>" +
" </div>" +
" </div>" +
"</div>";
Document doc = Jsoup.parse(html);
Elements eles = doc.select("div.slimScroller strong");
for(Element e :eles)
System.out.println(e.text() +
( e.nextElementSibling().tagName().equals("a")?
e.nextElementSibling().attr("href").replace("https://", ""):
e.nextSibling().toString()));
}
}
The following code should provide the output specified based off your comment describing how your a tags are:
private static void printStudentInfo(Document document){
Elements students = document.select("div.slimScroller strong");
for(Element student : students){
System.out.print(student.text());
System.out.println(student.nextElementSibling().tagName().equals("a") ?
student.nextElementSibling().text() : student.nextSibling().toString());
}
}
here's an html code. I want to print "Color:" and various color options present. And somehow I want it by using "select name=att1" that means by name tag of select.
<div class="box-body">
<div id="attributeInputs" class="attribute-inputs" data-defcolor="Palm">
<div class="row thinpad-top att1row">
<div class="small-24 columns">
<label for="att1_BA0FEDC6-8BF1-11E4-B816-87E377679EE2">Color:</label>
</div>
<div class="small-24 columns">
<select name="att1" id="att1_BA0FEDC6-8BF1-11E4-B816-87E377679EE2">
<option value="">Please Select Color</option>
<option value="Black">Black</option>
<option value="Palm">Palm</option>
</select>
</div>
I've tried so many jsoup tags. But I'm not able to get required output
I want output something like this:
Please Select Color:
Black
Palm
please help
This code will extract the elements inside the select tag and option tag
String html="<div class=\"box-body\">\n" +
"\n" +
" <div id=\"attributeInputs\" class=\"attribute-inputs\" data-defcolor=\"Palm\">\n" +
"\n" +
" <div class=\"row thinpad-top att1row\">\n" +
" <div class=\"small-24 columns\">\n" +
" <label for=\"att1_BA0FEDC6-8BF1-11E4-B816-87E377679EE2\">Color:</label>\n" +
" </div>\n" +
" <div class=\"small-24 columns\">\n" +
" <select name=\"att1\" id=\"att1_BA0FEDC6-8BF1-11E4-B816-87E377679EE2\">\n" +
" <option value=\"\">Please Select Color</option>\n" +
" <option value=\"Black\">Black</option>\n" +
" <option value=\"Palm\">Palm</option>\n" +
" </select>\n" +
" </div>";
Document doc = Jsoup.parse(html);
Elements links = doc.select("select option");
for (Element link : links) {
String linkText = link.text();
System.out.println(linkText);
}
I'm making a mock up of an online store for school. I'm storing my products in an SQL Database. I want to display all products in the DB with buttons next to them that will send their ID to the cart_servlet. In order to display the products I'm generating a table like so:
public static String getInventory(){
String result = "<table>";
for ( Product p : DAO_Product.getProducts() ) {
result = result + "<tr>"
+ "<td>" + p.getName() + "</td>"
+ "<td><img src=\"resources/images/" + p.getImage() + "\" alt=\"Duke waving his hand\"></td>"
+ "<td>" + p.getDollars() + "</td>"
+ "<td>" + p.getPennies() + "</td>"
+ "<td>" + p.getStock() + "</td>"
+ "<td>" + p.getDescription() + "</td>"
//creates a form consisting of one button and a hidden value
//clicking the button should submit the corresponding hidden value
+ "<td><form action=\"${pageContext.request.contextPath}/cart_servlet\" method=\"post\">"
+ "<input type=\"hidden\" name=\"product\" value=\"" + p.getId() + "\" />"
+ "<input type=\"submit\" name=\"submit\" value=\"Add to Cart\" />"
+ "</form>"
//End Form
+ "</tr>";
}
}
This creates the correct table as I envisioned and the HTML it generates looks correct:
<table><tr><td>Caverna</td>
<td><img src="resources/images/caverna.jpg" alt="sampleImage"></td>
<td>112</td><td>12</td><td>34</td>
<td> you are the bearded leader of a small dwarf family which lives in a little cave in the mountains. Together, you</td>
<td><form action ="${pageContext.request.contextPath}/cart_servlet" method="post">
<input type="hidden" name="product" value="4" />
<input type="submit" name="submit" value="Add to Cart" />
</form>
However when I click the button I am presented with: 'HTTP Status 404 - Not Found' description: 'The requested resource is not available'
The Servlet I am requesting is my 'CartServlet':
#WebServlet( "/cart_servlet" )
public class CartServlet extends HttpServlet{
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String result;
Status status = new Status();
req.setAttribute( "status", status );
//RequestDispatcher view = req.getRequestDispatcher( "browseProducts.jsp" );
if ( req.getParameter( "submit" ) != null ) {
}
//view.forward( req, resp );
}
}
I've commenting out everything in my 'CartServlet' and I am sure that it probably cant get the context somehow or find the class but I am unsure about how to fix this.
It turns out I was approaching this all wrong. Instead I should have built the table within my JSP using JSTL. With the following code I was able to delete 'getInventory()':
The First step is to add the following line to the top of your JSP:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Note Ensure you have downloaded the JSTL library and added it to your project. You can find it here.
The Second step is to declare any variables you would need. In this case I require an ArrayList of 'Products':
<%
ArrayList<Product> products = DAO_Product.getProducts();
//Set an attribute to reference our Arraylist
pageContext.setAttribute( "products", products );
%>
Now we are ready to build our table. The table will display information about each product and have a button in the last column that will call 'cart_servlet'. We are going to use the 'forEach' from 'JSTL':
<table border="1" cellpadding="5" >
<tr>
<th>Name</th>
<th>Image</th>
<th>Price</th>
<th>Description</th>
<th>Stock</th>
<th>Add to Cart?</th>
</tr>
<c:forEach var="p" items="${products}" >
<tr>
<td>${p.name}</td>
<td><img src="resources/images/${p.getImage()}" alt="Cool Pic"></td>
<td>$${p.dollars}.${p.pennies}</td>
<td>${p.description}</td>
<td>${p.stock}</td>
<td>
<form action="${pageContext.request.contextPath}/cart_servlet" method="post">
<input type="hidden" name="productID" value="${p.id}">
<input type="submit" name="addToCart" value="Add to Cart">
</form>
</td>
</tr>
</c:forEach>
</table>
Now I am learning building a simple bbs system where people post articles in a textarea.
The problem is when I display strings, the format is lost.
I know if you output a string with <s:property/>, it is by default html-escaped(I guess Struts seems use apache commons.lang.StringEscapeUtils). But it doesn't escape ' ' to . Also, it doesn't translate '/n' to <br/>.
What should I do?
By the way, how to implement a rich text editor(Like the text editor used in stackoverflow)?
I suggest you for such things you better don't rely on the Text-area and better use some kind of Text editor.
There are lots of java-script text editor out there and you need not to anything extra to integrate them to your existing S2 application.
Few options are
Tinymce
CKEditor
Because doing so isn't escaping, it's transforming.
If you want to change spaces to and \n to <br/> you'll need to do that yourself.
Edit HTML in textarea, jQ Shows Result (not Struts2 req in OP)
Sorry, this is a jQuery implementation vs Struts2 requested in OP O_o. Hope it gives Struts2 users a starting point?
This has a few control buttons to format supported by shortcut key combos in a textarea and a div where the changes are displayed dynamically.
<head>
<title>Text Editor (test)</title>
<script type="text/javascript" src="../lib/js/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
updCont();
var ht = window.innerHeight - $("#dvInput").css("height").replace(/[^.0-9]/g, '') - 30;
$("#dvCont").css("height", ht + "px");
$("#txtCont").keydown(function(e){
var curKey = e.which ? e.which : e.key;
if(e.ctrlKey && e.shiftKey){
if([56,190,54].indexOf(curKey)>-1)
e.preventDefault ? e.preventDefault() : (e.returnValue = false);
if(curKey == 56) fmt("bull");// *
else if(curKey == 190) fmt("indnt");// >
else if(curKey == 54) fmt("para");// ^
}else if(e.ctrlKey){
if([66,73,13].indexOf(curKey)>-1)
e.preventDefault ? e.preventDefault() : (e.returnValue = false);
if(curKey == 66) fmt("bld");// B
else if(curKey == 73) fmt("ital");// I
else if(curKey == 13) fmt("crlf");// [enter]
}
updCont();
});
$("#inp").keydown(function(e){alert(e.which)})
});
function updCont(){
$("#dvCont").html($("#txtCont").val());
}
function fmt(inAct){
var oTxt = $("#txtCont");
var s = oTxt[0].selectionStart;
var e = oTxt[0].selectionEnd;
var raVal = [oTxt.val().substring(0, s), oTxt.val().substring(s, e), oTxt.val().substring(e), s, e];
var ofstStart = 0;
var ofstEnd = 0;
if(inAct=="bld"){
oTxt.val(raVal[0] + "<b>" + raVal[1] + "</b>" + raVal[2]);
//if you don't want text selected, add raVal[1].length to ofstStart
ofstStart = 3;
//uncomment if you want cursor after closing tag (and note also ofstStart same point to unselect)
//ofstEnd = 4;
}else if(inAct=="ital"){
oTxt.val(raVal[0] + "<i>" + raVal[1] + "</i>" + raVal[2]);
ofstStart = 3;
//ofstEnd = 4;
}else if(inAct=="indnt"){
oTxt.val(raVal[0] + "<div class='dnt'>" + raVal[1] + "</div>" + raVal[2]);
ofstStart = 17;
//ofstEnd = 5;
}else if(inAct=="bull"){
oTxt.val(raVal[0] + "<br /> • " + raVal[1] + raVal[2]);
ofstStart = 20;
}else if(inAct=="para"){
oTxt.val(raVal[0] + "<p>" + raVal[1] + "</p>" + raVal[2]);
ofstStart = 3;
//ofstEnd = 4;
}else if(inAct=="href"){
oTxt.val(raVal[0] + "<a href='' class='' title=''>" + raVal[1] + "</a>" + raVal[2]);
ofstStart = 29;
//ofstEnd = 4;
}else if(inAct=="crlf"){
oTxt.val(raVal[0] + "<br />" + raVal[1] + raVal[2]);
ofstEnd = 5;
}
updCont();
oTxt[0].selectionStart = s + ofstStart;
oTxt[0].selectionEnd = s + ofstStart + raVal[1].length;
oTxt[0].focus();
}
</script>
<style>
.dnt{margin-left:40px;}
.styleBtn{width:20px;margin:2px 10px;border:1px solid black; padding:1px 7px;background-color:#EEE;float:left;text-align:center;}
</style>
</head><body>
<div id="dvInput">
<div style="border:1px solid black;border-radius:5px;background-color:silver;font-weight:bold;padding:2px 5px;float:left;">
<div style="float:left;margin:2px 20px;">Text Input:</div>
<span onclick="fmt('bld');" title="CTRL+B: Bold selected text">
<div class="styleBtn"><b>B</b></div>
</span>
<span onclick="fmt('ital');" title="CTRL+I: Italicize selected text">
<div class="styleBtn"><i>I</i></div>
</span>
<span onclick="fmt('indnt');" title="CTRL+Shift+>: <div> Indent a selected block of text\n(No remove shortcut.\nManually delete <div></div> to undo/outdent)">
<div class="styleBtn">></div>
</span>
<!-- <span onclick="fmt('outdnt');"><</span> -->
<span onclick="fmt('bull');" title="CTRL+Shift+*: Bullet (and new line)">
<div class="styleBtn">•</div>
</span>
<span onclick="fmt('para');" title="CTRL+Shift+^: Paragraph <p> around highlighted/selected text">
<div class="styleBtn">¶</div>
</span>
<span onclick="fmt('crlf');" title="CTRL+Enter: New Line <br>">
<div class="styleBtn">↲</div>
</span>
<span onclick="fmt('href');" title="CTRL+Shift+A: <a href>">
<div class="styleBtn"><a></div>
</span>
<!-- <input id="inp" size="2">-enter char, get ascii number -->
</div>
<textarea id="txtCont" rows="10" style="width:100%;">
</textarea><br />
</div>
Display:
<div id="dvCont" style="border:1px solid black;border-radius:5px;overflow-y:auto;"></div>
</body>
</html>
The StackOverflow editor has some clever formatting that is beyond the scope of what I wanted to implement in my display.