B4J Question JRDC2 and Websocket

yaqoob

Active Member
Licensed User
Longtime User
Hello,

I am using JRDC2 to access a database using an HTML form with an action Post. It works fine, but I have to refresh the screen to get the data. When I click the submit button nothing happens. It goes to the handler but does not show the data. After refreshing the browser the data is being displayed.
I have two questions:
1- How to make the data to be displayed after pushing the submit button?
2- Is it possible to use WebSocket with JRDC2 using HTML form? Is there is an example WebSocket with JRDC2?

Thank you.
 

OliverA

Expert
Licensed User
Longtime User
database using an HTML form with an action Post
What are you POSTing too? A modified jRDC2 server (since it does not handle this out of the box)? Another web server?
 
Upvote 0

yaqoob

Active Member
Licensed User
Longtime User
There are two services. One as per the example of JRDC2 for serving data.
The second acts as client using HTML forms as in the example of the JRDC2 to display or insert data. I used post and get action in he client service , but both need to reload the HTML form.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
How are you getting the data from jRDC2 to show in your html? What do you POST to? What is the code that handles the post? What is the code the is used to serve the first HTML form?
 
Upvote 0

yaqoob

Active Member
Licensed User
Longtime User
How are you getting the data from jRDC2 to show in your html? What do you POST to? What is the code that handles the post? What is the code the is used to serve the first HTML form?

This the code for client main

B4X:
Non-UI application (console / server application)
#Region  Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

'change based on the jdbc jar file
'#AdditionalJar: mysql-connector-java-5.1.27-bin
'#AdditionalJar: postgresql-9.4.1207
#AdditionalJar: mariadb-java-client-2.4.3

Sub Process_Globals
    Public srvr As Server
    
    Public const VERSION As Float = 2.22
    Type DBCommand (Name As String, Parameters() As Object)
    Type DBResult (Tag As Object, Columns As Map, Rows As List)
    
End Sub

Sub AppStart (Args() As String)
    srvr.Initialize("")
    
    srvr.Port = 17179
    srvr.StaticFilesFolder = File.Combine(File.DirApp, "www")
    srvr.LogsFileFolder = File.Combine(File.DirApp, "logs")
    srvr.AddHandler("/pup", "Publish", False)
    'SConnectedBrowsers = srvr.CreateThreadSafeMap
    srvr.Start
    StartMessageLoop
End Sub

This the client service handler code

B4X:
'Handler class
Sub Class_Globals
    Private mreq As ServletRequest 'ignore
    Private mresp As ServletResponse 'ignore
    Public connected As Boolean
'   
    
    Private const rdcLink As String = "http://localhost:17178/rdc"
    'Private const rdcLink As String = "http://localhost:17178/rdc"
    Type DBResult1 (Tag As Object, Columns As Map, Rows As List)
    Private start As Long = DateTime.Now
    
End Sub

Public Sub Initialize
    
End Sub

Sub Handle(req As ServletRequest, resp As ServletResponse)
    
    mresp=resp
    mreq=req

    
    If req.ContentType.StartsWith("multipart/form-data") Then ' this you have to add to the from genrated
        'parse the multipart data
        
        Dim parts As Map = req.GetMultipartData(File.DirApp & "/www", 10000000)
        Dim Value(parts.Size) As String
        For i = 0 To parts.Size - 1
            Dim name As String = parts.GetKeyAt(i)
            Dim p As Part = parts.GetValueAt(i)
            
            
            
            Value(i)=p.GetValue(req.CharacterEncoding)
        Next
    End If
    Dim reqType As String = Value(1) 'This are inputs from user
    Dim reqCR As String = Value(2)
    
    
            GetCustomer(reqCR)
    
End Sub


Sub CreateCommand(Name As String, Parameters() As Object) As DBCommand
    Dim cmd As DBCommand
    cmd.Initialize
    cmd.Name = Name
    If Parameters <> Null Then cmd.Parameters = Parameters
    Return cmd
End Sub

Sub CreateRequest As DBRequestManager
    Dim req As DBRequestManager
    req.Initialize(Me, rdcLink)
    Return req
End Sub



