Валиков Алексей Н.
Шрифт:
* текстовым значением value. Если value имеет значение null,
* текст не создается.
*/
public static Element addElement(Element parent, String name, String value) {
Element child = parent.getOwnerDocument.createElement(name);
parent.appendChild(child);
if (value != null) {
Text text = parent.getOwnerDocument.createTextNode(value);
child.appendChild(text);
}
return child;
}
/**
* Инициализация.
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
/**
* Основной метод сервлета
*/
public void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Выставляем тип содержимого
response.setContentType("text/html");
// Инициализируем выходящий поток
OutputStreamWriter o_sw =
new OutputStreamWriter(response.getOutputStream);
PrintWriter out = new PrintWriter(response.getOutputStream);
// Получаем объекты
cookie Cookie[] cookies = request.getCookies;
// Создаем выходящий документ
XMLDocument doc = new XMLDocument;
// Создаем корневой элемент
Request Element elRequest = doc.createElement("Request");
doc.appendChild(elRequest);
// Создаем элемент General
Element elGeneral = addElement(elRequest, "General", null);
// Создаем элементы, содержащие общую информацию
addElement(elGeneral, "ServerName", request.getServerName);
addElement(elGeneral, "ServerPort",
Integer.toString(request.getServerPort));
addElement(elGeneral, "RemoteAddr", request.getRemoteAddr);
addElement(elGeneral, "Protocol", request.getProtocol);
addElement(elGeneral, "Method", request.getMethod);
addElement(elGeneral, "RequestURI", request.getRequestURI);
addElement(elGeneral, "QueryString", request.getQueryString);
// Создаем элемент Param
Element elParam = addElement(elRequest, "Param", null);
// В элементе Param создаем элементы, описывающие параметры запроса
for (Enumeration e = request.getParameterNames;
e.hasMoreElements;) {
String name = e.nextElement.toString;
String[] values = request.getParameterValues(name);
// Для каждого из значений каждого из параметров
// создаем соответствующий элемент
for (int i=0; i < values.length; i++)
addElement(elParam, name, values[i]);
}
// Создаем элемент Session
Element elSession = addElement(elRequest, "Session", null);
// Получаем объект HTTP-сессии
HttpSession session = request.getSession(true);
// Получаем имена параметров сессии
String[] names = session.getValueNames;
// В элементе Session создаем по элементу
// для каждого из параметров сессии
for (int i=0; i < names.length; i++)
addElement(elSession, session.getValueNames[i],
session.getValue(session.getValueNames[i]).toString);
// Создаем элемент Cookie