I have this cezar.jsp file and the following code inside my head below.
I have a textarea and I want to pass the value as a parameter for a java function.
<head>
<%# page import="cpd.CezarBun" %>
<script>
<%
cpd.CezarBun cezar = new cpd.CezarBun();
//don`t know how to use scripplets here
String contentIn = document.getElementById('myTextArea').value;
cezar.criptare(contentIn); //takes a String parameter
%>
</scrupt>
</head>
For a JSP scriptlet be able to use a value typed in a form field, you will have to send that value to the server in some way, given the fact that your server side Java code has no acess to what happens inside your form fields until you send the values. This means that document.getElementById('myTextArea').value will not work inside a JSP scriptlet block.
You could post the form and then use something like request.getParameter("fieldName") inside your JSP scriptlet block.
There is something you need to understand about the java in JSP's. It doesn't exist as far as the client side is concerned. All of the java code in a JSP is translated to a regular servlet. As far as the client side is concerned it's dealing with a standard html page. The only way for you to get client information to the JSP is the same with any other servlet.
Related
Code:
I had set the SessionData(EmployeeID) = "12345" in server side and it first executes at page_load
Then, In client side
function getEmployeeId() {
return "<%# SessionData("EmployeeID")%>";
}
I am getting the empty string for the above code.
Also, I tried using = as "<%= SessionData("EmployeeID")%>";
but I am getting the error as "The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
"
Suggest me a solution.
I resolved the issue by putting the script code inside the Body section. Previously, it was in the Head section.
I used the "<%= SessionData("EmployeeID")%>"; code to access the server variable. It was working fine.
I am trying to create Javascript Session variables that will read information from one form and pass these variables to another form.
I can assign strings to these Session variables but i cannot assign values from Form elements to these variables
function SetUserName()
{
<% var text1= "Charles";%>
<%Session["UserName"] =text1;%>
var session_value='<%=Session["UserName"]%>';
}
</script>
This is the Javascript on the first page which works and on a button click i call another page where i can display this Session Variable.
function LoadUserName()
{
var username = '<%= Session["UserName"] %>';
alert(username);
}
This gets called on load of second page. How can i pass values from HTML elements to the Session Variable?
For Example Say i have a Text Box
<asp:TextBox ID="Address_Box" runat="server" TextMode="MultiLine"
MaxLength="200" ></asp:TextBox>
How can I send the text from this box to the session variable?
Your code:
var session_value='<%=Session["UserName"]%>';
does not "access the ASP.Net session from javascript".
With this code you do generate (during the processing of the request) some text that will be sent to the browser. This text contains the string that results from the expression Session["UserName"].
Once this text arrives at the browser it is interpreted as javascript. This javascript has no knowledge of any session values or the fact that it was (partly) generated.
You need to find some way to send data from the browser back to the server (using postback or ajax, for instance) before it can be stored in Session.
You would have to put this somewhere in your c# code.
Session["UserName"] = Address_Box.Text;
For a more complete answer I'd have to see more of your code. For a better solution you'd have to explain what you're trying to achieve
I try to pass my JS variable into razor, my script fragment:
select: function (event, ui) {
var docID = ui.item.DoctorCode;
#{
string org_code = _unitOfWork.Doctors.GetById("").OrganizationCode;
}
doctorOrgLabel.text('#org_code');
}
In GetById() method i want to pass JS variable docID. I'd appreciate any help!
I try to pass my JS variable into razor
This sentence makes strictly no sense at all.
Razor is a view engine used by the ASP.NET MVC framework running on the server to produce some HTML template.
javascript on the other hand is a client side language running on the client. Once the HTML template is rendered to the client and javascript starts to execute there's no longer such notion as Razor.
If you want to pass some javascript variable to the server you have a couple of options:
make an AJAX call to the server
set the value of this variable in some hidden field inside a form and submit this form
use window.location.href to redirect to the server and passing the variable as query string parameter
store the javascript variable in a cookie which will be sent to the server on subsequent requests
How to insert JavaScript variable in JSP String variable.
<script>
<%!
String host_name = "tst" + location.host;
%>
</script>
<%=host_name%> <!-- getting error -->
Thanks for help.
Javascript is executed in the browser, and JSP on the server. So you can't directly get a value from javascript into JSP (java) : when the javascript is executed, JSP has finished its execution and sent the result.
Depending on your need, you may want to send a value using ajax from the browser to the server but it's impossible to define the applicable strategy without knowing why you tried this.
If what you want is just have your hostname in java/jsp, I'd suggest you to do something along the lines of
<%
InetAddress addr = InetAddress.getLocalHost();
String hostname = addr.getHostName();
%>
i m doing a database project using mysql as the database and jsp for my frontend part along with javascript and ajax for interaction.
Now the problem is i need to assign a java script variable which is having a string to a jsp variable.
I did this using the following statement?
<% String str="document.write(s)"; %>
where "s" is already defined as
<script type="text/javascript">
var s="hello world";
<script>
but i m getting error in the assignment statement(which is shown in bold above) as incorrect syntax?
the error i m getting is-
check the manual that corresponds to your MySQL server version for the right syntax to use near '<script>document.write(s)</script>'
what is the error in this stmt or is there any other method in doing this assignment?
Can anyone help in doing this?
You cannot do this. The JSP statement is executed server-side, before the execution of the Javascript statement, that is executed client-side after the browser received the http response.
It is not clear your goal, but if you only need to display in the page the value of a javascript variable, you can use:
trivial javascript:
document.write(s);
targeting existing element:
document.getElementById('myElementId').innerHTML = s;
using jQuery:
$('#myElementId').html(s);
If i understand correctly you are trying to build and execute a sql query based on user input that is handled by javascript. As ADC said this can not be done since jsp is executed server side therefore before browser executes javascript. What you can do is create the sql query and pass this as a parameter a different jsp/servlet (or the same if you can handle this case) which will execute the query
In the first page where sql statement is constructed in variable s you should put something like this
<script>
var s = "hello world";
function createLink(){
document.getElementById('mylink').href= 'page2.jsp?statement='+s;
}
window.onload=createLink;
</script>
<a id="mylink" href=""/>Click to exexute query</a>
which create a link to you second page (page2.jsp) passing the statement as parameter.
Now in page2.jsp you should retreive the parameter value like
<% String statement = request.getParameter("statement") %>
and then execute your query.
Even better you should use a servlet instead of a jsp page to perform the query. You could read a tutorial for jsp/sevlets to see how this can be done
eg. http://www.laliluna.de/articles/posts/first-java-servlets-jsp-tutorial.html