ASP.NET page calls Java Applet and fields lost focus - java

I have a asp.Net application that make calls to a java applet.
Requests via javascript work as expected without problems. however
there is a mechanism that searches the time intervals in the applet.
(using setInterval)
And when this mechanism is triggered the page "loses" focus.
For example, if I'm with the focus on a text field, and the call occurs, the focus leaves the field.
I tried to go the other way, the applet calling the page, but the result was the same.
I also tried putting this control inside an UpdatePanel, to try to isolate the callback but it still fails.
Also tried putting inside an iframe, but it still fails.
Here is the code that makes the request
function initTimerDevices() {
setInterval(getDeviceStatus, 5000);
}
function getDeviceStatus(name) {
document.applet.getDeviceStatus("ASDF");
}
$(document).ready(function () {
initTimerDevices();
});
And here is the ASP
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<applet name="Myapplet" code=com.xpto.applet.class
archive="applet.jar"
width="0.5" height="0.5">
</applet>
<ul id="ulDevices">
</ul>
</ContentTemplate>
</asp:UpdatePanel>
Is there any way to make the call to the applet asynchronously? Or other form that causes this loss of focus does not exist.

One problem here is in the use of the ready() function which:
..will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.
That will probably be some time before:
The JVM is loaded.
The applet Jars or classes are loaded.
The applet itself has gone through the init() and start() methods.
Better to initiate this code from within the applet.

Related

How to use java applet in reactjs

I'm trying to integrate a specific java applet in my reactjs application.
More over, I would like to make it run in a Dialog (using matelial UI).
I have a render function which works and when I click on a specific button, the Dialog is shown. The code concerning the applet part in the Dialog of my return() function is shown here : 
<Dialog
ref="dialogAction"
title={this.state.dialogTitle}
actions={customAction}
actionFocus="submit"
modal={this.state.modal}
autoDetectWindowHeight={true}
autoScrollBodyContent={true}
contentStyle={styles.dialogMarvin}>
<applet
type="application/x-java-applet;version=1.6"
width="540"
height="480"
codebase="./marvin"
archive="appletlaunch.jar"
code="chemaxon/marvin/applet/JMSketchLaunch"
legacy_lifecycle={true}
java-vm-args="-Djnlp.packEnabled=true -Xmx512m"
codebase_lookup={false}>
</applet>
</Dialog>
When I run this code, the Dialog is shown as expected and the first time, firefox ask me if I want to activate java (telling me that it's working so far). Watch the following picture: applet ready to be loaded, but allowance asked
But once java allowed, nothing is displayed (only blank, look the following image: once allowance granted). If I check the code in the brower, some properties are missing. Instead of the original embeded code I have this one (missing archive, codebase and some other) :
<applet
type="application/x-java-applet;version=1.6"
data-reactid=".0.$=12:0.0.2.2.0.$=10.0.1.0"
height="480"
width="540">
</applet>
Some inforamtions are displayed but I don't understand them yet.
Have you arleady try to emmbed an applet in your reactjs ? Have you some tips to give ? I'm doing something wrong ?
React is picky when it comes to allowing random attribute names. So instead of injecting applet by using JSX, just set the innerHTML of the component instead. To do this, use React's dangerouslySetInnerHTML:
var dangerousMarkup = {
__html: '<applet.... ' // the whole markup string you want to inject
};
<div dangerouslySetInnerHTML={dangerousMarkup}></div>
This is basically the same as setting domNode.innerHTML = '<applet...' with vanilla javascript, and React just let's the raw markup pass through without modification.
Docs: https://facebook.github.io/react/tips/dangerously-set-inner-html.html

call a java method when Click on a html button without using javascript

i work on JSP and i want to call a java method(Function) on Click on a html button without using<script></script>.how?
i try to write this code:
<button onclick="<%po.killThread();%>">
<font size="4">Kill</font>
</button>
but it doesn't work... so please help me.
thanks
You're misunderstanding how server-side programming works. When you load that page, the webserver will get to the line <button onclick="<%po.killThread();%>"> and will immediately parse and execute the JSP snippet, in your case po.killThread(), and replace everything between the <% and %> with the return value of that method, if any. And all these happens on server side, before client receives any thing. (Note that this will only happen if that page is not already been loaded and compiled into a Servlet by the server.)
Thus, the HTML that client receives, will be something like, <button onclick="some return value or nothing">, which means that nothing will happen when you press the button. If you want to execute further JSP commands on the button press you will need to make a new request to the server - for example, by redirecting the page.
This will call the function killThread when you open the website.
Try to redirect to another jsp which calls the function.
this will not run at all because after the jsp page is compiled it will return the po.killThread() value but will not call this method
You can see this by viewing the page source
JSP is a server-side technology. Did I say server-side?
In order to understand how JSP works and to clear any misconception, JavaRanch Journal (Vol. 4, No. 2): The Secret Life of JavaServer Pages is a very good read.
An excerpt from the same,
JSP is a templating technology best-suited to the delivery of dynamic text documents in a format that is white-space agnostic.
Template text within a JSP page (which is anything that is not a dynamic element), to include all white-space and line terminators, becomes part of the final document.
All dynamic elements in a JSP are interpreted on the server and once the document is sent to the client, no further dynamic interaction is possible (short of requesting the same or another document).
If you are using JSPs, then to perform some method calles, you will have to write a servlet and then call the method in doPost or doGet method of servlet.
On the other hand, if you want to make things simpler, use JSF framework which will help you achieve your objective as JSF supports event handling.

How do I inject another JSP page into a <div> when clicking a link?

I have two different divisions in a JSP page. One contains a menu of links, when clicked the div2 (id-content) loads different pages accordingly. I am doing something like -
<div id="menu">
<ul class="navbar">
<li><a name="login" href="Login.jsp" onclick="changeContent()">Login</a>
</li></div>
and in the script I have something as -
<script language="JavaScript">
function changeContent() {
document.getElementById('content').load('Login.jsp');
}
</script>
I also tried -
document.getElementById('content').innerHTML=
"<jsp:include page="Login.jsp">";
None of the ways worked. Please suggest how should I
Try jquery..
function changeContent() {
$('#content').load('Login.jsp');
}
The solution is to use Ajax, which will asynchronously retrieve your page content that can be pasted in with the innerHTML method. See my answer to a similar question of how an Ajax call works and some introductory links.
As to why your examples in your answer don't work, in the first case there is no load() method on an Element object (unless you've defined one yourself and not shown it). In the second example, as one of the question comments says, there is probably something causing a syntax error in the javascript.
As an FYI, when there is a syntax error in some javascript in a web page, the current expression being parsed and the rest of the <script></script> block will be ignored. Since this is inside a function declaration, that function will never get defined.
For instance, an embedded quote in the included page will end the string for the innerHTML assignment. Then the javascript parser will try to parse the remainder of the HTML causing a syntax error as the HTML will not be valid javascript.
We use jquery. Add a click event handler to the anchor elements. In the click handler call $('#content').load(your_url);. You might want to use the load(url, function() { ...}) version. More info here http://api.jquery.com/load/
Your initial page comes down from the server. It's displayed by the browser. When you click on a link (or a button) in the browser, you want to fill the second div with new HTML. This is is a perfect job for an AJAX request. What the AJAX object in the browser does, is to send a POST (or whatever) string to the server. And then the Ajax object receives the HTML response back from the server. And then you can display that response data which the AJAX object contains, anywhere you want.

