ASP.NET Page.ispostback equivalent in JSP/Java - java

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

Related

Javascript changes dissapear when going back from servlet to JSP

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.

How to implement jsp code in android?

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.

JavaScript EJB variable field holding value?

I have an ajax enabled list of records that I'm going through and each one has a dropdown box that I'm trying to make a required field for the form to submit. To complicate matters the 'Close Record' button is not the submit button so I can't just use required attribute on the select(dropdown box) that I'm using. The value for the selected dropdown box is saved in an Enterprise Java Bean so I thought I could just write a JavaScript function to check the value:
function CheckForm() {
var clearObj = document.getElementById("mySelect");
if(clearObj.value != "") {
return true;
} else {
clearObj.style.backgroundColor ='yellow';
}
return false;
}
This doesn't work because once I close one and go to the next it's maintaining the value of the previous record on the page. Basically I have an update-content event that I need to know how to handle. Any ideas as to how to manipulate the DOM or JSON object to make this select a required field? Thanks.
With the little information given, I would assume that when you close the existing record and then loading the next record, you are doing it through an ajax request. If thats the case, then you can add a call back for the ajax request, which would reset the drop down.
This should be a comment, but as you see, I dont have 50 points :-)

ajax call in jsf 2.0 (myfaces), the onevent Javascript function in the ajax tag gets called before the rendering is complete

This is my first time asking a question on a forum, since usually my questions have already been asked and answered. I haven't found an answer to this problem that works for me, so here goes:
I'm making an Ajax call in JSF 2.0 like this:
< f :ajax listener="#{myReferenceController.clearRequiredReferenceNumber}"
onevent="resetFocus" execute="#form"
render=":resultsForm:ResultsDisplay" />
Everything in the listener works perfectly, and the data is then rendered as expected in the data table that I have in my .xhtml page. The problem is that the Javascript that I call in the onevent seems to be getting called before the rendering is complete, and therefore the process of resetting the focus to a column in my datatable doesn't work, since the datatable is removed and then re-added to the DOM when the Ajax is finished re-rendering.
I'm looking in my Javascript for the status of "success", in hopes that at this point, the rendering would have completed. Alas, this is not the case, and my getElementById (actually dojo.byId) is not finding the element in the datatable. I know my Javascript function works under normal circumstances, since I'm calling this same function in a situation where there is not an Ajax call, and everything works perfectly there.
If I could just avoid rendering the cell in the table that I'm trying to set focus to, that would be great, but my listener is making changes to this cell in the ajax call. I'm at my wits end, so any ideas on this would be very appreciated.
-- in response to Balusc (heard good things about you, btw)
Hmm, I was actually on the right track I think, but still seem to be having troubles. I am checking for "success" and still even in success, am not able to set the focus here.
Here's my Javascript function that is checking for "success": This function works in another situation, where it's not attached to an Ajax event.
function resetFocus(data) {
var theRow = dojo.byId("resultsForm:selectedRow").value;
if (data.status == "success") {
dojo.query('[widgetId]',dojo.byId('theResultsDataTable'))
.forEach(function(node) {
var widget = dijit.byNode(node);
var theId = widget.attr("id")
if (theId.indexOf(':' + theRow + ':') != -1) {
if (theId.indexOf('theOrppoNum') != -1) {
widget.focus();
widget.attr("isFocused",true);
}
}
});
}
}
The function attached to the onevent attribute will actually be invoked three times. One time before the ajax request is been sent, one time after the ajax response is been arrived and one time when the HTML DOM is successfully updated. You should be checking the status property of the given data argument for that.
function onEventFunction(data) {
var status = data.status; // Can be "begin", "complete" or "success".
switch (status) {
case "begin": // Before the ajax request is sent.
// ...
break;
case "complete": // After the ajax response is arrived.
// ...
break;
case "success": // After update of HTML DOM based on ajax response..
// ...
break;
}
}
In your particular case, you thus just need to add a check if the status is success.
function resetFocus(data) {
if (data.status == "success") {
// Do your job here.
}
}

what is the best way to display website visitors a one time welcome message?

I've got a website, and I want to add a welcoming message which hovers on a certain part of the page which only loads for the visitor for the first time they login, and won't again(presumably cookies used). And says something like "adjust your settings here.."
I don't want it to be an external popup but something that loads on the page in a certain area, defined by me (PX-pixle reference)
What would be the best coding language to do it in, oes anyone have any examples of this, or any site based generators to make it on?
thanks
Create one more field in database with lastlogin.
When user is created then make lastlogin field with special.
When user signs the next time from Login Page, update the field the lastlogin value to regular
//query to get value of lastlogin
//add css to elements you want to hover
<element class="<?php if($last-login == 'sepcial') { echo 'sepcialcss'; } else {echo 'regularcss'; }">
Done in PHP
As you added the tag, php would do this, actually any language will do.
Generally you have two ways to do this.
Do it on your server.
Do it on client's computer.
for the first way, you check the cookies and generate the page you want.
for the second way, you need to arrange the page the visitors see with java script.
way 1 recommended, coz it loads less bits. LOL
Update:
your server supports php right? the page, say it index.php, has a special area which is different when the visitors login the first time, right?
<?php
if (firstLogin()){
genSpecial();
}
else{
genRegular();
}
?>
in the funcition firstLogin(), you shall read the cookies and determine.
in the other two functions, just gen two different part, i.e. some html source code.
to your question, if you need to load some image, do it in genSpecial(). and if you choose the first way, js is not used to gen the special area, it's used only if in the special area, there needs some js.
It is possible through javascript. Once the user is shown the settings, store the result in a cookie valid for as long as you want. The next time the user logs in, verify if the cookie is set and then proceed.
Sample code to create cookies:
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
Refer this for more details on how to create and use cookies

Categories