JSP - JS - Java interaction - java

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

Related

Spring-Thymeleaf How to invoke with EL an action method from a Bean

I am using Spring/SpringMVC 5.x version, with Thymeleaf and Bootstrap on Tomcat server.
I need to ask something that maybe it might look to you very "st#pid" question.
In my html view I have the following button or a link:
<input type="button" .../>
<a .../>
I don't need to submit something, so I just use a simple button, so I think I don't need any form for it (except if I need for this).
In this html view (because of the thymeleaf library I added in the html tag), I need to add somehow,
(but I don't know how), to this button or in the link, an expression of Spring EL or Thymeleaf EL, so I can invoke a method from a
Spring bean, that I passed in the view, via a model which I added in my controller, e.g.:
${myBean.doSomething()
// or
${myBean.doSomething(parameters)
If this is not understandable I can update my question with some code (I believe that Spring developers
understand what I am talking about).
I don't know how to pass this expression. What attribute of button or link tag to use?
I used "action" attribute for the button:
<input type="button" th:action="${myBean.doSomething()".../>
or "href" attribute in the link tag:
<a th:href= "${myBean.getStringUrlAndDoSomething()"/>
Very significant info
When I started my tomcat running the page, the actions in the EL are run successfuly on the load of the page. When I pressed the button or the link nothing happened.
I know that I cannot use "onclick" attribute because there we write JS code.
But I need to run Java Spring code.
Any ideas about solving my problem?
Thanks in advance
I followed the advice of the #M.Deinum, #Wim Deblauwe, and I did not use
a button for this job. Button needs a form to work.
That is why I used a link, where the method from the bean is called like a charm, like
the following snippet:
<div class="blabla">
<div class="blablabla" th:text="|#{change_lang} EN/GR:|"></div>
<a class="bla" th:href="${localeService.switchLocale()}">
<div th:class="|${localeService.loadCss()}_blabla|"></div>
</a>
<span th:text="${#locale.getLanguage()}"></span>
</div>
And next is a snippet from the bean:
public String switchLocale() {
locale = LocaleContextHolder.getLocale();
if (locale.getLanguage().equals("en")) {
LocaleContextHolder.setLocale(EN_LOCALE);
return "?lang=el";
} else if (locale.getLanguage().equals("el")) {
LocaleContextHolder.setLocale(GR_LOCALE);
return "?lang=en";
} else {
return "";
}
}
So, the code from the bean IS invoked successfuly. I guess this is the solution to my issue.
Thanks a lot from the 2 people #M.Deinum, #Wim Deblauwe, who advised me.

ASP.NET page calls Java Applet and fields lost focus

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.

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.

Generating multiple events in JSP page having more than one button

I am a beginner in the JSP World as per I have noticed that, there is one form tag which has action and method attribute. In action tag we must write the URL of the servlet which gets activated after clicking the submit button.
But my problem start here. I am trying to develop a register page.
I have two servlets:
one checks for the availability of existing user,
second register the user.
Does anyone have any idea how to achieve it without a href link or image button?
Move the code that checks for availability of the existing user and register the user to its own service class say UserService. Make the form submit to 2nd servlet which uses UserService to perform both the operations.
You can have two separate forms but not nested forms in HTML. If you want a single form to change its actions (its target URL) depending on which submit is used, the only way you can achieve that is through javascript.
You can do this with your existing approach by registering a Javascript function
<body>
<script type="text/javascript">
function submitType(submitType)
{
if(submitType==0)
document.userForm.action="CheckUserAvailability.do";
else
document.userForm.action="RegisterUser.do";
}
</script>
<form name="userForm" method="post">
--Other elements here
<input type="submit" value="Check User Availabiliy" onClick="submitType(0)"/>
<input type="submit" value="Check User Availabiliy" onClick="submitType(1)"/>
</form>
</body>
Yes You can.. In the action you give the Servlet name. and inside that servlet you can call java methods which are written in other classes.
Which means you can check
1.one checks for the availability of existing user
2.second register the user
Using two java classes(may be according to your choice)
Just call those methods with in the same servlet..Guess you got an idea.

jsp function call and xml

i need to know the way for call a function in jsp?
in my function, i wrote a code for get user input and write it in xml file... when i call it, there is a error... how could i achieve this.?
<form method = "post" action="Result.jsp" >
<input type="submit" name="submit" value="Submit" onclick="writeXml()"/>
Place your function (method) in a class that extends HttpServlet
map your servlet in web.xml
make action="/yourServletMapping"
process the submit in the doPost(..) method
But first, read some servlets tutorial.
Update: place the jena jars in WEB-INF/lib
You are trying to call a java method in HTML/JSP.
This cannot be done.
When you write a JSP, and "access" it in a browser, the server (like Tomcat) will "process" the JSP and pass the "output" to the browser. The browser sees only HTML/CSS/Javascript and no java code.
The onclick is called by the browser, so the java method cannot be called here.
You need to submit the form to servlets - something like "pass control to servlets" and from there you can call java methods..
Write a servlet. In the onclick event, submit the form. And follow Bozho's advice. (As he said, please read some tutorial on Servlets)
EDIT:
BTW, the exception you had mentioned is NOT because of this. There is something else wrong. And to find out what is wrong, we need more details from you. Apart from the JSP what else do you have? Read the complete exception stack trace. Does it mention any of your classes?

Categories

Resources