Advertisement
Guest User

Untitled

a guest
Nov 29th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. import javax.servlet.ServletConfig;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7.  
  8. import com.google.gson.Gson;
  9.  
  10. import java.util.*;
  11. import java.io.*;
  12. import java.sql.*;
  13.  
  14. @WebServlet("/validate")
  15. public class ServerValidation {
  16.  
  17. private static final long serialVersionUID = 1L;
  18.  
  19. Gson gson = new Gson();
  20.  
  21. Connection conn = null;
  22. Statement stmt;
  23.  
  24. public void init(ServletConfig config) throws ServletException {
  25. // init method is run once at the start of the servlet loading
  26. // This will load the driver and establish a connection
  27. super.init(config);
  28. String user = "enter_your_mysql_username_here";
  29. String password = "enter_your_mysql_password_here";
  30.  
  31. String url = "jdbc:mysql://mudfoot.doc.stu.mmu.ac.uk:6306/" + user;
  32.  
  33. // Load the database driver
  34. try {
  35. Class.forName("com.mysql.jdbc.Driver");
  36. } catch (Exception e) {
  37. System.out.println(e.getMessage());
  38. }
  39.  
  40. // get a connection with the user/pass
  41. try {
  42. conn = DriverManager.getConnection(url, user, password);
  43. System.out.println("Sensor to DB server is up and running\n");
  44.  
  45. System.out.println("DEBUG: Connection to database successful.");
  46. stmt = conn.createStatement();
  47. } catch (SQLException se) {
  48. System.out.println(se);
  49. }
  50. }
  51.  
  52. public void destroy() {
  53. try {
  54. conn.close();
  55. } catch (SQLException se) {
  56. System.out.println(se);
  57. }
  58. }
  59.  
  60. public ServerValidation() {
  61. super();
  62. }
  63.  
  64. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  65. response.setStatus(HttpServletResponse.SC_OK);
  66.  
  67. // Check to see whether the client is sending the tag id
  68. String tagParam = request.getParameter("tag");
  69.  
  70. if (tagParam != null){
  71.  
  72. Tag tag = gson.fromJson(tagParam, Tag.class);
  73.  
  74. boolean valid = checktag(tag.getTagId());
  75. String str;
  76.  
  77. if (valid == true)
  78. str = "true";
  79. else
  80. str = "false";
  81.  
  82. Response serverResponse = new Response(str);
  83.  
  84. String validJson = gson.toJson(serverResponse);
  85.  
  86. PrintWriter writer = response.getWriter();
  87. writer.println(str);
  88. writer.close();
  89.  
  90. updateLockHistory(tag, str);
  91.  
  92. }
  93. }
  94.  
  95. public void updateLockHistory(Tag tag, String valid) {
  96. String sql = "insert into LockHistory (tagId, lockTime, tagIsValid) VALUES (\"" + tag.getTagId() + "\", now(), \"" + valid + "\");";
  97. try {
  98. stmt.executeUpdate(sql);
  99. System.out.println("History added to database");
  100. } catch (Exception e) {
  101. System.out.println(e.getMessage());
  102. System.out.println9("History not updated");
  103. }
  104. }
  105.  
  106. public boolean checktag(String id) {
  107. ResultSet rs;
  108. String sql = "select * from ValidTags where tagId = " + id + ";";
  109. try {
  110. rs = stmt.executeQuery(sql);
  111.  
  112. if (rs.next() == true)
  113. return true;
  114. } catch (SQLException se) {
  115. System.out.println(se);
  116. System.out.println("\nDEBUG: Update error - see error trace above for help. ");
  117. return false;
  118. }
  119. return false;
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement