0

I want to send data between two applications by using JSON and Ajax. For the first test, i want to click on a button (in xhtml) and receive data in managedbean (in the second application).

For this, i have created :

xhtml page :

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui">
    <body>
        <ui:composition template="/templates/template.xhtml">
            <ui:define name="content">
                <h:outputScript library="js" name="test.js" />
                <h:form>
                <h:button  onclick="validate();" value="Tester" type="button"/> 
                </h:form>
            </ui:define>
        </ui:composition>
    </body>
    </html>

test.js :

function validate(){ 
    try{
        var myJSONObject = {"name":"hello","address":"xyz"};
        var toServer = "data=" + encodeURIComponent(myJSONObject);
        var request=new XMLHttpRequest();
        request.open("POST", "http://'xxLocalIPxx':8080/Project1/folderTest/TestBean", true);
        request.send(toServer);
        return true;
    }
    catch(err)
    {
    alert(err.message);
    }
};

ManagedBean TestBean :

public class TestBean extends HttpServlet{
    private static final long   serialVersionUID    = 1L;

    public TestBean() {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // PrintWriter out = response.getWriter();
        String output = request.getParameter("params");
        System.out.println("Servlet : " + output);
    }
}

But, when i click on the button in the xhtml page, i don't execute the method doGet in the managedBean. I tried to put a breakpoint in this method but it never work.

Why ?

Thanks.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
holegeek
  • 97
  • 2
  • 12

1 Answers1

3

You're mixing servlets and JSF backing beans. They have nothing to do with each other. The TestBean class which you've there is in essence a servlet, not a JSF backing bean. You cannot use it by registering it as a JSF managed bean by @ManagedBean on the class or <managed-bean> in faces-config.xml. It has to be registered as a fullworthy servlet. You can use the @WebServlet annotation on the class or the <servlet> entry in web.xml for this.

Assuming that your environment supports Servlet 3.0, just use @WebServlet to register it:

@WebServlet("/testservlet")
public class TestServlet extends HttpServlet {
    // ...
}

(here, /testservlet, is the URL pattern on which the servlet has to listen)

and, assuming that /Project1 is the context path, invoke it as

http://example.com:8080/Project1/testservlet

(it'd be easier if you test it first by entering the URL straight in browser's address bar)

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • http://example.com:8080/Project1/testservlet What it should display ? (HTTP Status 404 for me) – holegeek Sep 26 '12 at 15:58
  • It should display whatever the `doGet()` is doing. By the way, my answer assumes that you're using Servlet 3.0 and that the domain name is "example.com". May I assume that you're smart enough to know what that all implies? You should fix/change it in your real environment/config/approach whenever applicable. See also the "servlets" link in the 1st paragraph for our servlets wiki which contains more detailed examples and explanation. – BalusC Sep 26 '12 at 16:07
  • i don't use @WebServlet but the configuration for web.xml and it display error 404 when i test the url – holegeek Sep 26 '12 at 16:08
  • Then apparently either that configuration or the URL was wrong. Hard to tell based on the sparse information provided so far. – BalusC Sep 26 '12 at 16:10