Tuesday, June 9, 2009

Starting RPXResults Servlet

To create the RPXResults servlet, navigate in Eclipse to your project.server package. Right click on that and choose New->Class. It should prepopulate the package name for you.

For the name of the class, use RPXResults (or whatever you used in your web.xml file in the previous steps). For the superclass, use javax.servlet.http.HttpServlet.

When the class comes up, right click on it and choose Source->Override/Implement Methods. Choose doGet and doPost and click OK.

The doGet method is what is called when the servlet is loaded via an HTTP GET method. That's what RPXResults will do. Just in case they change to POST later, we'll handle that, too. In your doGet method, replace what's there with a call to doPost, looking like this:

doPost (req, resp);
Now in the doPost, we want to just make sure that we're getting a result from RXP at first. We'll worry later about putting in the processing that's needed. Put something like this in doPost:

String token = req.getParameter("token");
PrintWriter writer = resp.getWriter ();

writer.print("RPX sent this: " + token);
The getParameter call on the request gets the token paramter passed by RPX to the servlet. We then use the PrintWriter to just output the token value as our HTML page. The user will see the token value come up after a successful login.

You can test this by running the web application and logging in via RPX. You should see the token value come up in the page after you log in.

Note that this servlet nicely highlights the way servlets work. A request for a web page starts the servlet running (via a call to doGet or doPost), and the servlet simply outputs the HTML that the user should see. In our case, we didn't output HTML, but we could have.

Next we'll put in the code to complete the login processing and set up a session.

5 comments:

  1. I really hate to bother you, but I am trying to follow this tutorial and I keep getting the message that the rpxresults page is not found. I have the servlet and servlet mapping bits in the web.xml file, I have an RPXResults.java file.

    I gather at some point the token_url was supposed to match this up, I've been using http://localhost:8888/rpxresults as my token. Was I supposed to do more or have I missed something?

    ReplyDelete
  2. Make sure that's the port your server is actually running on. Half the time on a new setup for me, it's 8080, and half the time it's 8888.

    Also, make sure you can go directly to that URL in your web browser...if it doesn't work there, it won't work with RPX involved.

    ReplyDelete
  3. Thank you so much for your reply. I'm trying to learn this for school, and I'm getting the sense that I don't understand as much as I should - sigh. I appreciate you taking the time to clarify things - I realize from the length of time since your last post that you've moved on from these tutorials, so I am doubly grateful.

    With respect to the RPXResults.java page, in the method doPost(Param param) should it look like this:

    String token = req.getParameter("http://localhost:8888/rpxresults");
    PrintWriter writer = resp.getWriter ();

    writer.print("RPX sent this: " + token);

    RPXHelper rpx = new RPXHelper ("####################", "https://rpxnow.com");

    Where ##### is the stripped out api key.
    It seems not to be able to find it, even in debug mode, as though the servlet is not actually creating a page. Or should it even be able to? Or in debug mode is localhost findable?

    Regards,
    Llachlan

    ReplyDelete
  4. >req.getParameter("http://localhost:8888/rpxresults");

    That's not the spot for the localhost bit. What you're trying to do with that line is get some info that was sent to your servlet by RPX. You're not trying to access a page on your site (the code you're writing should be running *in* the page already).

    The info you want to get is the token variable, as in: req.getParameter("token");

    That token you then pass into other RPX calls (see the post about fetching the user's email from RPX for an example).

    ReplyDelete
  5. Thank you very much, between the bits you steered me back on course for, and the two bits I misunderstood, I've gotten the rpxresults page to show the token.

    Llachlan

    ReplyDelete