Mercurial Hosting > luan
comparison src/luan/tools/WebServlet.java @ 76:97b03fc807ad
add HttpLib and WebServlet
git-svn-id: https://luan-java.googlecode.com/svn/trunk@77 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 15 Feb 2013 04:52:16 +0000 |
parents | |
children | 4bf3d0c0b6b9 |
comparison
equal
deleted
inserted
replaced
75:aa7538ae5fb6 | 76:97b03fc807ad |
---|---|
1 package luan.tools; | |
2 | |
3 import java.io.IOException; | |
4 import javax.servlet.ServletException; | |
5 import javax.servlet.ServletOutputStream; | |
6 import javax.servlet.http.HttpServlet; | |
7 import javax.servlet.http.HttpServletRequest; | |
8 import javax.servlet.http.HttpServletResponse; | |
9 import luan.LuanState; | |
10 import luan.LuanException; | |
11 import luan.LuanRuntimeException; | |
12 import luan.LuanFunction; | |
13 import luan.LuanElement; | |
14 import luan.interp.LuanCompiler; | |
15 import luan.lib.HttpLib; | |
16 import luan.lib.BasicLib; | |
17 import luan.lib.PackageLib; | |
18 import luan.lib.MathLib; | |
19 import luan.lib.StringLib; | |
20 import luan.lib.TableLib; | |
21 import luan.lib.HtmlLib; | |
22 | |
23 | |
24 public class WebServlet extends HttpServlet { | |
25 | |
26 public static final String HTTP_SERVER = "http_server"; | |
27 | |
28 protected void loadLibs(LuanState luan) throws LuanException { | |
29 luan.load(BasicLib.LOADER,BasicLib.NAME); | |
30 luan.load(PackageLib.LOADER,PackageLib.NAME); | |
31 luan.load(MathLib.LOADER,MathLib.NAME); | |
32 luan.load(StringLib.LOADER,StringLib.NAME); | |
33 luan.load(TableLib.LOADER,TableLib.NAME); | |
34 luan.load(HtmlLib.LOADER,HtmlLib.NAME); | |
35 } | |
36 | |
37 protected void loadLuan(LuanState luan) throws LuanException { | |
38 PackageLib.require(luan,HTTP_SERVER); | |
39 Object fn = luan.global.get(HttpLib.FN_NAME); | |
40 if( !(fn instanceof LuanFunction) ) | |
41 throw new LuanException( luan, LuanElement.JAVA, "function '"+HttpLib.FN_NAME+"' not defined" ); | |
42 } | |
43 | |
44 protected LuanState newLuanState() throws LuanException { | |
45 LuanState luan = LuanCompiler.newLuanState(); | |
46 loadLibs(luan); | |
47 loadLuan(luan); | |
48 return luan; | |
49 } | |
50 | |
51 protected LuanState getLuanState() throws LuanException { | |
52 return newLuanState(); | |
53 } | |
54 | |
55 protected void service(HttpServletRequest request,HttpServletResponse response) | |
56 throws ServletException, IOException | |
57 { | |
58 try { | |
59 LuanState luan = getLuanState(); | |
60 HttpLib.service(luan,request,response); | |
61 } catch(LuanException e) { | |
62 throw new LuanRuntimeException(e); | |
63 } | |
64 } | |
65 | |
66 } |