Sub GetCustomer (CR As String)

    Dim rowlist As String
    Dim collist As List
    
    Dim req As DBRequestManager = CreateRequest
    collist.Initialize
    
    Log("this CR "&CR)
    Dim cmd As DBCommand = CreateCommand("SelectCustomer", Array(CR))
    Wait For (req.ExecuteQuery(cmd,0, Null)) JobDone(j As HttpJob)
    If j.Success Then
        req.HandleJobAsync(j,"req")
        Wait For (req) req_Result(res As DBResult1)
        For Each row() As Object In res.Rows
            For i=0 To row.Length -1 ' this will get one row
                rowlist=rowlist&row(i)&"   "
        Next
            collist.Add(rowlist)
            'mresp.Write(CRLF).Write(CRLF)' this for new line
            rowlist=""
        Next' will get new new raw
    
        For i=0 To collist.Size-1
            mresp.Write(collist.Get(i)).Write(CRLF)
        Next
    
        req.PrintTable(res)
    
Else
        Log("ERROR: " & j.ErrorMessage)
        mresp.Write("There no data  "&"ERROR: " & j.ErrorMessage)
End If

End Sub

This the HTML form code

HTML:
<!DOCTYPE html>
<html >
   <head >
      <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
      <meta name='viewport' content='width=device-width, initial-scale=1'>
      <title >Form Page: EvokeBookingPost</title>
      <meta name='generator' content='Simfatic Forms 5.0.8.459'>
      <script src='scripts/jquery-1.7.2.min.js' type='text/javascript'></script>
      <script src='scripts/jquery.sim.utils.js' type='text/javascript'></script>
      <script src='scripts/sfm_validatorv7.js' type='text/javascript'></script>
      <link rel='stylesheet' type='text/css' href='style/EvokeBookingPost.css'>
   </head>
   <body id='sfm_EvokeBookingPost_body'>
      <form id='EvokeBookingPost' class='sfm_form' novalidate='novalidate' method='post' action='http://localhost:17179/pup' enctype="multipart/form-data" accept-charset='UTF-8'>
         <div id='EvokeBookingPost_errorloc' class='error_strings' style='width:949px;text-align:left'></div>
         <div id='EvokeBookingPost_outer_div_p1' class='form_outer_div'>
            <div style='position:relative' id='EvokeBookingPost_inner_div'>
               <input type='hidden' name='sfm_form_submitted' value='yes'>
               <div id='Image_container'>
                  <img class='sfm_image_in_form' src='images/tec_203.jpg' width='234' height='72' alt>
               </div>
               <div id='heading_container' class='form_subheading'>
                  <h2 id='heading' class='form_subheading'>Welcome to Evoke System</h2>
               </div>
               <div id='type_container' class='sfm_element_container'>
                  <input type='text' name='type' id='type' class='sfm_textbox_common sfm_textbox' size='20' value>
               </div>
               <div id='label2_container' class='sfm_form_label'>
                  <label id='label2'>Service Type</label>
               </div>
               <div id='CR_container' class='sfm_element_container'>
                  <input type='text' name='CR' id='CR' class='sfm_textbox_common sfm_textbox' size='20' value>
               </div>
               <div id='label_container' class='sfm_form_label'>
                  <label id='label'>CR Number</label>
               </div>
               <div id='Submit_container' class='loading_div sfm_element_container'>
                  <input type='image' name='Submit' id='EvokeBookingPost_Submit_img' src='images/EvokeBookingList-Submit-0.png' alt='submit'>
               </div>
               <div id='box_element'></div>
            </div>
         </div>
      </form>
      <script type='text/javascript'>
$(function()
{
   sfm_show_loading_on_formsubmit('EvokeBookingPost','EvokeBookingPost_Submit_img');
});
      </script>
      <script type='text/javascript'>
var EvokeBookingPostValidator = new Validator("EvokeBookingPost");
EvokeBookingPostValidator.addValidation("type",{required:true,message:"Please fill in type"} );
EvokeBookingPostValidator.addValidation("CR",{required:true,message:"Please fill in CR"} );
      </script>
   </body>
</html>
 
Upvote 0
Top