can ajax call other ajax page?

I have index page,which contain div section.i want to call different pages into this div section on the basis of her-link click,this is done. when calling pages that may also using ajax call,in this case ajax call not work for this page.please tell me it is possible to call ajax inside an ajax.if yes then please help me.
Update Edit:
I have created a gist where i have include all used pages,script https://gist.github.com/2786811.page service_status called the view.jsp using ajax call every 5 second,on hand view.jsp interact with database and prepare view.when we do ajax call from index.jsp, service_status called but it doesn't called view.jsp file.
Thanks
You can make ajax call on page load using $(document).ready(function () {...}); in target page you are calling.
If you are trying to dump a <script> tag with innerHTML, it won't work.
However, you could (as an example) return a JSON object with two properties:
{
html: "All your <b>HTML</b> here",
js: "alert('JavaScript to be run');"
}
Then, use the html property to put content on the page, and then eval the js property.
This isn't the best solution, since it uses the evil eval, but unless you want to rewrite your whole basic structure to use callbacks properly then it's probably your best bet.

JSP - JS - Java interaction

I am trying to call a method in a Java applet from JS. I am a little bit confused about how to arrange my workflow. I have a jsp (pending.jsp) class which includes a javascript (pending.js). pending.jsp is just a container to hold pending.js. pending.js' purpose is to enable users to cancel report creation.
pending.jsp:
<div id="pending-reports-container" class="pending-reports-container">
<div id="pending-reports">
</div>
</div>
pending.js:
function update_pending_reports(response_data)
{ bla }
function delete_report(report_id)
{ bla }
function request_pending_reports()
{ bla }
function start_pending_reports() {
request_pending_reports();
setInterval("request_pending_reports()", 5000);
}
Because cancelling reports sometimes is not that efficient I want to use Java to cancel requests that goes to the postgres (pending.js does not kill the process that works at postgres). I created my Java class but I don't know where to add it. I know I can make use of tag, but when I tried to add this tag to my jsp file like this:
<div id="pending-reports-container" class="pending-reports-container">
<div id="pending-reports">
<applet id="LS" width=1 height=1 code ="module/LicenseCheckService.class" codebase="."/>
</div>
</div>
I can not reach it from my pending.js with this code:
getJS = document.getElementById("LS");
Is there something wrong with my code or am I wrong at this structure in the first place?
See the Invoking Applet Methods From JavaScript Code Oracle docs.
Nutshell version is that the JavaScript refers to the applet object by the applet ID, and can call methods on it.
appletId.anAppletMethod();
var anAppletClass = appletId.getAnAppletClass();
anAppletClass.methodCalled("with a JavaScript string parameter");
// Etc.
See Dave Newton's answer.
Alternatively, you can also check the HTML field or call a javascript method from Applet. You can construct the interaction to be actively controlled by the applet.
Link here : Java Applet To JS Link

Categories

Resources