2 Replies Latest reply on Jan 5, 2010 1:06 PM by Leo van den berg

    Servlet in Seam

    Chao Cui Newbie
      I try to write a servlet in seam to extract the json from request,then convert json to object and write the object into the database, but there is a problem.
      Thank you in advance for your help.

      the servlet:

      import ...
      @Name("getJsonServlet")
      public class GetJsonServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet{
           static final long serialVersionUID = 1L;
           public GetJsonServlet(){
                super();
           }
           protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
                System.out.println("doGet");
                PrintWriter out = response.getWriter();
                CreateRecordedcon createRecordedcon = (CreateRecordedcon) Component.getInstance("createRecordedcon");
                createRecordedcon.persistRecordeconTest();
           }
      }

      the CreateRecordedcon:

      import ...
      @Name("createRecordedcon")
      public class CreateRecordedcon  extends EntityController{
           private XStream xstream;
           public CreateRecordedcon(){
                xstream = new XStream(new JettisonMappedXmlDriver());
           }
           public void persistRecordeconTest(){
                RecordedconHome rh = new RecordedconHome();

                String json = "{'list':{'recordedconList':[{'contentId':7,'contentLocation':'www.comb.cn','contentType':'Voice','tag':'aaa','APartyNo':12121,'BPartyNo':12121,'contentSms':'meiyoushemm','startDatetimeStamp':{'@class':'sql-timestamp','$':'2010-01-28 12:00:00.0'},'endDatetimeStamp':{'@class':'sql-timestamp','$':'2010-01-21 12:00:00.0'}},{'contentId':8,'contentLocation':'hahah','contentType':'Voice','tag':'cuichao','APartyNo':11,'BPartyNo':22,'contentSms':'sfasd','startDatetimeStamp':{'@class':'sql-timestamp','$':'2010-01-05 12:00:00.0'},'endDatetimeStamp':{'@class':'sql-timestamp','$':'2010-01-06 12:00:00.0'}}]}}";

                xstream.alias("recordedconList", Recordedcon.class);
                List<Recordedcon> recordedlist = (List<Recordedcon>) xstream.fromXML(json);  //json to object
                for(int i=0;i<recordedlist.size();i++){
                     Recordedcon r = recordedlist.get(i);
                     rh.setInstance(r);
                     persist(r);
                }
                this.flush();  //remove this line then it shows no error, but object will not be written into the database
           }
      }

      And then I add a line in the component.xml
      <web:context-filter url-pattern="/seam/servlet/*"/>
      Also in the web.xml:
         <servlet>
           <servlet-name>GetJsonServlet</servlet-name>
           <servlet-class>com.mydomain.conmanager.action.GetJsonServlet</servlet-class>
         </servlet>
          <servlet-mapping>
            <servlet-name>GetJsonServlet</servlet-name>
            <url-pattern>/seam/servlet/getJsonServlet</url-pattern>
         </servlet-mapping>

      When I browse the URL:http://localhost:8080/conmanager/seam/servlet/getJsonServlet
      it gets the error:
      javax.servlet.ServletException: java.lang.IllegalArgumentException: no file extension in servlet path: /seam/servlet/getJsonServlet
           org.jboss.seam.web.ExceptionFilter.endWebRequestAfterException(ExceptionFilter.java:126)
           org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:70)
           org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
           org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           org.jboss.seam.web.ContextFilter$1.process(ContextFilter.java:42)
           org.jboss.seam.servlet.ContextualHttpServletRequest.run(ContextualHttpServletRequest.java:53)
           org.jboss.seam.web.ContextFilter.doFilter(ContextFilter.java:37)
           org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
           org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
           org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:390)
           org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:517)
           org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
           org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:60)
           org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:73)
           org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
           org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

        • 1. Re: Servlet in Seam
          Ilya Dyoshin Novice

          I guess, it would be better to use RESTeasy for this purposes. And this would be in my oppinion more SEAM-way.


          regards,
          Ilya Dyoshin

          • 2. Re: Servlet in Seam
            Leo van den berg Master

            Hi,


            it seems that you're struggling with JSON as indicated in:


            Seam Thread:JsonProblemInSeamUsingResteasy


            I have RestEasy working with different Consumer and Provider types, from plain text to whatever complex type. It took me some time to figure out what was wrong, but you really need a clear undesratnding of the technology you're using. Without some basic testing, it will not work with or without Seam.


            Going back to a basic Servlet and try-to-seam-enable-annotate it is NOT the way to do this. Try a simple example first with RestEasy; download the distribution, read the included manual and samples  and create a simple project where you just do the Hello World with JSON and see how that works. If you can't get that working ask the RestEasy forum first.


            It will take some additional time, but it's really worth the effort. Believe me, i've done this for every piece of of yechnology of the Seam framework.



            Leo