-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmlServlet.java
More file actions
95 lines (82 loc) · 3.22 KB
/
Copy pathXmlServlet.java
File metadata and controls
95 lines (82 loc) · 3.22 KB
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
89
90
91
92
93
94
95
package com.jagacy.totalAccess.servlet;
import java.io.OutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jagacy.totalAccess.TotalAccess;
/*****************************************************************
*
* An example XML servlet.
*
* @author Bob Preston
*
* To use this in your web application:
*
* 1) Copy totalAccess.jar and this file into your web application.
* 2) Copy xml2html.awk, xml2html.vm and nutrition.xml to the WEB-INF/totalAccess
* directory in your web application.
* 3) Add the following to web.xml:
* <servlet>
* <servlet-name>xml2html</servlet-name>
* <servlet-class>
* com.jagacy.totalAccess.servlet.XmlServlet
* </servlet-class>
* </servlet>
*
* <servlet-mapping>
* <servlet-name>xml2html</servlet-name>
* <url-pattern>/xml2html</url-pattern>
* </servlet-mapping>
*
* 4) Start your web application.
* 5) Type the following into your web browser:
* http://localhost:8080/<webapp>/xml2html
*/
public class XmlServlet extends HttpServlet {
private static final long serialVersionUID = -2122149597586470901L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
OutputStream output;
boolean attachment;
output = null;
attachment = false;
String path = request.getServletContext().getRealPath("/WEB-INF/totalAccess");
try {
// Prepare:
//int contentLength = input.available();
String fileName = "food.html";
String contentType = "text/html";
String disposition = attachment ? "attachment" : "inline";
// Init servlet response:
response.setHeader("cache-control", "no-cache, max-age=0");
response.setHeader("pragma", "no-cache");
response.setHeader("expires", "-1");
response.setDateHeader("Last-Modified", System.currentTimeMillis());
//response.setContentLength(contentLength);
response.setContentType(contentType);
response.setHeader("Content-disposition", disposition
+ "; filename=\"" + fileName + "\"");
output = response.getOutputStream();
// Invoke the Total Access engine:
TotalAccess totalAccess = new TotalAccess();
totalAccess.registerInputVariable("htmlFileName", ":stream:");
totalAccess.registerInputVariable("path", path);
totalAccess.registerOutputStream(output);
try {
totalAccess.invoke(path + "/xml2html.awk", path + "/nutrition.xml");
} catch (Throwable t) {
throw new RuntimeException(t);
}
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}