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

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology, BBSR
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment

Sidhharth – November 15, 2019:

it's simply awesome for newbies in Java and other language also. java8s.com is a website which provides the details of the java from introduction to the end in a very easy way.Although it names in java but it has lots of other language materials too.it is also very clear to understand and you get to clear your concepts very easily. One can never find it boring. Once u have came past 1st chapter it will generate curiosity and u will keep moving on. I am using this website since 1 year. Whenever I am in doubts I'll prefer to open java8s.com and I find my query easily and now I feel like I am much better in Java. I would highly recommend this website to anyone who is just planning to dive into Java.This website is awesome both for beginners and for experienced professionals. This site contents lots of in-depth of programming language concepts, programs and examples also.