'Class module
Sub Class_Globals
Private mreq As ServletRequest 'ignore
Private mresp As ServletResponse 'ignore
Private templates As Map
End Sub
Public Sub Initialize
templates.Initialize
End Sub
Sub Handle(req As ServletRequest, resp As ServletResponse)
Try
Log(req.RequestURI) 'handle the request based on the URL
If req.RequestURI.EndsWith("html") Then
HandleMainPage (resp, req.RequestURI)
End If
Catch
resp.Status = 404
resp.ContentType = "text/html"
Log("Error serving request: " & LastException)
resp.Write("<h1><b>Error serving request</b></h1><br>Error: " & LastException)
End Try
End Sub
' Will replace values on the page and will then send it to the user in the browser
' Response = resp
' Page = page name including file extension, For Example: index.html
'<code>HandleMainPage(resp,"index.html")</code>
Sub HandleMainPage (Response As ServletResponse, page As String)
Dim MainPage As String = GetTemplate(page) 'load the template from the assets folder - ** All web pages should be in the 'www' folder **
MainPage = MainPage.Replace("$test$", "Hello World")
Response.ContentType = SetContentType(page)
Response.Write(MainPage)
End Sub
Sub GetTemplate(Name As String) As String
If templates.ContainsKey(Name) Then Return templates.Get(Name)
Dim temp As String = File.ReadString(File.DirApp & "/www/", Name)
templates.Put(Name, temp)
Return temp
End Sub
Sub SetContentType(FileName As String)
Dim extension, ContentType As String
Dim m As Matcher = Regex.Matcher("\.([^\.]*)$", FileName) 'find the file extension
If m.Find Then
extension = m.Group(1).ToLowerCase
Log(extension)
Select extension
Case "html", "htm"
ContentType = "text/html"
Case "js"
ContentType = "text/javascript"
Case "gif", "png"
ContentType = "image/" & extension
Case "jpeg", "jpg"
ContentType = "image/jpeg"
Case "css", "xml"
ContentType = "text/" & extension
Case "ico"
ContentType = "image/vnd.microsoft.icon"
Case "txt"
ContentType = "text/plain"
Case "logfile"
ContentType = "application/octet-stream"
Case Else
ContentType = "application/octet-stream"
End Select
Return ContentType
End If
End Sub