To create a web application using Java servlets proceed as follows:
Go to the root directory of Obix.
Create a new project named hello_web by entering the following command:
on Linux, type: ./obix.sh create_project project_id:"hello_web" kind:"web" on Windows, type: obix create_project project_id:"hello_web" kind:"web"
Obix creates a directory named pr_hello_web in subdirectory projects of your Obix root directory (i.e. obix/projects/pr_hello_web)
Moreover, a number of subdirectories and files are created under pr_hello_web. To get more information about them please have a look at the readme file in the project's root directory.
To create a servlet written in Obix, create file fa_hello_servlet.osc in directory pr_hello_web/work/obix/source_code/li_hello_web/ with the following content:
factory hello_servlet type:HTTP_servlet java: "extends javax.servlet.http.HttpServlet"
%java_servlet_extension_code
command start
script
// nothing to do
end
end
command end
script
// nothing to do
end
end
command handle_request
script
i_response.string_response_writer.write ( string = "<h3>Hello world</h3>" )
end
end
endNow the servlet must be registered so that it is known to the servlet container. To do this open file web.xml in directory pr_hello_web/work/web/WEB-INF/ with your preferred text editor and insert the following XML code after the comment add your servlets here:
<servlet> <servlet-name>Hello</servlet-name> <servlet-class>li_hello_web.fa_hello_servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Hello</servlet-name> <url-pattern>/hello_servlet</url-pattern> </servlet-mapping>
Compile and build your project by executing the compile_and_build system file which is located in your projects root directory (i.e. compile_and_build.sh on Linux and compile_and_build.bat on Windows).
To package your application into a single .war file for deployment on a servlet container:
Execute the package system file in your projects root directory (i.e. package.sh on Linux and package.bat on Windows).
This command creates a single compressed .war file in your project's distribution subdirectory (i.e. pr_hello_web/distribution/hello_web.war). The file contains everything needed to execute your web application in a servlet container.
To deploy and run your web application on your PC or another PC, follow the steps below:
If not done already, install a Java servlet container (e.g. Tomcat or Jetty).
![]() | Note |
|---|---|
The following instructions refer to a Tomcat server. If you use a different server then please consult your server's documentation for instructions on how to deploy a .war file. |
Copy the .war file created in the previous step to Tomcat's webapps directory.
Start Tomcat.
Start your web browser and enter the following URL: http://127.0.0.1:8080/hello_web/hello_servlet
Hello world is displayed in your web browser.