Thursday, 31 July 2008

Enable an HTML Form to Submit to Multiple Locations Using Javascript

This is a simple solution to allow a single html form to submit to multiple locations.

<form name="myform" method="post" action="noscriptPage.php">
...
<input type="submit" name="submit" value="Update"
onclick="document.myform.action='updatePage.php';"/>
<input type="submit" name="submit" value="Submit"
onclick="document.myform.action='submitPage.php';"/>
</form>

Add the multiple submit buttons to the form, each with a Javascript onclick clause to change the forms action property, to the desired action page.

Easy.

Optionally you can also cater for users with Javascript disabled, by adding a default action to the form, to either go to a message page, forward page that can determine the button pressed and forward.

Wednesday, 30 July 2008

Oracle Forms: Jar Icon Images

Oracle web forms uses image files for icons, by default forms will get each image individually, wasting time and bandwidth.

If you add the images to a jar file they are compressed and are loaded at startup, so each form wouldn't need to load the images each time. It also seems like a neater solution.

This code is a windows command script for creating the icon jar file.
The three parameters need to be set for each system.
  • oh = Oracle home
  • id = Icon directory
  • jf = Jar filename
@ECHO OFF

TITLE Jar Icons

SET oh="C:\Oracle\DS10"
SET id="C:\MyApp\icons"
SET jf="C:\MyApp\icons.jar"

CD /D %id%

%oh%\jdk\bin\jar -cvf %jf% *.*

TITLE Jar Icons Complete

PAUSE

You then need to place the jar file on to your web server, so the web server can access them via a URL.
If you placed the jar file in the forms/java directory you wouldn't need a path, however mixing application and forms files isn't best practice.
The URL path to the jar file needs to be added to the archive parameter in forms/server/formsweb.cfg file, so for example:

archive=frmall.jar,myIconPath/icons.jar

or in the archive_jini parameter, if you use JInitiator

archive_jini=frmall_jinit.jar,myIconPath/icons.jar

You also need to set the imagebase parameter

imagebase=codebase

You can then reference the icons by name inside the form and they should then be displayed.

Quicker, Faster, Neater Forms Icons.. Done.

Tuesday, 29 July 2008

Oracle Forms: Web.Show_Document without Toolbars

When using web deployed Oracle Forms, it was really annoying that we just wanted to display a report or popup page in the broswer without the toolbars and buttons. They weren't necessary and they cluttered up the page. The web.show_document command didn't have an option to remove them so we came up with a little routine to get around the problem and make things a little nicer.

First we added a hidden iframe, to the page forms was running on and gave it the name "hiddenframe".

<iframe name="hiddenframe" style="display:none"></iframe>

We then created a new html page, "newWindow.html".

<html>
  <body>
    <h1>Open Page in a New Window</h1>
    <script type="text/javascript">
      var thisUrl = document.location.toString();
      var newPage = thisUrl.substring(thisUrl.indexOf("?",0)+1);
      var openWin = window.open(newPage,"_blank","resizable=yes,scrollbars=yes");
      openWin.focus();
    </script>
  </body>
</html>

This page is always called into "hiddenframe" with the report page we want to display passed as a parameter. Javascript in the "newWindow.html" page then opens a new window without toolbars and buttons and calls the desired page.

So if I wanted to open a page called myReport.html in a new window without toolbars and buttons I can call web.show_document as:

WEB.SHOW_DOCUMENT('http://myserver/newWindow.html?http://myserver/pls/myReport.html', 'hiddenframe');

The user needs popup blockers turned off for this server but this shouldn't be an issue for business applications of this type.

This is not restricted to Oracle Forms, it's so simple it can be used for any web application.
You can use this test page to test the newWindow.html page. Enter the url to open in the input box.

<html>
  <body>
    <h1>New Window Test</h1>
    URL: <input id="page" type="text" value="http://www.google.com/"/>
    <input type="button" value="Open" onclick="window.open('newWindow.html?'+document.getElementById('page').value,'hiddenframe')"/>
    <iframe name="hiddenframe" style="display:none"></iframe>
  </body>
</html>

Done...

Monday, 28 July 2008

Get URL Parameters Using Javascript

Most URL parameters are read and processed by server side code, however you can access them easily by using Javascript. Add the function from this example to your page and call it, passing it the parameter name you want to read.

<html>
<head>
<script>
function getParam(name)
{
var start=location.search.indexOf("?"+name+"=");
if (start<0)
start=location.search.indexOf("&"+name+"=");
if (start<0)
return '';
start += name.length+2;
var end=location.search.indexOf("&",start)-1;
if (end<0)
end=location.search.length;
var result=location.search.substring(start,end);
var result='';
for(var i=start;i<=end;i++)
{
var c=location.search.charAt(i);
result=result+(c=='+'?' ':c);
}
return unescape(result);
}
</script>
</head>
<body>
<script>
document.write(getParam("test1")+"<br>");
document.write(getParam("test2")+"<br>");
</script>
</body>
</html>

