The following lisp program doesn't work as expected - web

The followwing is my lisp code to complish a simple web server.
; 一些辅助函数
(require :asdf)
(defun loadlib (mod)
(asdf:oos 'asdf:load-op mod))
(defun reload ()
(load "web.lisp"))
; load 需要的库
(loadlib :html-template)
(loadlib :hunchentoot)
; 设置 hunchentoot 编码
(defvar *utf-8* (flex:make-external-format :utf-8 :eol-style :lf))
(setq hunchentoot:*hunchentoot-default-external-format* *utf-8*)
; 设置url handler 转发表
(push (hunchentoot:create-prefix-dispatcher "/hello" 'hello) hunchentoot:*dispatch-table*)
; 页面控制器函数
(defun hello ()
(setf (hunchentoot:content-type*) "text/html; charset=utf-8")
(with-output-to-string (stream)
(html-template:fill-and-print-template
#P"/home/chonglinsun/Learn/cl/lib/web/index.tmpl"
(list :name "Lisp程序员")
:stream stream)))
; 启动服务器
(defun start-web (&optional (port 4444))
(hunchentoot:start (make-instance 'hunchentoot:acceptor :port port)))
(defun restart-web ()
(progn
(reload)
(start-web)))
index.tmpl 的内容如下:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Lisp Web</title>
</head>
<body>
<h1>Lisp web开发实例</h1>
hi, <!-- TMPL_VAR name -->
</body>
</html>
When I start-web, I can not access localhost:4444/hello, but I can not firgure where
the problem is. I searched the Internet, some people said that is my because of the
path. But I do not think there is something related to it in my code. Hope there would be
someone coming tell me why. I used ubuntu 12.10.

You need to use an easy-acceptor instead of an acceptor to use the *dispatch-table* mechanism.

Related

Hamlet html not registering <head> or <title> tags correctly for my home route handler

For some reason the <head> tag for the home of my website is not being processed the way I would like. Here is the source code of my site that I get from firefox. Please note that the first head and title tags are empty:
<html><head><title></title><style>form{margin:0 auto;width:250px}</style></head><body><head><title>Fishing Weather Hub </title>
<meta name="description" content="Completely free fishing weather data provided by NOAA and Open Weather Maps. Designed to be simple and easy to use.">
<meta name="keywords" content="fishing weather,fishing,weather">
<meta name="robots" content="index,follow">
</head>
<header><center style="font-size:30px">Fishing Weather Hub</center>
...
Here is the Haskell code for this route handler. To me I have made it clear that within the head tag I would like to specify the title and meta tags. The HTML above does not seem to align with this code.
getHomeR :: Handler Html
getHomeR = do
clearSession
key <- liftIO $ MYP.randomString 9
setSession "cache_key" (T.pack key)
sess <- getSession
-- Generate the location form
(widget, enctype) <- generateFormPost $ renderDivs locationForm
defaultLayout $ do
[whamlet|
<head>
<title>Fishing Weather Hub
<meta name="description" content="Completely free fishing weather data provided by NOAA and Open Weather Maps. Designed to be simple and easy to use.">
<meta name="keywords" content="fishing weather,fishing,weather">
<meta name="robots" content="index,follow">
<header>
<center style="font-size:30px">Fishing Weather Hub
<br>
<form method=post action=#{ProcessLocationFormR} enctype=#{enctype}>
^{widget}
<button>Submit
<a href="https://www.anglerwise.com/2010/02/09/how-does-barometric-pressure-affect-fishing/">How does barometric pressure affect fishing?
|]
--CSS
toWidget
[lucius|
form {
margin: 0 auto;
width:250px;
}
|]
I do not know what the problem is.
If you just drop HTML from whamlet directly in defaultLayout, it goes in toWidget and so in the <body>. For the title in particular, use setTitle to set it. For the rest of the stuff that goes in <head> (like your meta tags), wrap their hamlet quasi-quotes in toWidgetHead.

Add a delay to a button which is also playing a sound

I have a button link which I'm using the 'onClick' event to play a sound (the correct answer to a quiz question). I then want to add a delay so the sound has enough time to play out before the link function loads the next page in.
I've tried using the set time out function, but I can't get it to work. Is this the best way to do this?
HEAD
<script>
$("#correct").click(function()
{
var url = "question2.html";
//5000 is the number of milliseconds (or 5 seconds) that you want to wait before redirection.
var delay = 5000;
setTimeout(function() {
window.location.href = url;
}, delay);
});
</script>
BODY
<button onclick="document.getElementById('answer1').play()" id="correct">A) Reduce – make less waste in the first place</button>
This is using HTML5 tags. Therefore, it will only work with browser that support HTML5. However, I think is a bad idea to validate the answer via javascript, since people can cheat and look the code and know which answer is corrected.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- javascript/jQuery -->
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
<div id='audio'>This will be replaced with an audio tag</div>
<button id="correct">A) Reduce – make less waste in the first place</button>
<script type='text/javascript'>
$("#correct").on('click',function (){
$("#audio").stop("true").delay('5000').queue(function() {
$(this).html('<audio controls="controls" autoplay="autoplay"><source src="song.mp3" type="audio/mp3" /></audio>');
});
});
</script>
</body>
</html>

Node.js and swig template engine - including template inside template

I trying to create main page (part of node.js and mongoDB application) that includes login form.
To add view part I included js files with function that returns HTML, but as I can see much better is using template engine.
Everything is OK until I including one compiled swig part inside another one.
The output of main page is OK, but login part outputs like text on the page.
How is possible to output the login HTML as HTML instead of plain text?
Does more information needed to understand the issue?
Thank you in advance.
var swig = require('swig');
var mainPage_tpl = swig.compileFile(__dirname+'/../views/mainpage_tpl.html');
var formLogin_tpl = swig.compileFile(__dirname+'/../views/login_tpl.html');
var loginOutput = formLogin_tpl();
var mainPageOutput = mainPage_tpl({
title: 'Sometitle',
pagetitle: 'Somepagetitle',
content: loginOutput
});
exports.get = function(req, res){
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(mainPageOutput);
res.end();
}
mainpage_tpl.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{{title}}</title>
<link rel="stylesheet" href="/assets/reset.css" />
<link rel="stylesheet" href="/assets/style.css" />
<script type="text/javascript" src="/assets/jquery-2.0.0.js"></script>
<script type="text/javascript" src="/assets/script.js"></script>
</head>
<body id="login_page">
<h1>{{pagetitle}}</h1>
<div id="content">{{content}}</div>
</body>
</html>
If you want to include literal HTML, you need to tell Swig to not escape it using the safe filter:
...
<div id="content">{{ content|safe }}</div>
...

How to make Y.io's global events broadcast across instances

I would like to listen for all io request in all my web pages, however, when I used the syntax below, I can ONLY listen for the io with the yui instance.
Y.on('io:success', myCallBack);
How can I write a custom method/event or something else to achieve this?
My question is actually the same as below URL:
http://yuilibrary.com/forum/viewtopic.php?p=26009
I googled for a while but did not find a practical answer for this, any help will be appreciated, thanks.
Try
(Y.io._map['io:0'] || new Y.IO()).publish({
'io:success': { broadcast: 2 },
'io:complete': { broadcast: 2 },
etc.
});
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>io.jsp</title>
<script src="/spring-test/scripts/yui_3.5.1/build/yui/yui-min.js"></script>
</head>
<body>
test
<script>
YUI().use("io-base", function(Y){
function onSuccess(transactionid,response,arguments){
alert('start!');
}
Y.Global.on('io:start',onSuccess);
});
Y1 = YUI().use("io-base", "node", function(Y1){
Y1.publish('io:start',{broadcast : 2});
Y1.io("http://www.yahoo.com.hk");
});
</script>
</body>
Finally I got one example , however it works only on FF, Chrome but fail in IE:
Please suggest if there is any improvement, thanks.

character encoding in JSF 1.1

I'm developing a tool using JSF 1.1 and I'm having this problem :
I have a String in my backing bean which is printed as:
./src.cpp: In function ‘int main()’:
./src.cpp:4: error: ‘dsdada’ was not declared in this scope
on a txt file.
But when I put it on a h:inputTextArea, it goes like this:
./src.cpp: In function ‘int main()’:
./src.cpp:4: error: ‘dsdada’ was not declared in this scope
-
<%# page contentType="text/html;charset=UTF-8" %>
and this
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
But it didn't work either.
can somebody tell me how to fix this. Thanks
/* String[0] as stdout, String[1] as stderr */
String[] results = sshBO.execCommand(cmd, timeout);
/* Done with SSH things */
sshBO.closeSession();
/* Bring the output and err to the presentation */
msg = results[1]+results[0];
FileServices.saveStringToFile("F:/myoutput.txt", msg);
msg = new String(msg.getBytes("UTF8"), "UTF8"); /* makes no difference */
on the JSP pages :
<%# page contentType="text/html;charset=UTF-8" %>
<%# taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%# taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%# taglib uri="http://sakaiproject.org/jsf/sakai" prefix="sakai" %>
<f:view >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
...
<h:inputTextarea disabled="true" value="#{SSH.msg}" styleClass="myTextArea" />
The character ‘ exist in Unicode of the bytes 0xE2 0x80 0x98. When you use the CP1252 (Windows default) encoding to encode those bytes, you get ‘.
You need to explicitly set the pageEncoding to UTF-8.
<%# page pageEncoding="UTF-8" %>
This way it will print the characters using UTF-8 and implicitly set the Content-Type header right.
In order to determine the source of your transcoding bug, inspect the data after each transcoding operation. Use a tool like this one to determine what the values should be.
For example, Java strings are being transcoded from UTF-16 (the encoding of Java strings) to UTF-8 by your JSP writer. The other transcoding operation looks like reading program output from the native system.
/* String[0] as stdout, String[1] as stderr */
String[] results = sshBO.execCommand(cmd, timeout);
For example, you can print the hex values of your strings using code like this:
for (char ch : msg.toCharArray())
System.out.format("%04x ", (int) ch);
The code point ‘ should print as 2018. Your file writing code may share a similar bug to the code that reads input, misleading you about the source of the problem.
msg = new String(msg.getBytes("UTF8"), "UTF8"); /* makes no difference */
This makes no difference because it takes UTF-16 data, transcodes it to UTF-8, then transcodes it back to UTF-16. But aside from that, if your string is corrupt, it's already too late. You're fixing the bug in the wrong place.

Resources