I have a webpage which asks for username,password and there is a log in button. Log in button uses jsp function as follows
function submitData(){
document.getElementById('txtSN').value=document.getElementById('txtRegNumber').value;
document.getElementById('txtPD').value=document.getElementById('txtPwd').value;
if(document.getElementById('txtSN').value == '' || document.getElementById('txtSN').length == 0) {
alert(' Please Enter Student Register No/Studen ID');
document.getElementById('txtSN').focus();
return false;
}
if(document.getElementById('txtPD').value == '' || document.getElementById('txtPD').length == 0) {
alert(' Please Enter Password');
document.getElementById('txtPD').focus();
return false;
}
if(document.getElementById('txtverifycode').value == '' || document.getElementById('txtverifycode').length == 0) {
alert(' Please Enter Verification Code');
document.getElementById('txtverifycode').focus();
return false;
}
document.getElementById('txtPA').value=1;
document.getElementById('txtRegNumber').value="iamalsouser";
document.getElementById('txtPwd').value="thanksandregards";
document.getElementById('frmStudentMain').action="youLogin.jsp";
document.getElementById('frmStudentMain').submit();
}
Can I use the above code in android. That is I will create two EditText for username and password; and I am going to create a function which is going to use above jsp code. What my question is, Is it possible to use jsp code for buttons in android?? Whether I want to convert the jsp code to java??
Actually I am newbie. If my question doesn't sounds good I am sorry about that.
Not sure what you want to explain because your snippet is not actually a jsp code but it is javascript function.
Regarding jsp implementation in android is,
JSP can't be implemented in Android. As JSP is build up using scriptlets and scripting tags which is deployed on server only. Server renders that jsp page whenever any request from client came. As a result it will convert data to pure HTML code and server to requester.
So, that is not at all possible to do on mobile.
But yes, if you want pure HTML to be display on android that can be possible using Web components of android. You can ajax call (in a way services) to server through it, get result (XML or JSON response), and render it back to your required view using JQuery or pure Javascript.
Related
I´m trying to add hotkeys to the web application we use at work. The solution was to apply a Greasemonkey script, but the web system uses Liferay Portal, which is made with JavaX and jspx server applets.
I DON´T KNOW HOW TO APPLY "WAITFORKEYELEMENTS" answer, my knowledge is not that advanced.
What is the problem? I need to search the label for a link, i.e. “case file history” , add a keylistener event and simulate a mouse click. Here´s an example link:
<a id="form1:commandLink1211" name="form1:commandLink1211"
onclick="submitForm('form1',1,{source:'form1:commandLink1211'});
return false;"
class="linkMenu" href="#">
Look case file history
</a>
I need it to simulate a mouse click into this link, but the getElementById returns null. The page loads “empty” and then loads the jspx module. I know greasemonkey puts the script first. I tried to use
window.onload=funcion
document.ready() //didn´t know how to add this to my code
Other solutions include using a timer, but I don´t know how to apply it.
Current greasemonkey script:
(function(){
document.addEventListener('keydown', function(e) {
// pressed alt+g
if (e.keyCode == 71 && !e.shiftKey && !e.ctrlKey && e.altKey && !e.metaKey)
{
console.log ("key pressed");
document.getElementById('form1:commandLink1211').click();
}
}, false);
})();
The default behaviour of GreaseMonkey and similar alternatives is to run userscripts after the DOM (document-object-model) has been loaded, i.e. as soon the HTML has been parsed but before included resources have finished to load.
Some Scripts need to do stuff before the DOM has loaded. To achieve this they use the meta block entry #run-atdocument-start. This gives you the chance to intercept anything the site is doing while loading. However, with this entry your scripts do not know anything about the document when they start running. (Note the limited support in GM 4.0)
If you do not need to do stuff before the site has been loaded, use the meta entry #run-at document-end or omit this entry at all.
The more accurate approach is to implement an event listener. Modern browsers support the DOMContentLoaded event, formerly known as "domready" event in some JS-frameworks.
document.addEventListener('DOMContentLoaded', evt =>
{
// Do DOM-based stuff here...
document.addEventListener('keydown', evt =>
{
document.getElementById('form1:commandLink1211').click();
});
});
Using JQuery the shorthand $(evt => {/* DOM ready stuff */} ); will also work.
The element was not loaded yet when the Javascript ran, so the element with the given id was not found. Try to wrap a function around your code:
function loaded(){
document.addEventListener('keydown', function(e) {
// pressed alt+g
if (e.keyCode == 71 && !e.shiftKey && !e.ctrlKey && e.altKey && !e.metaKey)
{
console.log ("key pressed");
document.getElementById('form1:commandLink1211').click();
}
}, false);
}
and make sure you have
onload="loaded()"
in the body tag.
Instead of document.getElementById('form1:commandLink1211').click(); can yo instead do submitForm('form1',1,{source:'form1:commandLink1211'});. It should do the same thing, right?
Good evening,
I have a form on a JSP page that's connected to a servlet, that form has some dynamic parts using JavaScript like adding a row to a table or adding a text field based on the selected option on a select element, Actually my problem is that I have some validations on the servlet-side, so when I go to servlet to check the (National ID) for example if there's any problem or any violations to my validation I force to get back to the form using :
if (dbm.MatchIdNumber(Candidate.getRegNumber(), Candidate.getNationalID()) == false) {
out.println("<script>\n"
+ " alert('Your National Id does not match your Registration Number');\n"
+ "</script>");
out.println("<script>\n"
+ " window.history.go(-1);\n"
+ "</script>");
}
What happens is when I get back to the form I lose all the JavaScript changes, Which's very important.
I've been reading for a while that using ajax might be the optimal solution for me, but here is my questions:
Is there a way to call a java method from JavaScript or JQuery before getting to servlet without using ajax !?!
Is there a way to get back from the servlet to the jsp page with the ability to keep all the JavaScript Chages !?
If not !!, How to use ajax in my case ?!
Thank you so much
No. JavaScript runs on the user's browser and your Java code runs on your webserver, so basically the only way to communicate between the two is via HTTP requests.
If you don't want to use AJAX, you could provide all of the relevant info when you submit the form to the server for validation. You could pass all the info you need to re-generate the form as it was, like which new fields are there and such
First, you'll need to add a new webservice to your Java webapp which performs validation. To achieve this, you could either add additional logic to your servlet (so that it looks for a request parameter like "doValidation=1" and performs validation if it's there) or write a different servlet that handles validation itself. You'll need to decide the format it should expect the form data in and how it should return the validation information.
On your frontend page, you'll need to modify the behavior of the form so that, when you need to do validation, it performs a request to this webservice and passes along the form data. I would probably do this with jQuery and do something like jQuery.ajax(...) and pass the contents of the form as a JSON object.
When your validation servlet returns data from the ajax call, you'll need to update the form based on the data it provides. If I was doing it, I would probably just have the servlet return a JSON object like {errorMessage:"..."} and I would use jQuery to add an element to the form containing the text of the validation error when it occurs. If the servlet returns an empty string or JSON object or something, I would consider it a validation success.
I'm working on a system for my university and I'm using the play framework to do that.
The Admin of this system sets a marker on a google map and I get the coordinates from that point.
Now I'm trying to pass this information to the server side, so that I might store these to Strings in a mySQL database. The only problem I have is passing the data from my String in javascript/JQuery to the java function.
I tried different solutions on the internet but some of them seemed outdated and I couldn't figure out how to do it.
I've only been programming in Java, Javascript, JQuery and PHP and have never used AJAX (like the $.get() methode from JQuery), but I think it might be pretty similar to what I know from PHP.
e.g.
http://java.dzone.com/articles/jquery-ajax-play-2
I'd like to pass my String with a button click to my java function, so I can store it in my db.
I'm really confused about this.
I know I can use something like
<a href="{#routes.Application.postMethod()}"> Send </>
and then mention the function in the routes like
POST /post controllers.Application.post();
but how do I pass my qjuery string?
and how do I store my String as a String in a java function like:
public static Result post(String Lat, String Lng){
???????????? EVOLUTION NEEDED ?????
}
Thanks in advance I really need your help :)!
I don't see why you are doing a POST, since this can be done using a GET request.
As per this example:
http://www.playframework.com/documentation/1.2/ajax#jsaction
We can see Play makes it easier using the jsAction tag. Lets assume you have the following route:
GET /admins/marker Admins.marker
Then in your HTML, at the bottom you'd do something like:
<script type="text/javascript">
$('#myButton').click( function() {
var action = #{jsAction #marker(':latitude',':longitude') /}
$('#result').load(
action({latitude: '23', longitude: $('#longitudeField').val }),
function() {
$('#content').css('visibility', 'visible')
}
)
});
</script>
In this case, the request will be sent like (example):
GET /admins/marker?latitude=23&longitude=67
And then on your backend, you need the have to java fn to deal with that route.
Basically the javascript/jquery, is called when #myButton element is clicked, then we generate the route URL we are going to make a request to using jsAction, then we make using load to make a GET request. You can change this to post too if you'd like.
I know that variations of this question have been asked but I have yet to be able to implement any code that successfully works, so kindly forgive any redundancy. My restaurant used a third party to produce a mobile app and menu on cel phones, etc. To accomplish this, on my wordpress site, I have inserted the following (specified by third party) code in the header.php section:
<script src='http://s.singleplatform.com/js/mobile_redirect.js'></script>
<script>
if(document.referrer)
{
if(document.referrer.indexOf('m.singlepage') == -1) redirect_mobile_browser(navigator.userAgent||navigator.vendor||window.opera,'http://www.singlepage.com/victorys-banner/menu');
}
else
redirect_mobile_browser(navigator.userAgent||navigator.vendor||window.opera,'http://www.singlepage.com/victorys-banner/menu');
</script>
But like others who have asked, I only want www.victorysbanner.com (or victorysbanner.com) to land on the mobile site, but for other URLs, for example, www.victorysbanner.com/about, I want to bypass the call to the mobile site and go to the actual URL. I have tried various if statements but just can't make it happen. Any help would be greatly appreciated.
Pradhan Balter
try putting
if(document.URL == "http://www.victorysbanner.com/") {redirect code}
around your code. you may need to add
|| document.URL == "http://victorysbanner.com/"
if your webpage can be accessed with or without the www.
Is there any ASP.NET Page.IsPostback equivalent in JSP/Java? What I need to do is check whether this request is first time or is it result of post back. There are few Combo Boxes on JSP page and I need to load it only for the first time, (I am loading combo box's data from DB) not everytime page reloads. We reload the same page based on various input/selections etc. So I was looking for that type of method.
if ( "POST".equalsIgnoreCase(request.getMethod()) &&
((request.getRemoteURL() != null &&
request.getRemoteURL().toString().equalsIgnoreCase("http://yoursite.com")) {
//postback call
}
You may also see this question