So to test the code call it up in your browser and add the following to the end of the URL in the address bar.

?test1=Hello&test2=World

The test page will then display the parameter values

....Done.

Thursday, 24 July 2008

Java Session Cookies v HttpSession

Having written some Java Servlet code, which stored a string in a session cookie, I then looked into how HttpSession would compare and found I can get the same results with much simpler code.

My original session cookie code:

private String mySessionValue;

private String getMySessionValue(HttpServletRequest request) {
if(mySessionValue == null) {
Cookie[] cookies = request.getCookies();
if(cookies != null) {
for(int i=0; i < cookies.length; i++) {
Cookie c = cookies[i];
if (c.getName().equals("mySessionValue")) {
mySessionValue = c.getValue();
break;
}
}
}
}
return mySessionValue;
}

private void setMySessionValue(HttpServletRequest request, HttpServletResponse response, String newCookieValue) {
mySessionValue = newCookieValue;
Cookie cookie = new Cookie("mySessionValue", newCookieValue);
response.addCookie(cookie);
}

I had several issues with the cookie version,
  • I had to loop through the cookies to find my cookie
  • As the cookie value could be referenced after it was set, I had to store it locally too, as the actual cookie would not be updated until the responce is sent.
With HttpSession the code is much much less and no need to store the value locally:

private String getMySessionValue(HttpServletRequest request) {
HttpSession session = request.getSession(true);
return (String)session.getAttribute("mySessionValue");
}

private void setMySessionValue(HttpServletRequest request, String newSessionValue) {
HttpSession session = request.getSession(true);
session.setAttribute("mySessionValue", newSessionValue);
}

As well as much less code one main advantage to HttpSession is that it handles Objects not just Strings, so arrays can be stored much easier.

HttpSession wins I think.

Wednesday, 23 July 2008

Daily Notes

Something I have found very useful over the last year is a notepad script I wrote, that creates me a new text file for each day I use it. Whether it be for little code snippets, notes taken whilst on the phone, interesting websites or whatever, the "Day Notes" shortcut on my taskbar is used alot.

I know there are many free note taking programs available but I like to keep things simple and they don't get much simplier than text files. So:

1) Create a directory where you want to keep your day note files. EG: C:\DayNotes

2) Create a text file called "DayNotes.cmd", edit it with notepad and paste in the following code and save.

@ECHO OFF

SET DAYNOTEHOME=C:\DayNotes

REM ------------------------
REM -- Set today variable

FOR /f "tokens=1,2,3 delims=/" %%A in ('echo %DATE%') DO SET TODAY=%%C-%%B-%%A.txt

IF NOT EXIST "%DAYNOTEHOME%\%TODAY%" (
ECHO %DATE% >> "%DAYNOTEHOME%\%TODAY%"
ECHO ========== >> "%DAYNOTEHOME%\%TODAY%"
)

START NOTEPAD.EXE "%DAYNOTEHOME%\%TODAY%"

3) If you want, create a shortcut and place it on your taskbar. Set the Run property to Minimized, so you don't get a command window flash up.

When you run the script it creates a text file in your day notes directory, EG: 2008-07-23.txt, if it doesn't already exist, so you can easily refer back to notes made that day.

If I can't remember when I had a particular telephone call or wrote some notes, out comes the windows search, which finds it quite quickly.


Daily Notes Script...Done.

Tuesday, 22 July 2008

Wrap Oracle PLSQL Routine

This Windows command script uses Oracle's wrap utility to wrap the contents of an entire directory. The wrapped files are placed in a sub directory is created called "Wrapped", which is created if it doesn't exist.

Copy the following code into a file called "WrapDirectory.cmd".

@ECHO OFF

TITLE Wrap Directory

SET /p src="Enter Source Directory...: "
SET ora=E:\Oracle\DB10

IF NOT EXIST %src%\Wrapped MKDIR %src%\Wrapped

TITLE Wrapping Directory

FOR %%F IN (%src%\*.sql) DO CALL %ora%\Bin\WRAP INAME=%%F ONAME=%%~dpF\Wrapped\%%~nF.plb >> %%~dpF\Wrapped\wrap.log

TITLE Wrapped Directory

PAUSE

The Oracle home directory needs to be configured to your own home, before it can be run. It can also be easily modified to wrap files other than ".sql" which it currently does.

This has been been written and tested against Oracle 10g.

PLSQL Files... Wrapped.