How do you prepare response page in handleURL?

In App there is an event called handleURL where I can extract url parameters and make decisions on what to present back to the user based on the query.

I have built a custom web page to respond to that url.
I have a myWebPage class that is to be the response to be displayed to the requestor.

When you create a webapp in xojo you get an app instance and a session instance…
I’m wondering why there is only one session instance rather than an instance for each session…
I would have expected to see an array of Sessions that belongs to the App.
Yet there does seem to be a notion of session at index therefore has the session already been created for me and in handle url I can pass the parameters to a method of the session or myWebPage?

Should i have derived mySession from Session and should I have set the session reference in app?

The Session class is just that: a class. An instance of it is created for every user session in your application.

Pages you create in HandleURL do not use the built-in Sessions. The built-in Sessions are reserved for Xojo WebPage apps.

HandleURL is a way to write your own response INSTEAD of using Xojo WebPages.

So is there a way in the webpage open event for it to know what parameters were passed? Could it be that simple?

Do you mean like in the querystring or when you go to show webpage?

http://myserver:8080/myservice/?name=test?id=10980?number=112234

How and when do I extract these parameters?

When you open a Xojo web app it is always going to open the single default page unless you hijack the request process and change it (Session.Open for example).

Because Xojo web is a single page application there really is only one actual page so querystring is discouraged. There are use cases if you are being redirected from somewhere else and you need those parameters. In that case you can use the RawHeaders property to parse out the querystring from the first header line.

Alternatively if you control the source application doing the linking you can change the first question mark to a pound symbol and access the Session.HashTag property.

[quote=363487:@Brian O’Brien]http://myserver:8080/myservice/?name=test?id=10980?number=112234

How and when do I extract these parameters?[/quote]

Hey Brian,

When you load a url like that in a browser, the HandleURL Event Handler in App will fire passing Request As WebRequest. You can get the parts of the request from:

  • Request.Path for the Path
  • Request.QueryString for the Get Params
  • Request.Entity for the Post Params.

From there, you can use those values as you wish! :slight_smile:

To send a response, check out the docs / example for WebApplication.HandleURL:

[quote]// Don’t override an empty request, or a session won’t start.
If Request.Path <> “” Then
// First, create the HTML answer
Request.Print “”
Request.Print "Oops! You took a wrong turn. "
Request.Print “Please put it in reverse and try again.”
Request.Print “”

// Set the status code. The default value is 404,
// but if you leave it that way, some browsers
// will show their own. A value of 200 will make
// the browser show your text instead.
Request.Status = 200

// You MUST return True if you want to override the output.
Return True
End If[/quote]

@Phillip Zedalis Nope I don’t see anything in RawHeaders.

@Hal Gumbert So I can loop through the sessions and call a method of that session to pass the session the parameters?

Assuming there is a session id as part of the url can I not call a ‘safe’ method of that session class instance?

For i As Integer = 0 To App.SessionCount - 1 if identifier = App.SessionAtIndex(i).identifier then App.SessionAtIndex(i).AcceptParameters(q) context.Session.CurrentPage = New handlerPage // Can I change this based on other parameters? return ??? end if Next Return ????

Hey Brian,

App.HandleURL doesn’t have any connection to WebPages or Sessions. But like you said, if you pass the session ID in the URL, you could insert the code you just posted within the code I posted from WebApplication.HandleURL docs page.

Something like:

[code]If Request.Path = “myservice” Then

// find the session and get some info

Request.Print theInfo
Request.Status = 200
Return True

End If[/code]

None of this is making sense…

Brian,

You might want to take a look at Aloe ( https://aloe.zone ).

Depending on what it is that you’re hoping to do, it might help to shed some light on how HandleURL works and how to make the best use of it.

  • Tim

You gotta use the right tool for the job.
If you feel like you’re bending over backwards to use Xojo, then it’s not the right tool.

I think i’m struggling to ask a simple question and getting the wrong answer because I don’t know how to make the question simple enough…

Here is the project:

Sample Project

There are one or two issues with this at the moment and that has to do with the webpage sub class wanting to know which session it belongs .

Comments please?

this project is running on port 8086

initial request

http://localhost:8086/special/?key=value?key1=value

subsequent request when button on gui pressed should be

dim url as string url = "http://localhost:8086/special/?identifier=" + identifier + "?name=bob?id=123" ShowURL(url)

I have no indication that no parameters were passed on the first invocation.
If I fudge the sessionID and put it in manually the second call works fine, but I’m confused as to how to get the session id in the web page instance.

Am I completely out to lunch?

I’m not sure if this is related to the problem, but in standard URL notation the parameters follow a question mark and are separated by ampersands.

e.g.

http://localhost:8086/special/?key=value&key1=value

Brian, as I mentioned on your other thread, please go look at Session.open. You can access the url parameters from there and set up their initial state appropriately.

@Brian O’Brien I took a look at your sample project and have fixed it up.

There were a few problems:

  1. As @Andrew Lambert mentioned, the data URL should use “&” to separate the params, “?” designates the start of the params.
  2. You weren’t setting the webpage’s identifier property so the URL had a blank one, it’s now set in the page’s Open event.
  3. Using ShowURL without it opening a new window meant you were replacing the current page with the data URL and therefore closing your session. It’s now set to open in a new window, you might need to change your pop-up blocker settings to allow it.
  4. You had the wrong control name for the listbox, that’s fixed now.

It works, but I’m very confused as to what you’re trying to accomplish.

If all you want to do is populate the list with some data, you don’t need to be using HandleSpecialURL at all, just call a function on your page that populates the listbox with data. Heck, you could do it straight from the button action if you’re in cowboy mode.

I’m guessing this is just a test app for something else and I’m just missing the point of calling back to the App with a fresh URL.

You’d be much better off using something like Xojo.Net.HTTPSocket instead of ShowURL for sending the test data, that way a new page isn’t needed.

I found the session id in the session event… I just didn’t have a WebPage instance to send the session id to…
but in the web page instance i found a session that had the id.

Thanks everyone!