-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chat.java
108 lines (94 loc) · 3.94 KB
/
Chat.java
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
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.afonina;
import org.w3c.dom.Document;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.util.ArrayList;
public class Chat extends HttpServlet {
private DatabaseForXMLChat myDB = new DatabaseForXMLChat();
private ArrayList<Document> messList = new ArrayList<Document>();
private Document mainDoc = myDB.getMainDoc();
/* public void refreshHTML(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html><html>");
out.println("<head>");
out.println("<meta charset=\"UTF-8\" />");
String title = "ServletChatWithHistory";
out.println("<title>" + title + "</title>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("<h3>" + title + "</h3>");
out.println("<P>");
out.print("<form id = \"form\" action=\"/Chat/message\"");
out.println("method=POST>");
out.println("<input type=text size=20 name=testText>");
out.println("<br>");
out.println("<input type=submit>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}*/
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/xml; charset=utf-8");
try {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
mainDoc = myDB.getMainDoc();
DOMSource source = new DOMSource(mainDoc);
StreamResult result = new StreamResult(response.getWriter());
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(source, result);
} catch (Exception e) {
e.printStackTrace();
}
/*refreshHTML(request,response);
PrintWriter out = response.getWriter();
String filePath = "C:\\Users\\st032334\\IdeaProjects\\Chat\\src\\main\\java\\com\\afonina\\database.txt";
InputStream is = new FileInputStream(new File(filePath));
Reader r = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(r);
while (true) {
String s = br.readLine();
if(s!= null) {
out.println(s);
out.println("<br>");
}
else{
break;
}
}
is.close();*/
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
/*String mewMess = request.getParameter("testText");
String filePath = "C:\\Users\\st032334\\IdeaProjects\\Chat\\src\\main\\java\\com\\afonina\\database.txt";
if(mewMess!=null){
FileWriter fw = new FileWriter(filePath, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.newLine();
bw.write(mewMess);
bw.close();
fw.close();
}
doGet(request, response);*/
if (request != null){
String mewMess = request.getReader().readLine();
myDB.addMessageToDB(mewMess);
mainDoc = myDB.getMainDoc();
doGet(request, response);
}
}
}