Saturday, 19 September 2015

Call to Servlet with query parameters using groovy script.

Below is Java Servlet class . It has been given a functionality to make it dynamic on each request.
It contains some predefined employee  data which it sorts based on firstly on first  name, then by last name and lastly by salary. Also employee data can be send as query parameters . Server will take into account the new employee data and do sorting after considering it.


import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.web.domain.Employee;
import com.web.misc.GetSortedEmp;

public class HelloServlet extends HttpServlet {

private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException{
System.out.println("Servlet called");
PrintWriter out = response.getWriter();
//If request has emp info

String fname = request.getParameter("fname");
String lname = request.getParameter("lname");
int salary =0;
Employee web = null;
if(fname !=null){
salary = Integer.parseInt(request.getParameter("salary"));
}
if(salary !=0){
web = new Employee(fname,lname,salary);
}
List<Employee> list = GetSortedEmp.getSortedEmp();
if(web!=null){
list.add(web);
}
Collections.sort(list);
String s ="Employee in Sorted Order\n";
for(Employee e : list){
s += e+"\n";
}
out.println(s);
System.out.println("Servlet ends");
}

}

The other helper classes are as follows

package com.web.domain;
public class Employee implements Comparable<Employee> {

private String fname;
private String lname;
private int salary;
public Employee(String fname,String lname,int salary){
this.fname = fname;
this.lname = lname;
this.salary = salary;
}
public String toString(){
return this.fname+" "+this.lname+" "+this.salary;
}
@Override
public int compareTo(Employee o) {
int r = this.fname.compareTo(o.fname);
if(r!=0){
return r;
}
r = this.lname.compareTo(o.lname);
if(r!=0){
return r;
}else{
return Integer.compare(this.salary, o.salary);
}

}


}

package com.web.misc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.web.domain.Employee;

public class GetSortedEmp {
public static List<Employee> getSortedEmp(){
List<Employee> list = new ArrayList<Employee>();
list.add(new Employee("Abhishek","Saxena" , 10000));
list.add(new Employee("Abhishek","Goyal" , 20000));
list.add(new Employee("Abhishek","Goyal" , 30000));
list.add(new Employee("Amar","Yadav" , 10000));
list.add(new Employee("Ankur","M" , 10000));
Collections.sort(list);
return list;
}
}

Now comes the part of interest . Groovy script to call this service. It can be seen how simple it is 

// client.groovy file

println 'Server call started'
def base ='http://localhost:8080/WebDeploy/hello?'    // base url
def params = [fname:'Abhishek',lname:'Saxena',salary:10909]   // map containing query params
url = base + params.collect {k,v->"$k=$v"}.join("&")  // modify url as required 
println url.toURL().getText();    // do server call and return result as String
println 'Server call ends'

Below is the output post run of above groovy script

Server call started

Employee in Sorted Order
Abhishek Goyal 20000
Abhishek Goyal 30000
Abhishek Saxena 10000
Abhishek Saxena 10909
Amar Yadav 10000
Ankur M 10000


Server call ends


No comments:

Post a Comment