Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

JSP Introduction :-

JSP means Java Server Page, It is used to create a dynamic web page under Java web application.

We can easily write HTML Code on the JSP web page hence JSP is mostly used to create the design layer of an application.

Designers can also design a complete web page using JSP because the JSP view is similar to HTML View.

For Standard Java Web Application, We will create JSP for designing and Servlet for Programming.

 .............................................................................................................................................



 What is the difference between JSP and Servlet?


1) JSP is used to create a design layer and the Servlet is used to contain the code layer of an application.

2)  We can write static text and dynamic text both under the JSP web page with separate code structure but  Servlet is a Pure Java class that means we will write HTML Code content under the Servlet predefine method.


3)  JSP web page only handle HTTP Request but Servlet can handle all type of protocol

4)  JSP web page creates default Session Object but in Servlet we should manually create session Object.

5)  We can easily implement Javabean class to bind JSP view to Object but in Servlet we will manually Create Object of Bean class.

6)  JSP performance is slow, If we write dynamic code under JSP because JSP will filter Java program code then compile then execute but servlet has a readymade class that directly compiles and execute.



 How to write HTML Code in JSP:-

JSP by default provide complete HTML Structure we can directly write HTML code under JSP Web page.


 How to write Java Code in JSP:-

JSP provides special tag expression to write Java code under JSP Web pages.

JSP provides three different types of tag


Type of Expression in JSP:-

1)  Declaration Tag   <%!                  %>  :-

  It is used to clear a global variable whose scope will be in the complete web page


<%!    int a,b,c              %>


2)  Expression Tag or Scriptlet:-

     It provides a complete block of code  where we write a program

   <%


   %>

Example:-
<%
                int a,b,c;
                a=100;
                b=200;
                c=a+b;
                out.print(c);
            
            %>

3)  Output Tag:-

      <%=            %>

  <%= c   %>




Example of expression tag into JSP Web page:-

We can write multiple expression tag into JSP Web pages.



<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
       
            <%
                int a,b,c;
                a=100;
                b=200;
                c=a+b;
                out.print(c);
           
            %>
           
           
           
            <hr>
           
                <%
                int x,y,z;
                x=1000;
                y=200;
                z=x+y;
                out.print(z);
           
            %>
       
    </body>
</html>





Complete Code of JSP With All Tag:-



<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <%! int a,b,c;        %>
    </head>
    <body>
        <h1>Hello World!</h1>
       
            <%
               
                a=100;
                b=200;
                c=a+b;
              
           
            %>
           
           
           
            <hr>
           
               <%= c %>
       
    </body>
</html>



JSP  Code to implement Multiple Tag Similtenious:-


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<%! int a,b,c;  %>

<div>
<%

a=1000;
b=2000;
c=a+b;


%>
</div>

<div>

Result is <%= c %>

</div>
<div>

<%

a=1000;
b=20;
c=a*b;


%>

</div>

<div>

Result is <%= c %>

</div>

</body>
</html>

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post