The obvious advantage of being able to run the same piece of JavaScript code on both the server and the client is that it enables you to use the same code base for Ajax and non-AJAX clients, and provides more flexibility. For example, if you develop JavaScript code that you don't want others to see, you can run it on a server to protect your intellectual property and minimize security risks. If you don't focus on code protection later, you can move JavaScript code to the client to improve application performance.
Page context and script context
The Java Scripting API and JavaServer Pages are two separate Java technologies that can be easily integrated. All of them can execute code in well-defined contexts. Using JSP technology, you can access a set of JSP implicit objects: PageContext, page, request, response, out, session, config, and application. In the 1th part, you've learned how to import these objects into JavaScript files that the servlet can execute. In this article, you'll learn how to apply these actions to code that a JSP page can execute.
The JavaScript engine uses different types of contexts to maintain script variables and functions defined in the application code. If you run a script that sets a variable or contains a function, and a subsequent script that executes in the same context, you can use the variables and functions of the previous script. Therefore, when processing HTTP requests, you should use a single script context, as shown in the next section.
Scripts written in the JavaScript language can access public fields and invoke any Java object. In addition, you can use the object.property syntax (instead of using the get and set methods) to obtain and modify the values of the JavaBean property. Because Java objects are easy to use in JavaScript code, the only missing is a set of custom tags that Exchange objects between the JSP page context and the JavaScript context. As you can see from this article, you can implement them in just a few lines of code.
Using server-side JavaScript code on a Web page
This section shows how to manage JavaScript contexts throughout the ajax/http request, and how to exchange variables between the JSP page context and the JavaScript context.
Using JSP objects in JavaScript
The 1th part of this series shows a Java Servlet,java Scripting API based on the Java Scripting API that can be used to execute JavaScript files on the server. This section describes a class called Jsutil that runs JavaScript snippets using the same Java scripting API when executing JSP pages. First, you need to create a Scriptenginemanager object and then get the ScriptEngine instance, as shown in Listing 1.
Listing 1. Getscriptengine () Method of Jsutil
package jsee.util;
import javax.script.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.io.*;
public class JSUtil {
private static ScriptEngine engine;
public static synchronized ScriptEngine getScriptEngine() {
if (engine == null) {
ScriptEngineManager manager = new ScriptEngineManager();
engine = manager.getEngineByName("JavaScript");
}
return engine;
}
...
}
Listing 2 contains the Createscriptcontext () method that initializes the ScriptContext instance, obtains the JSP implicit object from the page context, and sets these objects as variables for the script context. This operation allows the implicit object to be accessed through JavaScript code executed in the script context:
The PageContext object is a repository for page-wide variables that contains methods for obtaining all other implicit objects.
The Page object is an instance of the Servlet class that handles the current request.
The Request object allows you to obtain HTTP request parameters and headers.
The response object allows you to set the HTTP response header and provide a writer that uses the out identity in the JSP code.
The Out object is used for the output of the JSP page.
The Session object maintains user-related state between requests.
The Config object represents the configuration of the servlet using the JSP.
The Application object is used to store all user-shared bean instances and to obtain the initialization parameters specified in the Web.xml file.
Listing 2. Createscriptcontext () Method of Jsutil
public class Jsutil {
...
public static ScriptContext Createscriptcontext (PageContext pagecontext) {
ScriptContext ScriptContext = NE W Simplescriptcontext ();
int scope = Scriptcontext.engine_scope;
Scriptcontext.setattribute ("PageContext", PageContext, scope);
Scriptcontext.setattribute ("page", Pagecontext.getpage (), scope);
Scriptcontext.setattribute ("Request", pagecontext.getrequest (), scope);
Scriptcontext.setattribute ("Response", pagecontext.getresponse (), scope);
Scriptcontext.setattribute ("Out", pagecontext.getout (), scope);
Scriptcontext.setattribute ("Session", pagecontext.getsession (), scope);
Scriptcontext.setattribute ("config", pagecontext.getservletconfig (), scope);
Scriptcontext.setattribute ("Application",
Pagecontext.getservletcontext (), scope);
Scriptcontext.setwriter (Pagecontext.getout ());
return scriptcontext;
}
...
}
In the process of processing an HTTP request, you need to use a script context so that the JavaScript fragment can use the variables and functions defined in the last executed script. The simple way to satisfy this request is to store the script context as a property of the request object. The Getscriptcontext () method (see Listing 3) uses Jsee. ScriptContext as the property name.