checking whether the variable is true or not - java

function add(){
<%if(empRecNum != null && !(empRecNum.equals("")))
{
empSelected=true;
}
boolean canModify = UTIL.hasSecurity("PFTMODFY") && empSelected;
%>
df('ADD');
}
When i click on add, i need to check whether the empSelected is true or not and pass this canModify value. Will this be called?
Is this right way i am checking a Scriptlet inside a JavaScript

A more elagant way to do this
var canModify = Boolean(${canModify});
Use jstl el, it turns more clear what do you intend to do.
The call to boolean will convert the given value in javascript boolean.
Remember:
Boolean(true); // returns true
Boolean(false); // return false
Boolean(); // returns false

You need to get the following concept right: Java/JSP runs at webserver and produces HTML/CSS/JS output. Webserver sends HTML/CSS/JS output to webbrowser. Webbrowser retrieves HTML/CSS/JS output and displays HTML, applies CSS and executes JS. If Java/JSP has done its job right, you should not see any line of Java/JSP code in webbrowser. Rightclick page in webbrowser and choose View Source. Do you see it, right?
The webbrowser has totally no notion about the Java/JSP code on the server side. All it knows about and can see is the HTML/CSS/JS code it has retrieved. The only communication way between webbrowser and webserver is using HTTP requests. In the webbrowser, a HTTP request can be fired by entering URL in address bar, clicking a (bookmark) link, pressing a submit button or executing XMLHttpRequest using JavaScript. In the webserver, the Java/JSP (and Servlet) code can be configured so that it executes on certain URL's only. E.g. a JSP page on a certain location, a Servlet which is mapped on a certain url-pattern, etcetera.
In a nutshell, to have JavaScript to access Java/JSP variables, all you need is to let Java/JSP print them as if it is a JavaScript variable. To have JavaScript to execute Java/JSP methods, all you need is to let JavaScript fire a HTTP request.
See also: Communication between Java/JSP/JSF and JavaScript

The canModify value defined in JSP is never passed to JavaScript. You need to redefine the variable in JavaScript, for example:
<%
if (canModify) { // This is the JSP variable
%>
var canModify = true; // This is the JavaScript variable
<%
} else {
%>
var canModify = false;
<%
}
%>
On a different note, you should abandon JSP scriptlets and switch to JSTL.

Related

How do I prevent jsp scriptlet from execution?

when i run the program, both statements inside the condition got executed
<script>
if (true) {
<%System.out.println("true");%>
} else {
<%System.out.println("false");%>
}
</script>
Java and JavaScript are completely different—their similar names notwithstanding. They have as much in common as the words “pain” and “painting”.
JSP code is executed in the server, before the user ever receives the page.
JavaScript is executed on the end user’s computer, after the server has executed all of its code, assembled the page, and delivered it to the user’s browser.
The server doesn’t process any JSP content other than JSP tags, or known tag libraries like JSTL. That <script> element and its content is simply added to the final page as is, without ever being interpreted or executed.
In other words, if (true) means nothing to the server. It ignores the JavaScript, and only executes the <%…%> parts.
When the page is finally delivered to the browser, the source will contain this:
<script>
if (true) {
} else {
}
</script>
…because the JavaScript was not processed in any way on the server side. JavaScript in HTML pages runs in browsers, not on the server.
You probably want something like this:
<%
if (true) {
System.out.println("true");
} else {
System.out.println("false");
}
%>
Not that the above does not need to be in a <script> element, and in fact it would not make sense to put it in one—because <script> contains JavaScript and this is server side Java code, not JavaScript.

I want to use java variable in single jsp page

<%!
public void runJavaMethod(int id)
{
%>
<%
try{
String icd = request.getParameter("icd");
String inm = request.getParameter("inm");
String istk = request.getParameter("istock");
String sstk = request.getParameter("sstock");
String upr = request.getParameter("uprice");
String spr = request.getParameter("sprice");
r = s.executeQuery("select * from itemsyncdata");
while(r.next())
{
s.executeUpdate("update itemsyncdata set itemcode='"+icd+"',itemname='"+inm+"',instock='"+istk+"',storestock='"+sstk+"',unitprice='"+upr+"',storeprice='"+spr+"' where id='"+a+"'");
}
}
catch(Exception e)
{
e.printStackTrace();
}
%>
<%!} %>
And I am Calling Function from html like
<input type="submit" id="btnSync" value="Sync" class="button" name="Sync" onclick="<%runJavaMethod(r.getInt(1));%>"/>
So We want runJavaMethod Parameter.
onclick="<%runJavaMethod(r.getInt(1));%>"/>
HTML/Javascript Plays on client side and JSP/Java plays on server side.
Simply you can't. You might misunderstand that JSP and HTML/JavaScript existed on same document. Yes but JSP part compiles on server side itself comes to client.
What you can do is you have to make a server request. Most probably look at Ajax requests.
You are trying to mix up two languages i.e., Java and Javascript/html together i.e., onclick is a Javascript event and you can't call runJavaMethod from Javascript.
In simple words, you can't directly call a Java method (present inside the scriptlet) using Javascript because all of your JSP code produces (becomes) html when it is loaded from the server.
So, if you have to fix the issue, upon onclick, you need to call an URL which hits a servlet/controller method on the server to do the job (i.e., executes the business logic).
One more important point is that Scriptlets are legacy code and I strongly suggest not use them to generate or manage the html content, rather use JSTL so that there will be a clear separation between the concerns (i.e., business logic and user interface requirements).
Also, I strongly suggest you read the JSP best practices from here and follow them.
You are mixing to two different things. JSP is server side code and it's rendered response is the HTML that is send back to the browser.Javascript is pure client side in your case.
If you really want invoke a server side processing than create a simple Java script function with Ajax call and get response back which u can use it.
I suggest send all the logic of JSP in backend class not a good practice to put in the jsp. JSP is ideally for UI design.

