Thursday 31 March 2016

MVC IN JSP

MVC stands for Model View and Controller. It is a design patternthat separates the business logic, presentation logic and data.
Controller acts as an interface between View and Model. Controller intercepts all the incoming requests.
Model represents the state of the application i.e. data. It can also have business logic.
View represents the presentaion i.e. UI(User Interface).

Advantage of MVC (Model 2) Architecture

  1. Navigation Control is centralized
  2. Easy to maintain the large application
mvc architecture

If you new to MVC, please visit Model1 vs Model2 first.


Example of following MVC in JSP

In this example, we are using servlet as a controller, jsp as a view component, Java Bean class as a model.
In this example, we have created 5 pages:
  • index.jsp a page that gets input from the user.
  • ControllerServlet.java a servlet that acts as a controller.
  • login-success.jsp and login-error.jsp files acts as view components.
  • web.xml file for mapping the servlet.
File: index.jsp

  1. <form action="ControllerServlet" method="post">  
  2. Name:<input type="text" name="name"><br>  
  3. Password:<input type="password" name="password"><br>  
  4. <input type="submit" value="login">  
  5. </form>  
File: ControllerServlet

  1. package com.javatpoint;  
  2. import java.io.IOException;  
  3. import java.io.PrintWriter;  
  4. import javax.servlet.RequestDispatcher;  
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9. public class ControllerServlet extends HttpServlet {  
  10.     protected void doPost(HttpServletRequest request, HttpServletResponse response)  
  11.             throws ServletException, IOException {  
  12.         response.setContentType("text/html");  
  13.         PrintWriter out=response.getWriter();  
  14.           
  15.         String name=request.getParameter("name");  
  16.         String password=request.getParameter("password");  
  17.           
  18.         LoginBean bean=new LoginBean();  
  19.         bean.setName(name);  
  20.         bean.setPassword(password);  
  21.         request.setAttribute("bean",bean);  
  22.           
  23.         boolean status=bean.validate();  
  24.           
  25.         if(status){  
  26.             RequestDispatcher rd=request.getRequestDispatcher("login-success.jsp");  
  27.             rd.forward(request, response);  
  28.         }  
  29.         else{  
  30.             RequestDispatcher rd=request.getRequestDispatcher("login-error.jsp");  
  31.             rd.forward(request, response);  
  32.         }  
  33.       
  34.     }  
  35.   
  36.     @Override  
  37.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
  38.             throws ServletException, IOException {  
  39.         doPost(req, resp);  
  40.     }  
  41. }  
File: LoginBean.java

  1. package com.javatpoint;  
  2. public class LoginBean {  
  3. private String name,password;  
  4.   
  5. public String getName() {  
  6.     return name;  
  7. }  
  8. public void setName(String name) {  
  9.     this.name = name;  
  10. }  
  11. public String getPassword() {  
  12.     return password;  
  13. }  
  14. public void setPassword(String password) {  
  15.     this.password = password;  
  16. }  
  17. public boolean validate(){  
  18.     if(password.equals("admin")){  
  19.         return true;  
  20.     }  
  21.     else{  
  22.         return false;  
  23.     }  
  24. }  
  25. }  
File: login-success.jsp

  1. <%@page import="com.javatpoint.LoginBean"%>  
  2.   
  3. <p>You are successfully logged in!</p>  
  4. <%  
  5. LoginBean bean=(LoginBean)request.getAttribute("bean");  
  6. out.print("Welcome, "+bean.getName());  
  7. %>  
File: login-error.jsp

  1. <p>Sorry! username or password error</p>  
  2. <%@ include file="index.jsp" %>  
File: web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  3. xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"   
  5. id="WebApp_ID" version="3.0">  
  6.     
  7.   <servlet>  
  8.   <servlet-name>s1</servlet-name>  
  9.   <servlet-class>com.javatpoint.ControllerServlet</servlet-class>  
  10.   </servlet>  
  11.   <servlet-mapping>  
  12.   <servlet-name>s1</servlet-name>  
  13.   <url-pattern>/ControllerServlet</url-pattern>  
  14.   </servlet-mapping>  
  15. </web-app>  

Wednesday 30 March 2016

JSP:USEBEAN ACTION TAG

The jsp:useBean action tag is used to locate or instantiate a bean class. If bean object of the Bean class is already created, it doesn't create the bean depending on the scope. But if object of bean is not created, it instantiates the bean.

Syntax of jsp:useBean action tag


  1. <jsp:useBean id= "instanceName" scope= "page | request | session | application"   
  2. class"packageName.className" type= "packageName.className"  
  3. beanName="packageName.className | <%= expression >" >  
  4. </jsp:useBean>  

Attributes and Usage of jsp:useBean action tag

  1. id: is used to identify the bean in the specified scope.
  2. scope: represents the scope of the bean. It may be page, request, session or application. The default scope is page.
    • page: specifies that you can use this bean within the JSP page. The default scope is page.
    • request: specifies that you can use this bean from any JSP page that processes the same request. It has wider scope than page.
    • session: specifies that you can use this bean from any JSP page in the same session whether processes the same request or not. It has wider scope than request.
    • application: specifies that you can use this bean from any JSP page in the same application. It has wider scope than session.
  3. class: instantiates the specified bean class (i.e. creates an object of the bean class) but it must have no-arg or no constructor and must not be abstract.
  4. type: provides the bean a data type if the bean already exists in the scope. It is mainly used with class or beanName attribute. If you use it without class or beanName, no bean is instantiated.
  5. beanName: instantiates the bean using the java.beans.Beans.instantiate() method.

jsp:setProperty and jsp:getProperty action tags


The setProperty and getProperty action tags are used for developing web application with Java Bean. In web devlopment, bean class is mostly used because it is a reusable software component that represents data.
The jsp:setProperty action tag sets a property value or values in a bean using the setter method.

Displaying applet in JSP (jsp:plugin action tag)

The jsp:plugin action tag is used to embed applet in the jsp file. The jsp:plugin action tag downloads plugin at client side to execute an applet or bean.

Syntax of jsp:plugin action tag






  1. <jsp:plugin type"applet | bean" code"nameOfClassFile"   
  2. codebase"directoryNameOfClassFile"  
  3. </jsp:plugin>  


Example of displaying applet in JSP

In this example, we are simply displaying applet in jsp using the jsp:plugin tag. You must have MouseDrag.class file (an applet class file) in the current folder where jsp file resides. You may simply download this program that contains index.jsp, MouseDrag.java and MouseDrag.class files to run this application.

index.jsp






  1. <html>  
  2.     <head>  
  3.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  4.         <title>Mouse Drag</title>  
  5.     </head>  
  6.     <body bgcolor="khaki">  
  7. <h1>Mouse Drag Example</h1>  
  8.   
  9.  <jsp:plugin align="middle" height="500" width="500"  
  10.      type="applet"  code="MouseDrag.class" name="clock" codebase="."/>  
  11.   
  12.     </body>  
  13. </html>