Here, we are going to create simple dynamic web app to create Registration and Login form in servlet and jsp. We have used MySql database and this whole project follows MVC pattern.
We assume that you have basic knowledge of setting up of project in eclipse and configuring Tomcat server.
let's start...
package com.javaeexpress.controller;
import java.io.IOException;
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.javaeexpress.model.UserBean;
import com.javaeexpress.model.UserDao;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String email=request.getParameter("email");
String password=request.getParameter("password");
UserBean regbean=new UserBean();
regbean.setEmail(email);
regbean.setPassword(password);
UserDao regdao=new UserDao();
String check=regdao.validateUser(regbean);
if(check.equals("Success"))
{
String userName=regbean.getName();
request.getSession().setAttribute("Name",userName);
request.getRequestDispatcher("./Home.jsp").forward(request, response);
}
else
{
request.getRequestDispatcher("./Login.jsp").forward(request, response);
}
}
}
package com.javaeexpress.database;
import java.sql.*;
public class DBConnection {
public static Connection createconnection(){
Connection con = null;
String path="jdbc:mysql://localhost:3306/hotelmanagement?useTimezone=true&serverTimezone=UTC";
String dbusername="root";
String dbpass="";
try {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
con=DriverManager.getConnection(path,dbusername,dbpass);
}catch(Exception e)
{
e.printStackTrace();
}
return con;
}
}