Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Calling a java script function from a jsp code

Posted on 2011-10-14
13
Medium Priority
?
1,434 Views
Last Modified: 2012-05-12
Hi Experts...
I have a jsp page which allows users to delete data from the database based upon certain conditions.
Here i want to restrict the users to delete when certain conditions are not met.So i want to be able to call a javascript function telling the user that cerain condition is not met.
So how do i call a javascript function without any event trigered here.
Please help...
0
Comment
Question by:gaugeta
  • 4
  • 4
  • 2
  • +2
13 Comments
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 36967001
1. in button you can call onclick="javascript method"

in that java script method check ur bussiness

give alert message .
alert:("hi");

2.do all page through java script
<form action="#" name="f1">

for form submit.
document.forName.action="someaction";
document.forName.submit;

0
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 36967013
can you paste your code here ?
0
 

Author Comment

by:gaugeta
ID: 36967015
@dravidnsr:Thanks for the reply.
I want to call the javascript function to be called from scriplet code without any event like onclick,onsubmit,etc.
How do i achieve this.
Please help...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 36967025
<%
this scriplet code;u cannot call javascript in scriptlet
%>
u need to call javascript like this

<javascript>

function delete()
{
if(conditions met)
submit form.
}
</javascript>
0
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 36967036
<%
//javascript is client side and jsp is server side you cannot write javascript in jsp script.
if conditions are met u can redirect like this
response.sendRedirect("delete.jsp");
%>
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 36967287
>>>>>>I want to call the javascript function to be called from scriplet code without any event like onclick,onsubmit,etc.


how the browser knows when they want to call ,

 you can call the java script function from HTML code or java script only not from any other language...

because java scrpit is working in client side

java code are working from server side
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 36967289
@chaituu: as per his command you can forward to some dummy jsp nad call the java script function from body onload.
0
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 36967301
dravidnsr,

Yes you can acheive it thorugh onload .but he dont wanna use js events.may he assuming that  to mix js code in scriptlet thats not possible.
0
 
LVL 1

Accepted Solution

by:
enmacc earned 2000 total points
ID: 36967655
You change the javascript function on server, when processing http request.
<javascript>

function delete()
{
<%
//scriptlet check condition met
 if(conditions met)
 {%>
 submit form.
<% } else{ %>
alert('Condition not met');
<%}%>
</javascript>
0
 
LVL 4

Expert Comment

by:Lo-Tan
ID: 36973370
You can certainly write code that @enmacc has provided, but I don't think it's the best solution from a readability standpoint or from a maintainability standpoint.  Matter of fact, over use of scriptlet code is a bad practice.

Let's first look at what you want to do - you're trying to run JavaScript from scriptlet tags.  The main issue is that the scriptlet (JSP) code is processed on the server, and by the time the page ends up on your client's browser, it is completely gone and what remains is whatever HTML/JavaScript or anything else you wrote out dynamically with the JSP code.

Any time you feel the need for Java interaction on the fly from a web site, do not box yourself in and feel that you need to solve this using scriptlets or even JSTL and writing out dynamic JavaScript.  It's much better to write an AJAX call to your server, invoking a very simple web service, do whatever processing you needed to do on the server side, and give the client an answer.  Then use that answer to alert the user or allow the deletion to occur.

Or, if you don't want to run a web service, then surely your page has some sort of model or collection of database objects you are using for the JSP page/view.  What I would do is put a property on each model object saying whether or not the user can delete that particular entity (you obviously want to do this logic in Java, so do it while you're preparing your model).  In the page, use JSTL and just hide the delete button altogether if the condition is not met.  This solution still avoids scriptlet tags, and keeps your code cleaner.

Hope this helps.
0
 

Author Comment

by:gaugeta
ID: 37082004
@all:Thanks guys.
Will try the above and get back if i face any issues.
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 37769867
You cant get that proper value .... because java run in server side and java script is client side ....
0
 
LVL 1

Expert Comment

by:enmacc
ID: 37770028
Thank you @Lao-Tan, i approve and understand your suggest. This is (-your-) a business solution. And i think the same, is more simple from the readabilty and code maintenance.
Sometimes, in home do realisation,maybe we don't need or we don't realize a model, web service ecc, and we use.. a rapid and more difficult to read and to remember solution.
But yes,i agree, the well practice in long time period it's better. :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question