0

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 ?

Community
  • 1
  • 1
ZheFrench
  • 1,164
  • 3
  • 22
  • 46

2 Answers2

0

I don't see much wrong with it but if you are creating a webservice it's better to use JAX-RS (eg with Jersey), because you can make use of out-of-the-box support for Restful URLs - I don't think this is enormously complicated to set up either, especially in Servlet 3.0 container like Tomcat 7.

Si Kelly
  • 703
  • 3
  • 9
0

JSF is compatible with any other Servlet you declare, as long as you take care of the address patterns. A typical JSF 2.x servlet configuration will be:

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

Which maps any .xhtml ended request against the JSF Servlet. You can, however, use any other technology for your web service. I personally tend to use Spring MVC in order to implement RESTful services. I declare it like that:

<servlet>
    <servlet-name>REST Servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/rest-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>REST Servlet</servlet-name>
    <url-pattern>/service/*</url-pattern>
</servlet-mapping>

That way, I make all the /service/* kind requests be processed by my Spring MVC REST servlet. In the same way, Spring has also good JAX and CXF integration, so you don't have to define your own Servlet.

In conclusion, don't reinvent the wheel for such a work.

See also:

Aritz
  • 30,971
  • 16
  • 136
  • 217