-
Notifications
You must be signed in to change notification settings - Fork 54
/
servlet.m
88 lines (68 loc) · 2.21 KB
/
servlet.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
% A sample Mercury servlet.
:- module servlet.
:- interface.
:- import_module io.
:- type request.
:- type response.
:- pred handle_get(request::in, response::in, io::di, io::uo) is det.
:- implementation.
:- import_module list.
:- import_module string.
handle_get(Request, Response, !IO) :-
get_request_uri(Request, URI, !IO),
set_content_type(Response, "text/html", !IO),
Msg = string.append_list([
"<html>\n",
"<head>\n",
"<title>Mercury App Engine Sample</title>\n",
"</head>\n",
"<body>\n",
"<h2>You requested the URL: ", URI, "</h2>\n",
"</body>\n",
"</html>\n"]),
write_response(Response, Msg, !IO).
%---------------------------------------------------------------------------%
:- pragma foreign_decl("Java", "
import java.io.IOException;
import javax.servlet.http.*;
").
:- pragma foreign_code("Java", "
public static class Servlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
jmercury.servlet.handle_get(req, resp);
}
}
").
:- pragma foreign_export("Java", handle_get(in, in, di, uo), "handle_get").
:- pragma foreign_type("Java", request,
"javax.servlet.http.HttpServletRequest").
:- pragma foreign_type("Java", response,
"javax.servlet.http.HttpServletResponse").
:- pred write_response(response::in, string::in, io::di, io::uo) is det.
:- pragma foreign_proc("Java",
write_response(Response::in, Str::in, _IO0::di, _IO::uo),
[promise_pure, will_not_call_mercury],
"
try {
Response.getWriter().print(Str);
}
catch (Exception e) {
throw new RuntimeException(e);
}
").
:- pred set_content_type(response::in, string::in, io::di, io::uo) is det.
:- pragma foreign_proc("Java",
set_content_type(Response::in, ContentType::in, _IO0::di, _IO::uo),
[promise_pure, will_not_call_mercury],
"
Response.setContentType(ContentType);
").
:- pred get_request_uri(request::in, string::out, io::di, io::uo) is det.
:- pragma foreign_proc("Java",
get_request_uri(Request::in, URI::out, _IO0::di, _IO::uo),
[promise_pure, will_not_call_mercury],
"
URI = Request.getRequestURI();
").
:- end_module servlet.