How to Create MVC in Advance Java (J2EE)

In this tutorial how to create MVC structure in Advance Java (J2EE) is shown.

Code:

index.jsp

<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Login Page</title>
</head>
<body>
<form action=”ControllerServlet” method=”POST”>
Username: <input type=”text” name=”name”><br>
Password: <input type=”password” name=”pwd”><br>
<input type=”submit” name=”Login”>
</form>
</body>
</html>

 

ControllerServlet.java

package mvcdemo;

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.servlet.*;

public class ControllerServlet extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType(“text/html;charset=UTF-8”);
try (PrintWriter out = res.getWriter()) {
String name=req.getParameter(“name”);
String pwd=req.getParameter(“pwd”);

LoginBean bean=new LoginBean();
bean.setName(name);
bean.setPassword(pwd);
req.setAttribute(“bean”, bean);

boolean status=bean.validate();

if(status)
{
RequestDispatcher rd=req.getRequestDispatcher(“success.jsp”);
rd.forward(req,res);
}
else
{
RequestDispatcher rd=req.getRequestDispatcher(“fail.jsp”);
rd.forward(req,res);
}
}
}

}

 

LoginBean.java

package mvcdemo;

public class LoginBean {

private String name,pwd;

public String getName(){
return name;}

public String getPassword(){
return pwd;}

public void setName(String name){
this.name=name;}

public void setPassword(String pwd){
this.pwd=pwd;}

public boolean validate()
{
LoginDB user=new LoginDB();
boolean success=user.ValidateUser(this);
return success;
}
}

 

LoginDB.java

package mvcdemo;
import java.sql.*;

public class LoginDB {

static Connection conn=null;
static ResultSet rs=null;

public boolean ValidateUser(LoginBean bean)
{
Statement stmt=null;

String name=bean.getName();
String pwd=bean.getPassword();

String qry=”select * from user where Username='”+name+”‘ AND Password='”+pwd+”‘;”;

try{

Class.forName(“net.ucanaccess.jdbc.UcanaccessDriver”);
String url=”jdbc:ucanaccess://D:/Employee.accdb”;

conn=DriverManager.getConnection(url);

if(conn==null)
{
return false;
}

stmt=conn.createStatement();
rs=stmt.executeQuery(qry);
boolean more=rs.next();

if(!more)
{
return false;
}
else
{
return true;
}
}
catch(Exception e){}
return false;
}

}

 

success.jsp

<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<%@page import=”mvcdemo.LoginBean” %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Success Page</title>
</head>
<body>
<p> You are successfully logged in!</p><br>
<%
LoginBean bean=(LoginBean)request.getAttribute(“bean”);
out.println(“Welcome, “+bean.getName());
%>
</body>
</html>

 

fail.jsp

<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Fail Page</title>
</head>
<body>
<p>Sorry! Username or Password Incorrect</p>
<%@include file=”index.jsp”%>
</body>
</html>

 

https://youtu.be/v11-mK1fyyM

 

 

Add a Comment

Your email address will not be published. Required fields are marked *