7 Replies Latest reply on Dec 2, 2010 12:03 AM by Tim Evers

    Creating custom Servlets in JBoss Seam

    Andre Hopkins Newbie
      I am having problems invoking my Servlet from my XHTML form.  When I click on the Submit button, my Servlet is not getting invoked at all.  Below is how I access my GUI Tasking Page and from there I want to use the Servlet to process the request, once the user presses the "Submit" button on the XHTML page.  Am I using the right url pattern in my web.xml file?  Also, I am not quite sure I have my XHTML file setup correctly, please see below.  Any feedback would be much appreciated


      web.xml file...



      component.xml file....

      <web:context-filter url-pattern="/tasking/*" />

      tasking.xhtml file....

      <form action="TaskingServlet" method="post">

      <input name="action" value="Submit" type="submit">

      TaskingServlet file...

      public class TaskingServlet extends HttpServlet {
          protected void doPost(HttpServletRequest request, HttpServletResponse response)
                  throws ServletException, IOException {
                      String action = request.getParameter("action");
                      System.out.println("User clicked the following action: " + action);

                              if (action.equalsIgnoreCase("Submit")) {
                                     // do some work

        • 1. Re: Creating custom Servlets in JBoss Seam
          Tim Evers Master

          I could be way off here as I'm not very familiar with calling servlets from the action param but, shouldn't the action of the form be /tasking/TaskingServlet as that's what the url mapping has been set to.

          • 2. Re: Creating custom Servlets in JBoss Seam
            Andre Hopkins Newbie
            You are probably right on target as I have never done this before either.  So, the action should look like this?  Also, below this, what should my action be set to?  Since the form is invoking the servlet, the doPost() method should be invoked automatically...correct?  I can't actually do this action="#{servlet.doGet(request, response)} because I don't have the request and response object at this point, and as we know, the doPost() has the HttpServletRequest, HttpServletResponse as params. 

            form action="http://localhost:8080/test-gui/tasking/TaskingServlet"  method="post">

            <h:commandButton id="tasking" value="Submit"
            • 3. Re: Creating custom Servlets in JBoss Seam
              Tim Evers Master

              You are calling your own servlet so why are you even bothering with h:commandButton? Just use a standard <input type=submit/> as you are bypassing JSF anyways aren't you?

              To be honest with you I actually find it really odd that you need this behaviour in the first place. What is this servlet for? Why couldn't this just be a standard action that you call on a Seam bean?

              There are many things about this scenario that don't make much sense to me but, I have no idea what you are doing so...I guess being confused is normal. :P

              • 4. Re: Creating custom Servlets in JBoss Seam
                Andre Hopkins Newbie

                Here is the scenario:  I am printing out rows in a dataTable and the user can change values in each one of these rows as they choose (.i.e., the rows contain input text fields, richCombo boxes, ect).  Each one of the rows in this table represents an instance of a TaskingDataObject.  Once the user makes all their selections, they hit the Submit button and I was going to use a Servlet to get all of the instances of the TaskingDataObject out of the HttpSession and process their selections even further.  My entire GUI Framework revolves around JBoss Seam/AJAX/RichFaces and using Seam Beans like you mentioned above.  BUT, I am not sure how to get all the instances of the TaskingDataObject unless I get them out of the HttpSession.  I am definitely open to ideas as I am pretty new to Seam..thanks for your guidance. 

                • 5. Re: Creating custom Servlets in JBoss Seam
                  Tim Evers Master

                  OK, after reading what you are doing I think that the servlet idea is the wrong way to approach the problem. If your entire GUI framework is using the Seam/AJAX/Richfaces stack then use it for this as well.

                  Your Seam bean should contain the list of TaskingDataObjects and this list should be used by a dataTable component or equivalent as it's value. Then you can put your inputs in this table.

                  Then you would put a commandButton on the form that would call an action method in this Seam bean that would do any extra processing you want on the items in this list.

                  A rough example here.

                  public class MySeamBean() {
                      private List<TaskingDataObjects> tasks;
                      //Call this method on the first visit to the page
                      public void setup() {
                          //Setup your tasks list here
                          tasks = SomeMethodThatCollectsAllYourTaskingDataObjects();
                      public List<TaskingDataObjects> getTasks() {return tasks;}
                      public void setTasks(List<TaskingDataObjects> list) {this.tasks = list;}
                      public void doSomething() {
                          for(TaskingDataObject task : tasks) {
                              //Do whatever processing you require

                  in your xhtml

                      <h:dataTable value="#{MySeamBean.tasks}" var="row">
                              <f:facet name="header">
                                  <h:output value="Attribute1"/>
                              <h:inputText value="#{row.attribute1}"/>
                              <f:facet name="header">
                                  <h:output value="Attribute2"/>
                              <h:selectOneMenu value="#{row.attribute2}"/>
                          <!--And so on and so on for each attribute-->
                      <h:commandButton value="DO SOMETHING" action="#{MySeamBean.doSomething()}"/>

                  I may not have completely understood your problem so if that's the case maybe you can explain why this solution wont work.


                  • 6. Re: Creating custom Servlets in JBoss Seam
                    Andre Hopkins Newbie

                       When I press the Submit button, the value="#{MySeamBean.tasks}" will have all of the modifications that the user has made to the various instances of the TaskingDataObjects?  Essentially, the List<TaskingDataObjects> tasks variable in the Backing Bean, is really like a http session object which will contain all of the modifications that the user has made on the XHTML form?  If this is correct, you are right, this will be extremely easy and make my job a LOT simpler.  Please confirm if I am on the right track and I really appreciate you conversing with me on this as you have helped me tremendously!
                    • 7. Re: Creating custom Servlets in JBoss Seam
                      Tim Evers Master

                      In answer to your question...Yes. :) Every instance in the list will be updated to whatever the users input.

                      Remember Seam with JSF is essentially binding what you see on the screen to the java objects in the back end.

                      So just like a simple inputText bound to SeamBean.username will be updated to the value the user typed in before they hit the login button the same applies to more complex types like lists or custom POJOs.

                      I would HIGHLY recommend reading the articles found in This Linke. It will really help you understand what's going on.

                      For all intents and purposes the Seam/JSF framework is really quite powerful and flexible. There are very few reasons that you would need to drop down to the servlet level. That being said I do have a couple of servlets on my app but they are mainly for content delivery of attachments and reports.