Servlets and JSPs can be combined in a single web application. Therefore we will use the servlet application created in the previous section, and just modify it to show how to use JSPs.
We could simply embed the
Hello world message in the JSP file. But in practice it is often useful to call Obix code from a JSP file. Therefore we are going to first create an Obix service to get the text to be displayed, and we will then call this service from a JSP file.
To create an Obix service that returns the message, create file
se_JSP_service.osc in directory
hello_web/work/obix/source_code/hello_web/ with the following content:
service JSP_service command get_hello_message out result type:string end script o_result = "<h3>Hello world</h3>" end end end service
Now create file
hello.jsp in directory
hello_web/work/web/ with the following content:
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@page import="li_hello_04_web_application.se_JSP_service"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP hello page</title> </head> <body> <p>The following message is returned from an Obix service and displayed through a JSP:</p> <%= se_JSP_service.co_get_hello_message_command().getValue().java_value() %> </body> </html>
Compile and build your project by executing the
compile_and_build system file which is located in your project's root directory (i.e.
compile_and_build.sh on Linux and
compile_and_build.bat on Windows).
To package, deploy and run your application, proceed as described in the previous section about creating a servlet. But this time enter the following URL in your browser: