import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.sql.*; import java.util.Date; import org.w3c.dom.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.stream.*; public class Foorum extends HttpServlet { String[] sqlBases = { "servlet_foorum", "servlet_foorum_users", "servlet_foorum_messages" }; String[] cells = { "id", "user", "message", "name", "date", "time", "text" }; public void doGet(HttpServletRequest kysimus, HttpServletResponse vastus) throws IOException, ServletException { boolean done = false, header = false, mobile = false; String action = "false"; HTTP h = new HTTP(); PrintWriter stdOut = vastus.getWriter(); try { ResultSet rsAll = sqlGetAll(stdOut); action = kysimus.getParameter("action"); if (action.compareTo("mobile") != 0) { h.header(vastus); header = true; } if (action.compareTo("show") == 0) h.tShow(rsAll, stdOut, stdOut, false); else if (action.compareTo("mobile") == 0) { mobile = true; h.tShow(rsAll, stdOut, stdOut, true); } else if (action.compareTo("showxml") == 0) { XML x = new XML(); ResultSet rs = sqlGetAll(stdOut); x.writeXML(rs, stdOut, stdOut); } done = true; } catch (Exception e) {} if (!header && !mobile) h.header(vastus); if (!done) { h.fInsert(stdOut); stdOut.println("
Näita: tabel"); stdOut.println("
Näita: XML"); stdOut.println("
Näita: mobiil"); } else if (!mobile) stdOut.println("
Tagasi\n"); if (!mobile) h.footer(vastus); } public void doPost(HttpServletRequest kysimus, HttpServletResponse vastus) throws IOException, ServletException { SQL s = new SQL(); HTTP h = new HTTP(); h.header(vastus); PrintWriter stdOut = vastus.getWriter(); String name = kysimus.getParameter("name"); String message = kysimus.getParameter("message"); if (name != null && message != null) { Date date = new Date(); java.sql.Date sqlDate = new java.sql.Date(date.getTime()); Time sqlTime = new Time(date.getTime()); String sqlDateTime = sqlDate.toString() + " " + sqlTime.toString(); // kui kasutajat veel andmebaasis pole, siis ta lisatakse sinna String userID = s.get(sqlBases[1], "id", "where name = '" + name + "'", stdOut); if (userID == null) s.say("insert into " + sqlBases[1] + " (name) values ('" + name + "');", stdOut); userID = s.get(sqlBases[1], "id", "where name = '" + name + "'", stdOut); // lisatakse uus teade s.say("insert into " + sqlBases[2] + " (date, text) values ('" + sqlDateTime + "', '" + message + "');", stdOut); String messageID = s.get(sqlBases[2], "id", "order by id desc", stdOut); // lisatakse andmed yldisesse andmetabelisse if (messageID != null && userID != null) s.say("insert into " + sqlBases[0] + " (user, message) values ('" + userID + "', '" + messageID + "');", stdOut); } stdOut.println("
Tagasi"); ResultSet rs = sqlGetAll(stdOut); h.tShow(rs, stdOut, stdOut, false); stdOut.println("