Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sandeepmchouhan111293/2a767aa56bb02d1686eeccf62aa01b12 to your computer and use it in GitHub Desktop.
Save sandeepmchouhan111293/2a767aa56bb02d1686eeccf62aa01b12 to your computer and use it in GitHub Desktop.
package com.capgemini.Form.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.naming.NamingException;
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;
import com.capgemini.Form.DTO.Person;
import com.capgemini.Form.exception.PersonException;
import com.capgemini.Form.services.IRegistrationService;
import com.capgemini.Form.services.IRegistrationServiceImpl;
/**
* Servlet implementation class RegistrationController1
*/
@WebServlet("*.mvc")
public class RegistrationController extends HttpServlet {
private static final long serialVersionUID = 1L;
public RegistrationController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
IRegistrationService service =new IRegistrationServiceImpl();
String action = request.getServletPath();
Person person = new Person();
PrintWriter out=response.getWriter();
switch (action) {
case "/addPersonForm.mvc":
System.out.println("Add person");
RequestDispatcher dispatcher = request.getRequestDispatcher("addPersonForm.jsp");
dispatcher.forward(request, response);
break;
case "/addPerson.mvc":
String fName = request.getParameter("txtfName");
String lName = request.getParameter("txtlName");
String password = request.getParameter("txtpassword");
String gender = request.getParameter("txtgender");
String skill = request.getParameter("txtskill");
String city = request.getParameter("txtcity");
person.setfName(fName);
person.setlName(lName);
person.setPassword(password);
person.setGender(gender);
person.setSkill(skill);
person.setCity(city);
try
{
service.addPerson(person);
response.sendRedirect("index.html");
}
catch (PersonException | SQLException | NamingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
case "/showall.mvc":
ArrayList<Person> myPerson=null;
try
{
myPerson = service.showall();
}
catch (PersonException | SQLException | NamingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
for(Person person1: myPerson)
{
System.out.println(" Person Data is:"+person1.getfName());
System.out.println(" Person Data is:"+person1.getlName());
System.out.println(" Person Data is:"+person1.getPassword());
System.out.println(" Person Data is:"+person1.getGender());
System.out.println(" Person Data is:"+person1.getSkill());
System.out.println(" Person Data is:"+person1.getCity());
out.println(" Id is "+person1.getfName());out.println("<br/>");
out.println("first Name is "+person1.getlName());out.println("<br/>");
out.println(" last name "+person1.getPassword());out.println("<br/>");
out.println(" gender"+person1.getGender());out.println("<br/>");
out.println("skill set "+person1.getSkill());out.println("<br/>");
out.println(" city"+person1.getCity());out.println("<br/>");
out.println("<br/>");
}
break;
default :
break;
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
==================================================================================
package com.capgemini.Form.DAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.naming.NamingException;
import com.capgemini.Form.DTO.Person;
import com.capgemini.Form.DbUtil.DbUtil;
public class RegistrationDaoImpl implements RegistrationDao
{
static Connection conn = null;
static PreparedStatement pstm = null;
@Override
public void addPerson(Person person) throws SQLException, NamingException
{
conn = DbUtil.getConnection();
pstm = conn.prepareStatement("insert into registeredusers values(?,?,?,?,?,?)");
pstm.setString(1, person.getfName());
pstm.setString(2, person.getlName());
pstm.setString(3, person.getPassword());
pstm.setString(4, person.getGender());
pstm.setString(5, person.getSkill());
pstm.setString(6, person.getCity());
pstm.executeUpdate();
}
@Override
public ArrayList<Person> showall() throws SQLException, NamingException
{
ArrayList<Person> userList = new ArrayList<>();
conn = DbUtil.getConnection();
String query = "select * from registeredusers";
try {
pstm= conn.prepareStatement(query);
ResultSet res=pstm.executeQuery();
while(res.next())
{
Person person = new Person();
person.setfName(res.getString(1));
person.setlName(res.getString(2));
person.setPassword(res.getString(3));
person.setGender(res.getString(4));
person.setSkill(res.getString(5));
person.setCity(res.getString(6));
userList.add(person);
}
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return userList;
}
}
=============================================================================
package com.capgemini.web.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SuccessPage
*/
@WebServlet("/SuccessPage")
public class SuccessPage extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SuccessPage() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
processRequest(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request,response);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//MyServletContextListener
ServletContext context=getServletContext();
String company =context.getInitParameter("company");
//MIME Types
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<body>");
if(company!=null && company.isEmpty()==false)
{
out.println("<h1>"+company+"</h1>");
}
String name=(String)getServletContext().getAttribute("username");
out.println("<p>Hello "+name+" Form Submitted successfully</p>");
out.println("</body>");
out.println("</html>");
}
}
=======================================================================================
package com.capgemini.Form.DAO;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.naming.NamingException;
import com.capgemini.Form.DTO.Person;
public interface RegistrationDao
{
public void addPerson(Person person) throws SQLException, NamingException;
public ArrayList<Person> showall() throws SQLException, NamingException;
}
==========================================================================================
package com.capgemini.web.servlets;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class MyServletContextListener implements ServletContextListener
{
//It will execute at website load
@Override
public void contextInitialized(ServletContextEvent arg0)
{
arg0.getServletContext().setInitParameter("company", "capgemini");
}
//It will execute on website unload
@Override
public void contextDestroyed(ServletContextEvent arg0)
{
// TODO Auto-generated method stub
}
}
==============================================================================================
package com.capgemini.Form.DbUtil;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DbUtil
{
public static Connection getConnection() throws NamingException, SQLException
{
InitialContext context= new InitialContext();
DataSource datasource=(DataSource) context.lookup("java:/myDS");
return datasource.getConnection();
}
}
==============================================================================================
package com.capgemini.web.servlets;
import java.io.IOException;
import java.util.ArrayList;
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;
/**
* Servlet implementation class ProcessRegisterForm
*/
@WebServlet("/ProcessRegisterForm")
public class ProcessRegisterForm extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ProcessRegisterForm() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request,response);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String name=request.getParameter("name");
String mobileno=request.getParameter("mobileno");
String email=request.getParameter("email");
String country=request.getParameter("country");
getServletContext().setAttribute("username", name);
ArrayList<String> fieldErrorMessage=new ArrayList<>();
//form Validation
if(name==null || name.isEmpty())
{
fieldErrorMessage.add("Name cannot be empty");
}
if(mobileno==null || mobileno.isEmpty())
{
fieldErrorMessage.add("Mobile number cannot be empty");
}
if(email==null || email.isEmpty())
{
fieldErrorMessage.add("Email cannot be empty");
}
if(country==null || country.isEmpty())
{
fieldErrorMessage.add("Country cannot be empty");
}
//Request Dispatcher
RequestDispatcher rd=null;
if(fieldErrorMessage.isEmpty())
{
//action when validation is success
rd=request.getRequestDispatcher("SuccessPage");
}
else
{
//action when validation is failure
request.setAttribute("errMsg", fieldErrorMessage);
rd=request.getRequestDispatcher("RegisterForm");
}
rd.forward(request, response);
}
}
=====================================================================================
package com.capgemini.web.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ErrorPage
*/
@WebServlet("/ErrorPage")
public class ErrorPage extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ErrorPage() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
processRequest(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request,response);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//Catching ArrayList
Object obj=request.getAttribute("errMsg");
ArrayList<String> errMsg=null;
//MIME Types
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<p>Form not submitted.Validation Failure</p>");
out.println("</body>");
out.println("</html>");
if(obj !=null && obj instanceof ArrayList)
{
errMsg=(ArrayList<String>) obj;
out.println("<ul>");
for(int index=0;index<errMsg.size();index++)
{
out.println("<li>"+errMsg.get(index)+"</li>");
}
out.println("</ul>");
}
}
}
=======================================================================================
package com.capgemini.Form.exception;
public class PersonException extends Exception
{
public PersonException() {
super();
}
public PersonException(String string) {
// TODO Auto-generated constructor stub
}
}
==============================================================================================
package com.capgemini.web.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class RegisterForm
*/
@WebServlet(
name="/RegisterForm" ,
urlPatterns={"RegisterForm"},
initParams={
@WebInitParam(name="countries",value="India,USA,Australia")
})
public class RegisterForm extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
processRequest(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
processRequest(request,response);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//ServletConfig
ServletConfig config=getServletConfig();
String countries=config.getInitParameter("countries");
//MyServletContextListener
ServletContext context=getServletContext();
String company =context.getInitParameter("company");
//Catching ArrayList
Object obj=request.getAttribute("errMsg");
ArrayList<String> errMsg=null;
//MIME Types
response.setContentType("text/html");
PrintWriter out=response.getWriter();
//Login Page
out.println("<html>");
out.println("<body>");
if(company!=null && company.isEmpty()==false)
{
out.println("<h1>"+company+"</h1>");
}
if(obj !=null && obj instanceof ArrayList)
{
errMsg=(ArrayList<String>) obj;
out.println("<ul>");
for(int index=0;index<errMsg.size();index++)
{
out.println("<li>"+errMsg.get(index)+"</li>");
}
out.println("</ul>");
}
out.println("<form action='ProcessRegisterForm' method='post'>");
out.println("<table>");
out.println("<tr>");
out.println("<td>Name:</td>");
out.println("<td><input type='text' name='name' /></td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Mobile No:</td>");
out.println("<td><input type='text' name='mobileno' /></td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Email</td>");
out.println("<td><input type='text' name='email' /></td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Country</td>");
out.println("<td>");
out.println("<select name='country'>");
if(countries!=null && countries.isEmpty()== false)
{
String[] countryNames=countries.split(",");
for(int index=0;index < countryNames.length;index++)
{
out.println("<option value='"+countryNames[index]+"'>"+countryNames[index]+"</option>");
}
}
out.println("</select>");
out.println("</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td><input type='submit' value='Register'/></td>");
out.println("<td><input type='reset' value='Clear'/></td>");
out.println("</tr> ");
out.println("</table>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
}
==============================================================================================
package com.capgemini.Form.services;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.naming.NamingException;
import com.capgemini.Form.DTO.Person;
import com.capgemini.Form.exception.PersonException;
public interface IRegistrationService
{
public void addPerson(Person person) throws PersonException, SQLException, NamingException;
public ArrayList<Person> showall() throws PersonException, SQLException, NamingException;
}
============================================================================================
package com.capgemini.web.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class RegisterForm
*/
@WebServlet(
name="/RegisterForm" ,
urlPatterns={"RegisterForm"},
initParams={
@WebInitParam(name="countries",value="India,USA,Australia")
})
public class RegisterForm extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
processRequest(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
processRequest(request,response);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//ServletConfig
ServletConfig config=getServletConfig();
String countries=config.getInitParameter("countries");
//MyServletContextListener
ServletContext context=getServletContext();
String company =context.getInitParameter("company");
//Catching ArrayList
Object obj=request.getAttribute("errMsg");
ArrayList<String> errMsg=null;
//MIME Types
response.setContentType("text/html");
PrintWriter out=response.getWriter();
//Login Page
out.println("<html>");
out.println("<body>");
if(company!=null && company.isEmpty()==false)
{
out.println("<h1>"+company+"</h1>");
}
if(obj !=null && obj instanceof ArrayList)
{
errMsg=(ArrayList<String>) obj;
out.println("<ul>");
for(int index=0;index<errMsg.size();index++)
{
out.println("<li>"+errMsg.get(index)+"</li>");
}
out.println("</ul>");
}
out.println("<form action='ProcessRegisterForm' method='post'>");
out.println("<table>");
out.println("<tr>");
out.println("<td>Name:</td>");
out.println("<td><input type='text' name='name' /></td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Mobile No:</td>");
out.println("<td><input type='text' name='mobileno' /></td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Email</td>");
out.println("<td><input type='text' name='email' /></td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>Country</td>");
out.println("<td>");
out.println("<select name='country'>");
if(countries!=null && countries.isEmpty()== false)
{
String[] countryNames=countries.split(",");
for(int index=0;index < countryNames.length;index++)
{
out.println("<option value='"+countryNames[index]+"'>"+countryNames[index]+"</option>");
}
}
out.println("</select>");
out.println("</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td><input type='submit' value='Register'/></td>");
out.println("<td><input type='reset' value='Clear'/></td>");
out.println("</tr> ");
out.println("</table>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
}
==============================================================================================
package com.capgemini.Form.services;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.naming.NamingException;
import com.capgemini.Form.DTO.Person;
import com.capgemini.Form.exception.PersonException;
public interface IRegistrationService
{
public void addPerson(Person person) throws PersonException, SQLException, NamingException;
public ArrayList<Person> showall() throws PersonException, SQLException, NamingException;
}
===============================================================================================
package com.capgemini.Form.services;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.naming.NamingException;
import com.capgemini.Form.DTO.Person;
import com.capgemini.Form.exception.PersonException;
public interface IRegistrationService
{
public void addPerson(Person person) throws PersonException, SQLException, NamingException;
public ArrayList<Person> showall() throws PersonException, SQLException, NamingException;
}
==============================================================================================
package com.capgemini.Form.services;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.naming.NamingException;
import com.capgemini.Form.DAO.RegistrationDao;
import com.capgemini.Form.DAO.RegistrationDaoImpl;
import com.capgemini.Form.DTO.Person;
import com.capgemini.Form.exception.PersonException;
public class IRegistrationServiceImpl implements IRegistrationService
{
RegistrationDao dao = new RegistrationDaoImpl();
@Override
public void addPerson(Person person) throws PersonException, SQLException, NamingException
{
dao.addPerson(person);
}
public ArrayList<Person> showall() throws PersonException, SQLException, NamingException
{
return dao.showall();
}
}
===============================================================================================
package com.capgemini.Form.DTO;
public class Person
{
private String fName;
private String lName;
private String password;
private String gender;
private String skill;
private String city;
public Person()
{
super();
// TODO Auto-generated constructor stub
}
public Person(String fName, String lName, String password, String gender,
String skill, String city) {
super();
this.fName = fName;
this.lName = lName;
this.password = password;
this.gender = gender;
this.skill = skill;
this.city = city;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getSkill() {
return skill;
}
public void setSkill(String skill) {
this.skill = skill;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Person [fName=" + fName + ", lName=" + lName + ", password="
+ password + ", gender=" + gender + ", skill=" + skill
+ ", city=" + city + "]";
}
}
==========================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment