I have a project developed with JSF and Spring.
My intern has to a make a kind of 'web service' to return json that he uses to make some graphics with ajax/jquery queries.
We read that jsf was not the best stuff to make a web service style application. We make it work with jsf but we read that the best way was to make a simple servlet or to use JAX-RS.
To be simple as possible, and to make it understand the basis, we go through a simple servlet class.
So we only configure in web.xml
<servlet>
<servlet-name>ServletNormal</servlet-name>
<servlet-class>com.clb.genomic.lyon.beans.ServletNormal</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletNormal</servlet-name>
<url-pattern>/ServletNormal.do/*</url-pattern>
</servlet-mapping>
And then the intern a simple garbage code to test :
public class ServletNormal extends HttpServlet{
private static final long serialVersionUID = 123L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
String paramAuteur = request.getParameter( "projet" );
String message= "";
// Test with harcoded json
if(paramAuteur.equals("Profile")) {
message = "{\"value\":[23,58,87,115],\"Categorie\":[\"1-2013\",\"2-2013\",\"3-2013\",\"4-2013\"]}" ;
}
else if(paramAuteur.equals("Fake1")){
message = "{\"value\":[23,58,87,115,141,173,203],\"Categorie\":[\"1-2013\",\"2-2013\",\"3-2013\",\"4-2013\",\"5-2013\",\"6-2013\",\"7-2013\"]}" ;
}
else{
message = "{\"value\":[23,58,87,115,141,173,203,227,245,262,277,300],\"Categorie\":[\"1-2013\",\"2-2013\",\"3-2013\",\"4-2013\",\"5-2013\",\"6-2013\",\"7-2013\",\"8-2013\",\"9-2013\",\"10-2013\",\"11-2013\",\"12-2013\"]}" ;
}
PrintWriter out = response.getWriter();
out.print(message);
}
}
Then in ajax/jquery, we called the servlet as this : /ServletNormal.do/?projet=Profile
to get back json for graphics drawing.
This works fine...but to be sure, Is this method is clean (exept hard coded Json)? Or is it dirty and we shouldn't do as this ? Is there a better way ?