
package controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
import java.sql.SQLException;
import dto.UserDto;  //importing UserDto class of dto package ceated by us
import dao.UserDao;


public class Registration extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException,IOException,SQLException,ClassNotFoundException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        //retriving data from form entered by user
        String name= request.getParameter("name");
        String email= request.getParameter("email");
        String pwd= request.getParameter("password");
        String address= request.getParameter("address");
        
        //setting data to dto obj
        UserDto obj= new UserDto();
        obj.setName(name);
        obj.setEmail(email);
        obj.setPwd(pwd);
        obj.setAddress(address);
        
        //sending all data of user to UserDao 
        UserDao ud= new UserDao();
        int i= ud.addUser(obj);
        
        if(i>0){
            //JOptionPane.showMessageDialog(null, "Registration successfull");
            out.print("Registration successfull");
        }
        else{
            out.print("Error while registering");
        }
        
        out.close();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        }catch(Exception e){}
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        }catch(Exception e){}
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }

}
