Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import com.google.gson.Gson;
- import java.util.*;
- import java.io.*;
- import java.sql.*;
- @WebServlet("/validate")
- public class ServerValidation {
- private static final long serialVersionUID = 1L;
- Gson gson = new Gson();
- Connection conn = null;
- Statement stmt;
- public void init(ServletConfig config) throws ServletException {
- // init method is run once at the start of the servlet loading
- // This will load the driver and establish a connection
- super.init(config);
- String user = "enter_your_mysql_username_here";
- String password = "enter_your_mysql_password_here";
- String url = "jdbc:mysql://mudfoot.doc.stu.mmu.ac.uk:6306/" + user;
- // Load the database driver
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- // get a connection with the user/pass
- try {
- conn = DriverManager.getConnection(url, user, password);
- System.out.println("Sensor to DB server is up and running\n");
- System.out.println("DEBUG: Connection to database successful.");
- stmt = conn.createStatement();
- } catch (SQLException se) {
- System.out.println(se);
- }
- }
- public void destroy() {
- try {
- conn.close();
- } catch (SQLException se) {
- System.out.println(se);
- }
- }
- public ServerValidation() {
- super();
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- response.setStatus(HttpServletResponse.SC_OK);
- // Check to see whether the client is sending the tag id
- String tagParam = request.getParameter("tag");
- if (tagParam != null){
- Tag tag = gson.fromJson(tagParam, Tag.class);
- boolean valid = checktag(tag.getTagId());
- String str;
- if (valid == true)
- str = "true";
- else
- str = "false";
- Response serverResponse = new Response(str);
- String validJson = gson.toJson(serverResponse);
- PrintWriter writer = response.getWriter();
- writer.println(str);
- writer.close();
- updateLockHistory(tag, str);
- }
- }
- public void updateLockHistory(Tag tag, String valid) {
- String sql = "insert into LockHistory (tagId, lockTime, tagIsValid) VALUES (\"" + tag.getTagId() + "\", now(), \"" + valid + "\");";
- try {
- stmt.executeUpdate(sql);
- System.out.println("History added to database");
- } catch (Exception e) {
- System.out.println(e.getMessage());
- System.out.println9("History not updated");
- }
- }
- public boolean checktag(String id) {
- ResultSet rs;
- String sql = "select * from ValidTags where tagId = " + id + ";";
- try {
- rs = stmt.executeQuery(sql);
- if (rs.next() == true)
- return true;
- } catch (SQLException se) {
- System.out.println(se);
- System.out.println("\nDEBUG: Update error - see error trace above for help. ");
- return false;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement