cancel
Showing results for 
Search instead for 
Did you mean: 

How to get data from alfresco Java-backed webscript to SHARE Dashlet

malliswar
Champ in-the-making
Champ in-the-making
Hii Everyone,


        Recently i worked on sample javabacked webscript

which i found here– https://wiki.alfresco.com/wiki/Java-backed_Web_Scripts_Samples#SimpleWebScript.java

and now i want the result of this java-class-backed-script  to be displayed in a share dashlet

(to be more clear "I want the result inside a share-site-Dashlet")

Is there any way this can be done –if there is, please provide me a example or any reference link


Thanks  in advance 
4 REPLIES 4

s_palyukh
Star Contributor
Star Contributor
you can do it in several ways:

1) call from Share frontend javascript by using ajax request:


                      var me = this;
            var actionUrl = Alfresco.constants.PROXY_URI + "url/to/your/webscript";
            Alfresco.util.Ajax.request(
                      {
                          url: actionUrl,
                          method: Alfresco.util.Ajax.GET,
                          responseContentType: Alfresco.util.Ajax.JSON,
                          successCallback:
                          {
                             fn: me._successHandler,
                              scope: me
                          },
                          failureCallback:
                          {
                              fn: me.__failureHandler,
                              scope: me
                          },
                          scope: me,
                          noReloadOnAuthFailure: me
                     });      


2) call from Share backend js by using Surf Platform API (http://docs.alfresco.com/4.2/references/APISurf-Remote-remote.html😞


var response = remote.call("/url/to/your/webscript");


3) Also you can call your webscript from Share using Java but I believe you don't need it Smiley Happy

malliswar
Champ in-the-making
Champ in-the-making
Thanks for replay– s.palyukh

I tried to call from Share frontend javascript by using ajax request:
Still i am not able figure out why i am not geeting the response—i am new to alfresco

(The action i am performing is a row insertion into database,which is working fine but i am not getting the response from class to my html file)
can you please take a look at below code and tell me where i am doing wrong
here is my JAVA class

public class DatabaseConnect extends  AbstractWebScript{

   public void execute(WebScriptRequest request, WebScriptResponse response)
         throws IOException {
       PreparedStatement preparedStmt =null;
         Connection conn =null;
             try
          {
            // create a mysql database connection
          
            String myDriver = "com.mysql.jdbc.Driver";
            String myUrl = "jdbc:mysql://172.18.0.96:3306/test";
            Class.forName(myDriver);
             conn = DriverManager.getConnection(myUrl, "root", "root");
          
            // create a sql date object so we can use it in our INSERT statement
            //Calendar calendar = Calendar.getInstance();
            //java.sql.Date startDate = new java.sql.Date(calendar.getTime().getTime());
      
            // the mysql insert statement
            String query = " insert into testtable(empid,firstname,lastname)"
              + " values (?, ?, ?)";
      
            // create the mysql insert preparedstatement
             preparedStmt = conn.prepareStatement(query);
            int empid=Integer.parseInt(request.getParameter("empid"));
            preparedStmt.setInt (1,empid);
            preparedStmt.setString (2, request.getParameter("firstname"));
           // preparedStmt.setDate   (3, startDate);
            preparedStmt.setString(3, request.getParameter("lastname"));
            preparedStmt.execute();
         response.getWriter().write("values inserted into table");
         
         }
      catch (Exception e)
          {
            System.err.println("Got an exception!");
            System.err.println(e.getMessage());
            response.getWriter().write("values inserted into table Failed");
          }finally{
              try {
                 if(preparedStmt!=null){
               preparedStmt.close();
              }
            } catch (SQLException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }
               
               try {
                  if(conn!=null){
               conn.close();
               }
            } catch (SQLException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }              
          }
   }
and below is my front end HTML file where i am expecting the result

//<html>

//<div class="dashlet">
//  <div class="title"><font color=#0795D0><b>Insert Data into DB table</b></font></div>
// <div class="body">
//<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
//<script>
//function valuesfunction()
//{

   //var empid = document.getElementById("empid").value;
      //  var firstname = document.getElementById("firstname").value;
      //  var lastname = document.getElementById("lastname").value;
   // $.ajax({url: "http://172.18.0.96:8080/alfresco/service/demo/insert?empid='+empid+'&         //firstname="+firstname+"&lastname="+lastname, success: function(result){
               //not getting the result //                alert(result);
                                                     //    $("#message").html(result);
     }});
   }
//</script>

//<form enctype="multipart/form-data" name="tetsForm">
//<div id="message"></div>
   //  Employeeid: <input name="empid" id="empid"><br>
  //   Firstname: <input name="firstname" id="firstname"><br>
  //   Lastname: <input name="lastname" id="lastname"><br>
//   <input type="button" name="submit" value="submit" onclick="valuesfunction();">
// </form>
//</div>
//</div>


//</html>

s_palyukh
Star Contributor
Star Contributor
Code seems ok. Hard to say because code is not formatted…

You don't write to response any information if you get an error in java class. You need to do it, also you didn't write error handler in js for ajax request.

Also look at the log file ({alfresco}/tomcat/logs/catalina.out)

I hope it will help you

malliswar
Champ in-the-making
Champ in-the-making
Thank you ——s.palyukh for helping me

i figured out the error and resolved it

the error was cross origin request blocked because i was running alfresco on port 8080 and share on port 4040 – now i changed both the ports to 8080 and response is visible now

once again thanks for helping me