ShowTable of Contents
You can work with all Java API features in any ServerSide JavaScript. For example:
var javaString = new java.lang.String("this is a java string");
return javaString.contains("java");
This piece of code returns true because the string contains the word "java". It's a very basic example and is only intended to show that you can simply mix Java and JavaScript in XPage's ServerSide JavaScript.
Write your own Java code in Designer 8.5.3 and higher Simply use the new "Java" design element and create your Java class there. It's automatically included to the classpath of XPages and SSJS libraries.
Write your own Java code in Designer 8.5.2 For small code pieces, you can write Java code directly in Domino Designer. But in order to use your own Java code from inside your XPages, you need to do some preparations:
1.) Open the Package Explorer in Domino Designer with menu Window -> Show Eclipse View -> Other -> Java\Package Explorer
2.) Browser to your Notes database and open the WebContent/WEB-INF location.
3.) Create a new folder "src" there (rightclick -> new -> other -> general -> folder), so that you have the folder WebContent/Web-INF/src (you can give the folder any name, but "src" could be a good choice).
4.) Add this new "src" folder to the XPages Build Path:
- rightclick on your new "src" folder -> build path -> configure build path - click the button "add folder", select your new "src" folder
Note: in the package explorer, the WebContent/INF/src folder moves to the top after this operation.
5.) Rightlick the "src" folder and choose new -> other -> java -> package, give a package name (for example "com.youatnotes")
6.) The new package is created below the WebContent/Web-INF/src folder. Rightlick on the new package and choose new -> other -> java -> class.
Give a class name, for example "Test" and write some code, for example:
package com.youatnotes;
public class Test {
public Test() {
}
public String dummy() {
return "42";
}
}
7.) Use your own class in an XPage ServerSide JavaScript.
For example, create a label and compute it's value with this code:
importPackage(com.youatnotes)
var myTest = new Test();
return myTest.dummy();
Note: write the package name without quotes!
Using an external JAR file in Designer 8.5.4 and higher Import the JAR in the new JAR design element in Domino Designer. It's included in the classpath for all things Java inside the same NSF automatically.
Using an external JAR file in Designer 8.5.3 You have to use the Package Explorer to add the JAR file to the WebContent/Web-INF/lib folder. At least in Designer 8.5.3 it should be then accessible to XPages and SSJS libraries.
Example for getting java and using java classes from SSJS
var driverClass:java.lang.Class;
var driver:java.sql.Driver;
var connection:java.sql.Connection;
var statement:java.sql.Statement;
driverClass = java.lang.Thread.currentThread().getContextClassLoader().loadClass("org.gjt.mm.mysql.Driver");
driver = driverClass.newInstance();
java.sql.DriverManager.registerDriver(driver);
connection = java.sql.DriverManager.getConnection("jdbc:mysql://dataserver.mycompany.com/tours?user=datauser&password=datapw");
statement = connection.createStatement();
Tips for accessing Java via SSJS Be very careful with the types of parameters for Java methods. If a Java method expects a java.lang.String parameter, you cannot use a SSJS String object. You have to convert it to a java.lang.String. Example SSJS code:
var myString = "some string";
var myJavaClass = new com.julianbuss.MyClass(new java.lang.String(myString));
Every time you do something wrong with parameters in a Java call, you will simply get an Error 500 with "command not handled exception".
Sometimes, XPages input controls already deliver a Java type. For example a date input control, it delivers a java.util.Date, which you can use directly when calling a Java method. To check the type of a SSJS variable, use this code:
print("type of my SSJS variable: "+typeof myVariable);
|