public static void main(String[] args) throws IOException { WebServer webServer = new WebServer("localhost", 8080); webServer.registerHandler( new RequestHandler() { @Override public String handle(String query) { // We get the current date by creating a date object without any arguments whatsoever. Date currentDate = new Date(); // We wrap the string to integer conversion into a so called exception block. // If something goes wrong a default value is set. int upperRandomLimit; try { upperRandomLimit = Integer.valueOf(query); } catch (NumberFormatException e) { upperRandomLimit = 10; } int randomNumber = new Random().nextInt(upperRandomLimit); return "Current date: " + currentDate + ". Random number in [0, " + upperRandomLimit + "): " + randomNumber; } }); webServer.startServer(); }
public static void main(String[] args) { String rootDir = WebServerConfig.DEFAULT_ROOT_DIRECTORY; int port = WebServerConfig.DEFAULT_PORT; if (args.length > 0) { rootDir = args[0]; } if (args.length > 1) { try { port = Integer.parseInt(args[1]); } catch (NumberFormatException e) { // Stick with the default value. } } try { WebServer server = new WebServer(rootDir, port); server.activate(); } catch (WebServerException e) { System.out.println( "Jibble web server \nModified by Andaru Adiwignya\nStudentID:013611" + "\nRoot Directory: Users/andaruad/git/G53SQM.Jibber/G53SQM.Jibber/webfiles\nPort: 8088"); System.out.println(e.toString()); // Print (e.toString()) } }
/** * Starts the code server with the given command line options. To shut it down, see {@link * WebServer#stop}. * * <p>Only one code server should be started at a time because the GWT compiler uses a lot of * static variables. */ public static WebServer start(Options options) throws IOException, UnableToCompleteException { if (options.getModuleNames().isEmpty()) { throw new IllegalArgumentException("Usage: at least one module must be supplied"); } PrintWriterTreeLogger logger = new PrintWriterTreeLogger(); logger.setMaxDetail(TreeLogger.Type.INFO); Modules modules = new Modules(); File workDir = ensureWorkDir(options); System.out.println("workDir: " + workDir); for (String moduleName : options.getModuleNames()) { AppSpace appSpace = AppSpace.create(new File(workDir, moduleName)); Recompiler recompiler = new Recompiler(appSpace, moduleName, options.getSourcePath(), logger); modules.addModuleState(new ModuleState(recompiler, logger)); } SourceHandler sourceHandler = new SourceHandler(modules, logger); WebServer webServer = new WebServer(sourceHandler, modules, options.getBindAddress(), options.getPort(), logger); webServer.start(); return webServer; }
/** * 启动的主函数,接受一个参数,为 web 服务器的配置文件路径。如果没有这个参数,默认在 classpath 下寻找 "web.properties" 文件。 * * <p>这个文件遵循 Nutz 多行属性文件规范,同时必须具备如下的键: * * <ul> * <li>"app-root" - 应用的根路径,比如 "~/workspace/git/danoo/strato/domain/ROOT" * <li>"app-port" - 应用监听的端口,比如 8080 * <li>"app-rs" - 应用静态资源的地址前缀,比如 "http://localhost/strato",或者 "/rs" 等 * <li>"app-classpath" - 应用的类路径,可多行 * <li>"admin-port" - 应用的管理端口,比如 8081 * </ul> * * 这个文件的例子,请参看源码 conf 目录下的 web.properties * * @param args 接受一个参数作为 web 服务器的配置文件路径 */ public static void main(String[] args) { String path = Strings.sBlank(Lang.first(args), Webs.CONF_PATH); log.infof("launch by '%s'", path); final WebServer server = new WebServer(new WebConfig(path)); server.run(); log.info("Server is down!"); }
public void join() { try { server.start(); } catch (Exception e) { e.printStackTrace(); } }
@Override public void onCreate() { Toast.makeText(this, "Creating WebServerService", Toast.LENGTH_SHORT).show(); Log.i("HTTPSERVICE", "Creating and starting httpService"); super.onCreate(); server = new WebServer(this); server.startServer(); Toast.makeText(this, "WebServerService.startServer...", Toast.LENGTH_SHORT).show(); }
private void uploadMultipart(InputStream in, int len) throws IOException { if (!new File(WebServer.TRANSFER).exists()) { return; } String fileName = "temp.bin"; headerBytes = 0; String boundary = readHeaderLine(); while (true) { String line = readHeaderLine(); if (line == null) { break; } int index = line.indexOf("filename=\""); if (index > 0) { fileName = line.substring(index + "filename=\"".length(), line.lastIndexOf('"')); } trace(" " + line); } if (!WebServer.isSimpleName(fileName)) { return; } len -= headerBytes; File file = new File(WebServer.TRANSFER, fileName); OutputStream out = new FileOutputStream(file); IOUtils.copy(in, out, len); out.close(); // remove the boundary RandomAccessFile f = new RandomAccessFile(file, "rw"); int testSize = (int) Math.min(f.length(), Constants.IO_BUFFER_SIZE); f.seek(f.length() - testSize); byte[] bytes = DataUtils.newBytes(Constants.IO_BUFFER_SIZE); f.readFully(bytes, 0, testSize); String s = new String(bytes, "ASCII"); int x = s.lastIndexOf(boundary); f.setLength(f.length() - testSize + x - 2); f.close(); }
@Override public void onDestroy() { Log.i("HTTPSERVICE", "Destroying httpService"); server.stopServer(); super.onDestroy(); }
private void __stopServer() { if (server != null) { server.stop(); server = null; } }