Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
639 views
in Technique[技术] by (71.8m points)

how to use javascript in jsp

I want to call a javascript function that returns a value and then put that value in an if statement. There are two radio buttons in the HTML and the javascript checks to see which one is clicked. After that, the JSP compares it to either 'customers' or 'company' and does the appropriate SQL Query.

Javascript:

 function corc{
    var value;

    if(document.getElementById('cust').checked){
           value='customer';
            return value;
    }else if(document.getElmentById('comp').checked){
           value='company';
           return value;
    }
 }

JSP:

if(%>corc();<%.equals("customer")){
             String sqlqueryCommand = "SELECT * from customer where login='" + v1 + "' and password='" + v2     + "'";
}else if (%>corc();<%.equals("company")){
             String sqlqueryCommand = "SELECT * from company where login='" + v1 + "' and password='" + v2     + "'";
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • You can not call JavaScript function in if statement of JSP, because JSP is executed at the server side and JavaScript is executed at client side.

  • You have to trigger event when the one of the radio button is clicked, using onclick event you can call function corc().

  • Do not write scriptlets in JSP, because scriptlets shouldn't be used in JSPs for more than a decade. Learn the JSP EL, the JSTL, and use servlet for the Java code. How to avoid Java Code in JSP-Files?

JSP code:

.......
........
//use <form> to submit values to servlet

 <input type="radio" name="radio1" onclick="handleClick(this.id);" id="customerId" />
 <input type="radio" name="radio1" onclick="handleClick(this.id);" id="companyId" />
......
.......
//use hidden field to assign table value i.e. "customer" or "company".
 <input type="hidden" name="tableValue" id="tableTextId" />  
//</form> closing form tag

onclick event I assigned handleClick function and passed this.id, parameter this.id is used to pass the id attribute of the clicked radio button.

JavaScript code:

<script type="text/javascript">
  function handleClick(clickedId)
  {
     if(clickedId == "customerId")
       document.getElementById('tableTextId').value = "customer";
     else
       document.getElementById('tableTextId').value = "company";
  }
</script>
  • When you will submit form then in servlet you can get the value of hidden field.

String tableName = request.getParameter("tableValue"); // pass the name of hidden field i.e. tableValue

  • You can further pass this tableName to query.

Related links


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...