HelloWorldApp.java
/***************************************************************************
Copyright 2012 Emily Estes
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***************************************************************************/
package net.metanotion.web.examples;
import net.metanotion.util.Message;
import net.metanotion.web.RequestObject;
import net.metanotion.web.SessionHandler;
import net.metanotion.web.servlets.ServerUtil;
/** This is a web app server that ALWAYS sends the string "Hello World" as a response to any
HTTP request. The "traditional" example, written for this framework. */
public final class HelloWorldApp implements SessionHandler {
/** Our app server is just another Java program. So we start up in main like normal and
initialize our app with whatever needs initializing. When we're ready to listen for requests
we use the net.metanotion.web.servlets.ServerUtil class to start up Jetty + our framework
servlet in embedded mode.
@param args The command line arguments to our application... but this application doesn't use
any command line arguments.
*/
public static void main(String[] args) {
try {
// Start the web server listening on port 8080.
// Use this class as the session handler.
ServerUtil.launchJettyServer(8080, new HelloWorldApp());
} catch (Exception e) {
e.printStackTrace();
}
}
/** The session handler dispatches messages and returns their results. Since this app ALWAYS sends
"Hello World" the web browser, we just short cut everything and return the string "Hello World".
I think this is the simplest app that sends an actual response to the client.
@param m The message object created by the dispatcher(in this case, the
{@link net.metanotion.web.concrete.DefaultDispatcher} was used since we didn't specify one).
@param ro The HTTP Request object encoding this specific HTTP request.
@return The HTTP response to send to the client.
*/
@Override public Object dispatch(Message m, RequestObject ro) {
return "Hello World";
}
}