If you are already familiar with the servlet, you do not need to create a simple server to achieve what you want. But I would like to emphasize that your needs are likely to increase rapidly, and therefore you may need to go into RESTful frameworks (for example: Spring WS, Apache CXF) along the way.
You need to register the URI and get the parameters using standard servlet technology. Perhaps you can start here: http://docs.oracle.com/cd/E13222_01/wls/docs92/webapp/configureservlet.html
Next, you need a JSON provider and serialize it (aka marshall) in JSON format. I recommend JACKSON. Take a look at this tutorial: http://www.sivalabs.in/2011/03/json-processing-using-jackson-java-json.html
Finally, your code will look something like this:
public class Func1Servlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String p1 = req.getParameter("param1"); String p2 = req.getParameter("param2");
Map the urls in your web.xml:
<servlet> <servlet-name>func1Servlet</servlet-name> <servlet-class>myservlets.func1servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>func1Servlet</servlet-name> <url-pattern>/func1/*</url-pattern> </servlet-mapping>
Keep in mind that this is pseudo code. There are many features you can do to improve it by adding some utility classes, etc.
However, as your project grows, your need for a more integrated structure becomes more apparent.
Rafa
source share