Calling part of jsp page from Servlet or Java Class

I need to call small part of JSP page(like only div load) when a method is invoked? I know we can load whole JSP page from Servlet. Is this possible or any other solution for this?
know we can load whole JSP page from Servlet. Is this possible
Yes it is possible, you forward your request to jsp from servlet and it will render that jsp, here is an exact same example
1) Set some session or request variable equal to some value, say boolean myVar = true.
2) Redirect from your jsp page from your servlet.
3) In the jsp page, check for particular condition for the variable myVar. If condition is fulfilled, using scriptlets or JSTL display only the relevant part of you jsp.
You can use AJAX(Asynchronous Javascript and XML) for this purpose
For example, Suggestions, that appear without any refresh for auto-complete in google search, are using Ajax. JSP code will contain the Ajax code and will send the information to the servlet in form of XmlHttpRequest object and then takes the response from servlet as xmlhttp.responseText. The result can be written at JSP page by using DOM.
To initiate this process, you need to use the onkeyup in input tag like this:
<input type="text" onkeyup="methodName(this.value)"
Learn more about Ajax
To use AJAX with Java, try this
Ajax for Java Web Applications
$('#myDiv').load('serverPage.jsp #server_div_id');
OR
$.ajax({
url: 'serverPage.jsp',
success: function(data) {
data=$(data).find('div#id');
$('#mydiv').html(data);
}
});

is their any to assign or get Javascript variable to java variable?

......
<%
int s = (int) (Math.random() * 1000000);
%>
.................
<body bgcolor="<%=s%>"> .......
it shows no error and executing but on viceversa it shows error.I want JS varivble in java is their any otherway to do?
.....
var a=10;
<%
int s = a //is their any other way
%>
i know getParamater() method i want another alterntive way
No, you cannot do it like that. The Java is in a JSP that is executed on the server to generate a web page (in this case containing Javacript) which is returned to the user's web browser. When the web browser receives it typically renders it, and the Javascript is executed immediately or in response to some user action. By the time that the Javascript executes, it is on the wrong machine, and the execution context for the original JSP has gone away.
If Javascript needs to pass information to the server, it must do it by means of a new HTTP request. It could use an XmlHttpRequest object explicitly, or it could put information into the elements of a <form> in the current web page, or something similar.

Get user input from textbox on JSP

I need to get the value that a user has entered in a textbox on a JSP. I used JavaScript to obtain the value, but then I need to access that same variable outside the JavaScript later on in the page.
Here's what I have:
<script type="text/javascript">
var sp;
function setPolicy(){
sp = document.getElementById('policy').value;
if(sp != "")
alert("You entered: " + sp)
else
alert("Would you please enter some text?")
}
</script>
the textbox:
input type="text" id='policy' size="15" tabindex = "1" onblur="setPolicy()"
But I need to access the string entered in this "policy" textbox later on in a scriplet to call a function within my session bean. Is there any way I can pass that variable outside the JavaScript? Or is there another way to do this?
Thanks!
JavaScript execution happens at client side and Scriptlet executes at server side. You are trying to combine the two.
You should submit the form to the same page by passing a param which will have the value entered in the textbox. Your scriptlet should check if the param is present or not. First time coming on this jsp the param will not be present, it will be available only when user enter something in textbox.
May not be the best solution as I don't know the whole context.
I'm not sure you have the lifecycle of a jsp down quite yet. By the time that the javascript is running, all the scriptlets have been evaluated.
By this I mean, one the JSP is rendered, html and javascript are emitted on the response to the browser, and there is no more server to interpret the JSP.
So, you should think of your problem as how do I communicate back to the server, the result of the user action?
Probably by posting a form to an action on the server.
If you are asking how to get hold of text input value in a java session bean, it has nothing to do with your javascript code.
The session bean is server side code. To pass the input value to your session bean, you need to change server side code, a servlet, strut action, webwork action depending on which web framework you use.

Categories

Resources