Check_Email.java Source code

Java tutorial

Introduction

Here is the source code for Check_Email.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import com.google.gson.Gson;
import java.io.*;
import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.System.out;
import java.sql.*;
import java.sql.DriverManager;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author visheshtalreja
 */
@WebServlet(urlPatterns = { "/Check_Email" })
public class Check_Email extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @SuppressWarnings("empty-statement")
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        String email = request.getParameter("remail").replaceAll(" ", "");
        email = email.toLowerCase();
        System.out.println(email);
        Map<String, String> options = new LinkedHashMap<String, String>();
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        try (PrintWriter out = response.getWriter()) {
            String connectionURL = "jdbc:derby://localhost:1527/WTFtask";
            try {

                Connection conn = DriverManager.getConnection(connectionURL, "IS2560", "IS2560");
                String query1 = "SELECT * FROM WTFuser where email = '" + email + "'";
                Statement st = conn.createStatement();
                ResultSet rs = st.executeQuery(query1);
                //HttpServletResponse.sendRedirect("/your/new/location.jsp")
                boolean is = rs.next();
                if (!is) {
                    options.put("valid", "true");
                    String json = new Gson().toJson(options);
                    response.getWriter().write(json);
                } else if (is) {
                    options.put("valid", "false");
                    String json = new Gson().toJson(options);
                    response.getWriter().write(json);
                }
                st.close();
                rs.close();
                conn.close();

            } catch (SQLException ex) {
                out.print("Connection Failed!");
            }
        }

    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}