0

I need some help regarding js function call from a javascript.

I have a servlet which checks whether the agreement number is null or not. If agreement number is not null then it will show a message box in jsp.

Servlet code is -

String agrno = request.getParameter("agrno");
System.out.println("agrno in checkcash =" + agrno);
sql = "select agrno from ColdStorage.RecieptMaster where agrno = ?";
prest = (PreparedStatement) conn.prepareStatement(sql);
prest.setString(1, agrno);
String agrid = "";
rs = prest.executeQuery();
while(rs.next())
{
    agrid = rs.getString("agrno");
    System.out.println("agrid = "+agrid);
}

if(agrid != null)
{
    // javascript call should be here.
}

and javascript code is:

Ext.widget('button', {
    renderTo: Ext.getBody()
    , text: 'Show Message'
    , handler: function () {
        Ext.Msg.show({
            //title: '',
            msg: 'Cash Receipt for specified Agreement number already exist, do you want to regenerate it? ',
            buttonText: { yes: "YES", no: "NO"},
            buttons: Ext.MessageBox.YESNO
        });
    }
});
Prakash K
  • 11,669
  • 6
  • 51
  • 109
Divyang
  • 601
  • 2
  • 9
  • 18
  • You can't do a javascript call from java servlet code, since the servlet and JSP is executed at the server side and javascript is basically a client side i.e. browser side language. What you can do is you can set a `request` or `session` attribute for `agrid` and then fetch the value of the attribute in the JSP which contains the javascript code and check `agrid` from the attribute and execute the javascript code only when the `agrid` is not `null`. Also it would help if you can go through some nice book on concepts of JSP and Servlets or some tutorials explaining the core concepts. – Prakash K May 29 '13 at 07:37
  • can i use response object here? if yes how? – Divyang May 29 '13 at 07:39
  • Yes you can. You might be using either `doPost` or `doGet` method to write your servlet code, so these methods have `request` & `response` as parameters. By the way it is not good to mix the request processing code with database layer code. – Prakash K May 29 '13 at 07:45
  • You have to be [aware of the different technologies](http://stackoverflow.com/questions/16206746/how-do-the-different-technologies-used-for-programming-webapplications-in-java-w) (JSP, Javascript) and what runs on server and client-side. – Uooo May 29 '13 at 07:55

1 Answers1

0

You could render agrid value to HTML code (to a hidden input or to a data- attribute of some tag) and then run a script on a page load which checks that parameter and displays the message accordingly. Example:

HTML/JSP:

<input id="agrid" type="hidden" value="${agrid}">

JavaScript (jQuery):

$(document).ready(function() {
   var argid = $('#agrid').val(); // 
   if (!agrid) {
      // Show the message box
   }
});
Mikhail
  • 236
  • 2
  • 4