❮ Previous Next ❯
Mvc in JSP Program

Required Files:
⦁ login.jsp
⦁ loginBean.java         //Model Component
⦁ LoginServlet.java      //Controller Component
⦁ welcome.jsp             //View Component
⦁ error.jsp                  //View Component
javaserver page

login.jsp
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title> Insert title here</title> 
</head> 
<body> 
<form action="LoginServlet"> 
username <input type="text" name="uname"> 
password <input type="password" name="pass"> 
<input type="submit" value="login"> 
<h2> Insert title here</h2> 
</form> 
</body> 
</html>



loginbean.java
package java8s;
public class LoginBean {
private String uname, password;
public String getUname()
{
return uname;
}
public void setUname(String uname)
{
this.uname=uname;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password=password;
}
public boolean validation()
{
if(password.equals("silan"))
{
return true;
}
else
{
return false;
}
}
}

LoginServlet.java
package java8s;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();

String s1=request.getParameter("uname");
String s2=request.getParameter("pass");

LoginBean obj=new LoginBean();
obj.setUname(s1);
obj.setPassword(s2);

boolean result=obj.validation();

if(result)
{
RequestDispatcher rd=request.getRequestDispatcher("welcome.jsp");
rd.forward(request, response);
}
else
{
RequestDispatcher rd=request.getRequestDispatcher("error.jsp");
rd.forward(request, response);
    }
   }
  }

welcome.jsp
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title> Insert title here</title> 
</head> 
<body> 
<%
out.print("login is successful");
%> 
</body> 
</html>



Error.jsp
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title> Insert title here</title> 
</head> 
<body> 
<%
out.print("sry!!!invalid password");
%> 
</body> 
</html>



jsp.JPG
❮ Previous Next ❯

Follow Us




  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved