Creating a DB connection in global.asa with JScript - javascript

I have a problem. I have this piece of ASP code with JScript like this:
var stringa = "Driver=SQL Server;Server=xxx;Database=xxx;Uid=xxx;Pwd=xxx;";
var cn = new ActiveXObject("ADODB.Connection");
var rs = new ActiveXObject("ADODB.Recordset");
cn.Open(stringa);
var SQL = "SELECT * FROM pippo"
rs.Open(SQL, cn, 1);
// ...
rs.Close();
I want to convert this code to two files: global.asa (for the connection) and pippo.asp (for the rest of the code). On the web, I found only code for global.asa coded in VBScript, but I want to use JScript. Can you help me?

What I've done in the past is store the connection string as an Application variable set in global.asa, and create the Connection object on a page-by-page basis. The syntax for a JScript version of global.asa is going to be very similar to a VBScript version.
From the documentation:
When the application starts, the server looks in the Global.asa file
and processes the Application_OnStart function. When the application
ends, the server processes the Application_OnEnd function.
So a barebones global.asa may look like:
<script language="JScript" runat="Server">
function Application_OnStart() {
Application.Contents("connString") = "<etc...>";
}
</script>

Related

HTTP Request using Mozilla Rhino

I would like to write a JavaScript code processed with Mozilla Rhino that can do a simple HTTP GET request, which fetches a text string from a URL.
The problem is that, I couldn't find any support in Rhino to do any kind of HTTP requests. Besides, I don't have access to the Rhino instance itself, it's running via TopBraid Composer IDE for ontology modelling. I believe any idea about a simple library that I can import within my JavaScript file maybe a good solution.
Any help?
Thanks.
Okay, so it wasn't that difficult to figure it out. This one works via TopBraid Composer and without importing any JAVA libraries. Here's the answer in case anyone needs it later on.
var resourceURL = new java.net.URL(
'http://someurl');
var urlConnection = resourceURL.openConnection();
var inputStream = new java.io.InputStreamReader(urlConnection
.getInputStream());
var bufferedReader = new java.io.BufferedReader(inputStream);
var inputLine = bufferedReader.readLine();
bufferedReader.close();
var jsString = String(inputLine);
return jsString;

How to create intranet website accessing database dynamically without server or server side scripts?

Here is the problem, an Intranet webpage has to be created without the server for hosting and i have been trying to update Access database using HTML and javascript but this is not working with the below code plese help
When the below code ran from Visual studio an error which says "Microsoft JScript runtime error: Safety settings on this computer prohibit accessing a data source on another domain." is fired and the connecction was stopped. kindly help me
<script>
function removeRow(rnum) {
jQuery('#rowNum' + rnum).remove();
}
function AddRecord() {
var adoConn = new ActiveXObject("ADODB.Connection");
var adoRS = new ActiveXObject("ADODB.Recordset");
adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='\\dbName.mdb'");
//adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source='C:\sai\TrackerDB.mdb'");
adoRS.Open("Select * From sai", adoConn, 1, 3);
adoRS.AddNew;
adoRS.Fields("requester_name").value = "Quentin";
adoRS.Update;
adoRS.Close();
adoConn.Close();
}
</script>
<html>
<input onclick="AddRecord();" type="button" value="Add data" />
</html>
Just try mapping the network driver to a local driver, make sure you map it to the same letter on each computer.
Digital Chris - What he means is: you have a local HTML file with some HTML and SCRIPT and you just open it.

Does anyone knows how to import new classes when using Javascript instead of Groovy as default scripting language in SoapUI?

I have a important piece of code that modifies my requests written Javascript, but i want to add an external jar and use it from the scripts, any suggestions? It's defined a sort of import function or something like that?
In order to use your own jars in Setup Script in SOAPUI with javascript language you must do the next steps:
With SOAPUI closed, copy your jars to SOAPUI_HOME/bin/ext.
Start SOPAUI
On the project properties select Javascript for Script Language property.
Then in the Setup Script you have to reference the full package for your java classes like (I put a sample where I decode a base64 string):
java.lang.System.out.println("-----------");
java.lang.System.out.println("SAMPLE INIT");
java.lang.System.out.println("-----------\n");
var obj = new org.apache.commons.codec.binary.Base64();
var stringb64 = new java.lang.String("dGVzdA==")
var dataDecoded = obj.decodeBase64(stringb64.getBytes());
java.lang.System.out.println("RESULT " + new java.lang.String(dataDecoded) + "\n");
java.lang.System.out.println("-----------");
java.lang.System.out.println("SAMPLE END");
java.lang.System.out.println("-----------");
If you run soapui from .bat you can see the system out on the cmd:

WP8 App misbehaving due to StreamWrite in JavaScript

I would like to save the results calculated on html page in a textfile using javascript.
<script type="text/javascript">
window.onload = function () {
var sw : StreamWriter = new StreamWriter("HTML_Results.txt");
sr.Write('xyz");
*** calculations ******
sr.Write (result);
}
</script>
by doing this, my WP8 App is misbehaving and not displaying images as usual. This app is an Image Fader (calculates FPS).
Also tried:
StreamWriter sr;
try {
sr = new StreamWriter("\HTML5\HTMLResults.txt");
sr.Write("xyz");
File.SetAttributes("HTML5\HTMLResults.txt", FileAttributes.Hidden);
} catch(IOException ex) {
console.write ("error writing"); //handling IO
}
The aim is to:
Extract calculated values of several html pages(after getting loaded
one by one) in a single text file.
A Resultant HTML that reads this
text file and displays results in a tabular format.
Is there a better way to this job or the above can be rectified and used? Appreciate help.
Perhaps I've misunderstood your code but it looks like you're trying to write Java within JavaScript scripting tags. You cannot write Java in an HTML document. As far as I know, client-side JavaScript (which given your <script> tags is I guess what you're trying to write) can't perform the kind of file I/O operations you seem to want here.
You need to use Node JS to use JavaScript for something like that and then you're talking server-side. The closest you can get on client-side is using the new localStorage feature in HTML5 (not supported by all browsers).

How to implement printing in web application

In my desktop application, I developed text-based reporting and printing through a batch file because the printing is in bulk size and some times we have to restrict printing a single copy like Fixed Deposit Receipts etc. We are using lqdsi 5235 dot-matrix printers working file. Present desktop application process as follows:
The Batchfile is like this:
Name of the Batchfile: Dosprint.bat
Type %1 > prn
For Network printing:
Name of the Batchfile: Netprint.bat
Type %1 > \\SharedComputer\SharedPrinterName
And in application I redirect the printing as follows
Public Function printFile(ByVal mFileNamePath As String) As Boolean
Shell(Application.StartupPath & "\Printer\dosprint.bat " & mFileNamePath, AppWinStyle.Hide)
Return True
End Function
Printing value is very high and thousands of papers to be print some times. It is very safe and I can control No.of copies and everything like Fixed Deposit Receipts etc.
Help me if there any way to implement the same process in web application.
if you mean how to run your batch file from a web application, you can do something like this:
System.Diagnostics.Process.Start(file.FullName) //where file is a FileInfo class
As long as the file is reachable by your web application (for instance located inside the bin folder) and
The account on which your app is running has sufficient permissions to execute the file.
UPDATE:
The proper way to handle printing scenarios is to create a page that renders the content in a simple and easy way to be printed.
For example, using a simple table for tabulated data, use white color for most of the presentation to avoid spending print cartridges unnecesarily, pictures that matches your needs in specific sizes like A4 or letter, etc. Then you can call this function in the body tag:
<body onload="window.print();">
<!--content specially designed for proper printing -->
</body>
Thanks to Mr. SachinKumar K, for his article FILE HANDLING AT CLIENT SIDE USING JAVASCRIPT in WWW.CODEPROJECT.COM. In this article he mentioned that the website has to be added to the trusted site list so that the ActiveX object can be created and run.
To the dos print, I followed the below system to get dos based printing from my website on client systems dot-matrix printer.
Configuration on client System: (Internet Explorer/browser configuration)
Open Internet Explorer --> Tools --> Internet Options -->Security (tab).
add the SERVER URL in Trusted Sites
Note: uncheck "Required server verification (https:) for all sites in this zone (if your website is not HTTPS) to allow addition the site.
Next enable ActiveX controls and Plug-ins using Custom Level Tab on the same page
//create a batch file ex: printme.bat. And type the following command in the batch file.
Type %1 > prn
//The Batch file contain only one command line as above. You may change prn keyword to LPT1 or shared printer like \system_name\printer
//If required, Grant IIS_IUSRS, IUSR permission to the folder contains the printme.bat file to access by the browser.
Web Page Tags definition and javascript implementation:
// use PRE tag. It stores raw data (ASCII) in un-formatted manner
<pre id="predata" runat="server" style="display:none;"></pre>
<asp:Button Text="Print Report" runat="server" ID="btnprint" Width="101px" CssClass="buttonstyle" BackColor="DarkSlateGray" ForeColor="Aqua" OnClientClick="dosprint()" />
<%-- JAVA SCRIPT FOR PRINTING --%>
<script>
function dosprint () {
var fso, tempfile,mdata;
var fname = { key: 'value' };
fso = new ActiveXObject("Scripting.FileSystemObject");
function CreateTempFile(fname) {
var tfolder, tfile, tname, fname, TemporaryFolder = 2;
tfolder = fso.GetSpecialFolder(TemporaryFolder);
tname = fso.GetTempName();
fname.key = tfolder + '\\' + tname;
tfile = tfolder.CreateTextFile(tname);
return (tfile);
}
tempfile = CreateTempFile(fname);
mdata = document.getElementById('<%= predata.ClientID %>').innerText;
tempfile.writeline(mdata);
tempfile.close();
objShell = new ActiveXObject("WScript.Shell");
comspec = objShell.ExpandEnvironmentStrings("%comspec%");
objExec = objShell.Exec('c:\\temp\\printme.bat ' + fname.key);
// give double back slash to get a back slash in path
}
</script>
In the above code the PRINTME.BAT batch file is exist on client systems c:\temp directory.
The above system is worked for me.
Thanks everybody and happy coding.

Categories