Site Search:

How to set up a dynamic web service with eclipse in 5 minutes

Blogger post can have javascript, which allows you to send post request. However, it can not persist data, there is neither database nor servlet for a blogger html page at the server side. In order to store anything, you need to have the user store cookies at local host or have a third-party web service provide provision service for you.

We have already went through how to store object in cookie, here we discuss how to setup a web service to provide provision service for blogger post.

Here are a few options:

Option 1. find a web service provided by a third-party.
Option 2. have a cloud provider provide the service.
Option 3. have a dynamic web service setup local, then expose the service to internet.

In this post, I will talk about Option 3.

With eclipse IDE, you really only need 5 minutes to have a web service up and running local. Exposing the local web service to internet involves



Tools you will need:


JDK 1.8 +

Latest eclipse IDE for J2EE

Apache tomcat 7+

Apache CXF 3+

SOAP UI

Step 1, Setup IDE.

Once you have all the above tools downloaded and unzipped, you need to setup eclipse to use them.

open eclipse -> Preferences  -> Java -> Installed JREs
If there's no JRE setup, you need to add one.
click Add, select standard JVM, click Next, browse to where your JRE is located.
The finish setup looks like:




open eclipse -> Preferences -> Server -> Runtime Environment
click Add, select Apache Tomcat v7.0 (which matches the version you downloaded), click Next, then
browse to where your apache-tomcat is located.
The finished setup looks like:





open eclipse -> Preferences -> Web Services -> CXF 2.x Preferences
click Add, browse to where your apache-CXF is located.
The finished setup looks like:


Now let's start the 5 minutes count down.

Step 2, create an empty dynamic web service.


select File -> New -> select others -> click Web drop down -> select Dynamic Web Project.
Now follow the wizard to finish the configuration.

Step 3, create a servlet.

Once the project is created, navigate to the src folder. select File -> New -> select others -> click Web drop down -> select Servlet. Fill the package name and the Servlet name.



Step 4. test the default servlet.

highlight the current project, click Run -> Run As -> Run On Server -> select Manually define a new server, select Tomcat v7.0 server (matches what you installed) -> click Finish

The browser url should be:
http://localhost:8080/helloworld2/hello instead of http://localhost:8080/helloworld2/

Step 5. Modify the GET method to store the query parameter in memory so that user can retrieve later.

A simple way is to store the query parameter in a static class variable.

Then you can use http://localhost:8080/helloworld2/hello to retrieve last query parameter.
use http://localhost:8080/helloworld2/hello?key=payloadvalue to store query parameter.

package com.on;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class hello
 */
@WebServlet("/hello")
public class hello extends HttpServlet {
private static final long serialVersionUID = 1L;
private static String Store = "";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public hello() {
        super();
        // TODO Auto-generated constructor stub
    }

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String payload = request.getQueryString();
if(payload == null) {
response.getWriter().append(Store);
} else {
Store = payload;
response.getWriter().append(payload + " stored");
}
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}

A fancier way is to store the user query parameter into a file so that it can survive server restart.


package com.on;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class hello
 */
@WebServlet("/hello")
public class hello extends HttpServlet {
private static final long serialVersionUID = 1L;
private final String DIR = "/tmp/store.txt";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public hello() {
        super();
        // TODO Auto-generated constructor stub
    }

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String payload = request.getQueryString();
PrintWriter w = response.getWriter();
if(payload == null) {
if(!Files.exists(Paths.get(this.DIR))) {
w.append("nothing stored yet");
} else {
try(BufferedReader br = Files.newBufferedReader(Paths.get(this.DIR))) {
w.append(br.readLine());
}
}
} else {
try(BufferedWriter bw = Files.newBufferedWriter(Paths.get(this.DIR))) {
bw.write(payload);
w.append(payload + " saved");
}
}
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}


}


There are more details such as input and output validation, Exception handling, but these example code shows the essence of how to persist data with web services.

Now we have a basic dynamic web service, next let's setup a soap web service.

Next>