-1

I am a rails dev and starting to learn Java. I'm interested in building a java web server and have it execute certain commands by url for instance:

http://localhost:8080/downloadFile?id=1000

Would tell the webserver to go download a file with the id of 1000 from another server.

Should I be using a framework like Play 2.0 or something?

Appreciate any help!

=============================================================================

For anyone looking for a simple solution:

https://github.com/perwendel/spark

Quick Example:

import static spark.Spark.*;

import spark.*;

public class HelloWorld {

   public static void main(String[] args) {

      get(new Route("/hello") {
         @Override
         public Object handle(Request request, Response response) {
            // Execute code here
            return "Hello World!";
         }
      });

   }

}

If you want to use url parameters you can simply call request.queryParams("parameternamehere"). So if I wanted to have that /hello route use ?name=john to get the name, I would need to call request.queryParams("name").

I was able to get this quickly running with IntelliJ, installed com.sparkjava:spark-core:1.0 via maven, and then just created a new java class under /src/main/java.

4

2 回答 2

1

你可以写一个servlet。然后,在 webapp 的配置中(基本上是在 web.xml 中),您将指定从 URL(例如 /downloadFile)到 servlet 的映射。这样,网络服务器(例如 Tomcat)会将请求发送到您的 servlet。

您可以查看Oracle 关于 servlet 的教程

于 2013-07-18T21:35:45.750 回答
0

我只需要做类似的事情,并使用 Dropwizard:

http://dropwizard.codahale.com/

它有一个内置的 Jetty 网络服务器,还包括 JSON 格式的 Jackson。它易于设置,易于使用,而且我启动并运行得非常快。

于 2013-07-18T21:32:58.100 回答