problem getting value from jsp to servlet - javascript

I am trying to get value of count (which is 1) from the parent jsp page into servlet.
But it gives an error: "opener.document.getElementById(...)is null or not an object".
Is this because i'm trying to get value without submitting the form??
Will be glad if someone could help.
jsp:
<td> <input type="hidden" name="count" id="count" value="1"></td>
servlet:
out.print("var oWindow = window.external.menuArguments;");
out.println("var ctr=oWindow.opener.document.getElementById(\"count\").getAttribute(\"value\");");

That all depends on which page window, oWindow, or oWindow.opener is pointing to, do you really have 3 client-side window objects involved in this operation? Perhaps it should just be:
var ctr=oWindow.document.getElementById('count').getAttribute('value');
...or you should give a little more information as to the structure of your application. For example, window.external is for when you have extended MSIE and added your own context menu item, this has very little to do with servlets.

Related

Getting values from html form as JavaScript variables

i want to capture input variables sent via form from one page to another.html page and obtain these values as JavaScript variables. Can this be done.
This is my form;
<form action='another.html' id='form' data-ajax='false'>
<input id='sent_by' value='tom'/>
<input type='submit' value='Submit'/>
</form>
And in my another.html i tried to get the values as;
var sent_by = $.session.get("sent_by");
But i am not able to get the values. All help is appreciated.
You can use the browser's localStorage to achieve this. Before submitting and going to the other page store all the values of the form in the localStorage and you can access it on the other page:
Page1.html
Field Name = "name" <input type="text" name="name" id="name" />
Read the value and store it to localStorage
localStorage.setItem('name', document.getElementById('name').value);
and so on.
You can make a function in JavaScript that saves all the fields of the form in localStorage on / before submit.
To read these values on the other page:
Page2.html
Value stored for key name can be get using the following JavaScript:
localStorage.getItem("name");
NOTE
the page1.html and page2.html should be in the same domain for you to access the localStorage.
Read more about localstorage at https://developer.mozilla.org/en-US/docs/Web/API/Storage/LocalStorage
Have you tried using Window.localStorage? It's similar to sessionStorage but with no expiration date. So, be sure to clear it once the browsing session ends.
I think the answer provided by #Shakti Phartiyal is probably the most practical and a sweet trick I might add. The main reason I'm writing this post is because I had also taken a long path of using javascript to pass along this kind of information. It resulted in me bewildered at how powerful PHP can be for some tasks that you dedicated javascript to do. (Yea I know some javascript wizards out there can do everything with it, I'm just talking about basic programming tho). So if you wondered what the PHP way of passing this around:
Your modified html form:
<form action='another.html' id='form' data-ajax='false' method='post'>
<input id='sent_by' value='tom' name='uname'/>
<input type='submit' value='Submit'/>
</form>
and then in "another.php" you would have this to retrieve the input from the form:
<?php
$uname= htmlspecialchars($_POST['uname'];
?>
Great. But how to make this php variable into javascript?
Ah, the fun part. You're going to write javascript to your webpage with php - you do something like this:
var uname = "<?php echo $uname; ?>";

Retrieve variable name from an HTML site

I am trying to retrieve simple javascript variable (which is written to a File Systems Object) from a website which is served by an apache host on my ubuntu laptop.
So I have the function that writes the variable set up as follows:
<script type ="text/javascript">
function WriteToFile(passForm) {
set fso = CreateObject("Scripting.FileSystemObject");
set s = fso.CreateTextFile("/home/lex/Downloads/goal.txt", true);
s.writeline(document.passForm);
s.Close();
}
</script>
and the section that takes the user input from the html website is
<div id="bot-right">
<form onsubmit="WriteToFile(this['goal'].value)">
<a align = "left"> <b><Strong>Enter a Goal name</Strong></b></a><br>
<input type="text" name="goal"> <br>
<input type="submit" value="Send Zeus">
<br>
</form>
</div>
For some reason, when I type in variable names to the form on the website, the file goal.txt gets created in the directory, /home/lex/Downloads/, but nothing gets written to it.
I also noticed that when I delete the goal.txt file and rewrite the variable from the html website, the file doesn't always get created.
I am not a JavaScript person and I am at a loss as to what I may need to fix this.
My intention is to get the variable written to the text file and have a processing c++ file process the variable.
Would someone be kind enough to lend an insight?
Thanks!
one way to do it is just calling the function without parameters and just getting the input value like this:
adding and id or a class to your input to get that specific value:
document.getElementById('goal').value
document.getElementByClass('goal').value
Or getting the value by name:
document.querySelector('[name="goal"]').value;
EDIT1
You could add a console.log to check if the value is beign passed correctly like this:
var inputValue = document.querySelector('[name="goal"]').value;
console.log(inputValue);
And if the value is being passed then the problem is your writeline or in the creation of the document
EDIT2
I just tested it and retrieving the value works just fine, so the problem must be in your document writing method, please check this documentation it can help you and i think is a better solution:
http://www.html5rocks.com/en/tutorials/file/filesystem/

jquery val different from actual value

I'm trying to setup CSRF protection in asp.net mvc but I'm having problems with the Ajax. It's mind boggling as to why.
I'm using the built in mvc token, which looks like this on the page:
<input name="__RequestVerificationToken" type="hidden" value="gnAeuskcQCq+bjF5bSKg/wyed65M2XNedTPmvKu/+tzCe1GEmIhyeUnurdMgg0ORjfkCBjyOYe78WS6TdJaPSeECTl7w5yjzgwb6TSLjR74cYE4Bd4lJ9YgBGCTyYXur+39inD60jtQh7VciTlM2ap9/YLim/RSCNMxbrs+KMZM=" />
The problem is that when I get the value using jquery it's different to whats on the page. So when I do this:
requestVerifStr = $(document.getElementsByName("__RequestVerificationToken")).val();
I get this value:
X1WV+xrlkKXYjmLZS+OpPfjgeEPGjM/CjOSk0nKwV6iu3rCpHAo2XIDlJcX2ef4hBnK/BYrZw6xICQWqd8a7yJOjngDn1Yf7AKEl2uwLAtMXQQuRJkCzpq+lOWVv1Jyuud6dEIRiaXT0koGDxycps9wQoXvvnMlF1/4G5LDp9Y0=
I'm assuming there's some type of conversion jquery is doing.
It would seem that there are multiple inputs with name="__RequestVerificationToken" and .val() only returns the first result.
Here is an example of how that would happen: http://jsfiddle.net/4qA9V/

jsp in javascript

I've created a jsp page. In that when i select 1 check-box or both check-box or none, the corresponding text-boxes and list-box must be displayed in the same page.
For that i tried of calling a javascipt function when i click the checkbox. The javascript function contain code to display the textboxes. But it didn't work.
Since I'm doing this project in struts, I don't know how to get check-box value. And calling of JavaScript function works. But didn't enter into jsp code in JavaScript function.
My code is
<tr>
<td>SEJ:</td>
<td>SEJ 1:<html:checkbox property="sej1" value="on" onclick="checkbox_trial()"></html:checkbox></td>
<td>SEJ 2:<html:checkbox property="sej2" value="on" onclick="checkbox_trial()"></html:checkbox></td>
</tr>
<script type="text/javascript">
function checkbox_trial()
{
<% if(request.getParameter("sej1")=="on"){
%>
<tr><td>SEJ1 Age<html:text property="sej1_age"></html:text></td></tr>
<tr><td>SEJ1 DOI<html:text property="sej1_doi"></html:text></td></tr>
<%}
else if(request.getParameter("sej2")=="on"){%>
<tr><td>SEJ2 Age<html:text property="sej2_age"></html:text></td></tr>
<tr><td>SEJ2 DOI<html:text property="sej2_doi"></html:text></td></tr>
<%}
else if((request.getParameter("sej1")=="on")&&(request.getParameter("sej2")=="on")){%>
<tr><td>SEJ1 Age<html:text property="sej1_age"></html:text></td></tr>
<tr><td>SEJ1 DOI<html:text property="sej1_doi"></html:text></td></tr>
<tr><td>SEJ2 Age<html:text property="sej2_age"></html:text></td></tr>
<tr><td>SEJ2 DOI<html:text property="sej2_doi"></html:text></td></tr>
<%}
else{%>
NOTHING <% } %>
}
This is how it works: Java/JSP runs at webserver, produces HTML/CSS/JS, webserver sends it to webbrowser, webbrowser runs HTML/CSS/JS. Not Java/JSP. Rightclick the page in webbrowser and choose View Source. If Java/JSP has done its job right, you shouldn't see any line of it in there.
If you want to invoke Java/JSP code using JavaScript, you've got to invoke another HTTP request to the webserver so that it can execute the Java/JSP code based on the specific request. You can do that by either submitting the form or firing an asynchronous (ajaxical) request.
Given the skills shown as far and the fact that you're using Struts, I think ajax is going to be a bit too complex. I'd suggest to just submit the form on click of the checkbox
<input type="checkbox" name="show" value="true" onclick="submit()" />
and then let JSP conditionally display the input fields (just a JSTL example since I don't do Struts)
<c:if test="${param.show == 'true'}">
<input type="text" />
<select />
</c:if>
Update: you've by the way another major problem in the code. You can't compare string values by == in Java code (you can do so in EL only). In Java code you need to use equals() method. Otherwise they will be compared by reference instead of by value. I'd suggest to learn basic Java as well.

How do I pass Javascript variable to <jsp:setProperty> and JSTL?

How do I pass Javascript variable to and JSTL?
<script>
var name = "john";
<jsp:setProperty name="emp" property="firstName" value=" "/> // How do I set javascript variable(name) value here ?
<c:set var="firstName" value=""/> // How do I set javascript variable (name) value here ?
</script>
You need to send it as a request parameter. One of the ways is populating a hidden input field.
<script>document.getElementById('firstName').value = 'john';</script>
<input type="hidden" id="firstName" name="firstName">
This way you can get it in the server side as request parameter when the form is been submitted.
<jsp:setProperty name="emp" property="firstName" value="${param.firstName}" />
An alternative way is using Ajax, but that's a completely new story/answer at its own.
See also:
Communication between Java/JSP/JSF and JavaScript
Your previous question regarding the subject
Your other previous question regarding the subject
If you can't seem to find your previously asked questions back, head to your user profile!
AFAIK you can't send data from JavaScript to JSTL that way. Because the JSTL tags are handled serverside, so the <jsp:> tags will be parsed on the server and replaced by HTML. So the <jsp:> tags won't be a part of the response that is sent back to the client; it will consist only of HTML/text. Therefore you can't access the <jsp:> tags from JavaScript, because they won't exist in the document.
Edit: sorry, the <jsp:> tags wasn't visible.
<script>
var name = "<jsp:getProperty name="emp" property="firstName" />";
</script>
The JSP code executes before the JavaScript so by the time the JavaScript gets processed the tag will be replaced with the contents of emp.firstName.

Categories