Skip to main content

JSP Introduction,JSP Tutorials


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 create 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>

Radio Button Example in JSP:-
It is HTML predefine form element that is used to click a single option at a time in a group of options.
Radio button will be declared by <input type="radio" />. here input is the tag name and type is the attribute name.
the name attribute must be the same in the case of radiobutton. radio button creates a group using the name attribute.
Syntax of Radio Button:-
<input type="radio" name="any"  />
<input type="radio" name="any"  />
<%@ 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 bgcolor="<%
if(request.getParameter("btnsubmit")!=null)
{
out.print(request.getParameter("course"));
}
%>">
<form action="" method="post">
<input type="radio" name="course" value="Red" />Java
<br><br>
<input type="radio" name="course" value="Green" />.NET
<br><br>
<input type="radio" name="course" value="Blue" />PHP
<br><br>
<input type="submit" name="btnsubmit" value="Click"  />
</form>
</body>
</html>
Another Example of RadioButton in JSP:-
<%@ 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>
<form action="" method="post">
<input type="radio" name="course" value="Red" />Java
<br><br>
<input type="radio" name="course" value="Green" />.NET
<br><br>
<input type="radio" name="course" value="Blue" />PHP
<br><br>
<input type="submit" name="btnsubmit" value="Click"  />
</form>
<%
if(request.getParameter("btnsubmit")!=null)
{
out.print("Selected Course is "+request.getParameter("course"));
}
%>
</body>
</html>


CheckBox Example in JSP:-
It is HTML predefine form element that is used to click multiple options at a time in a group of options.
Checkbox will be declared by <input type="checkbox" />. here input is the tag name and type is the attribute name.
the name attribute must be the array type in the case of the checkbox. it contains multiple values that's why we should take array type identifiers.
Syntax of CheckBox:-
<input type="checkbox" name="any[]"  />
<input type="checkbox" name="any[]"  />
<%@ 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>
<form action="" method="post">
<input type="checkbox" name="course[]" value="Java" />Java
<br><br>
<input type="checkbox" name="course[]" value=".NET" />.NET
<br><br>
<input type="checkbox" name="course[]" value="PHP" />PHP
<br><br>
<input type="submit" name="btnsubmit" value="Click"  />
</form>
<%
if(request.getParameter("btnsubmit")!=null)
{
String arr[] = request.getParameterValues("course[]");
String c="";
for(String s:arr)
{
c = c+s +" ";
}
out.print("Selected Course is "+c);
}
%>
</body>
</html>

ListBox Example in JSP:-

It is HTML predefine form element that is used to select multiple options at a time in a group of options.
Listbox will be declared by <select name="any[]" multiple="true" />. here select is the tag name and multiple is the attribute name.
the name attribute must be the array type in the case of the list box. it contains multiple values that's why we should take array type identifiers.
<select name="array[]" multiple="true">
<option value="data">Display Data</option>
</select>

<%@ 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>
<form action="" method="post">
<select name="course[]" multiple="true">
<option value="JAVA">JAVA</option>
<option value=".NET">.NET</option>
<option value="PHP">PHP</option>
<option value="iOS">iOS</option>
</select>
<br><br>
<input type="submit" name="btnsubmit" value="Click"  />
</form>
<%
if(request.getParameter("btnsubmit")!=null)
{
String arr[] = request.getParameterValues("course[]");
String c="";
for(String s:arr)
{
c = c+s +" ";
}
out.print("Selected Course is "+c);
}
%>
</body>
</html>

Dropdownlist Example in JSP:-

It is HTML predefine form element that is used to select multiple options at a time in a group of options.
Listbox will be declared by <select name="any" >. here select is the tag name and name is the attribute name.
the name attribute must be the normal type in the case of the list box. it contains single value that's why we should take normal identifiers.
<%@ 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>
<form action="" method="post">
<select name="course">
<option value="JAVA">JAVA</option>
<option value=".NET">.NET</option>
<option value="PHP">PHP</option>
<option value="iOS">iOS</option>
</select>
<br><br>
<input type="submit" name="btnsubmit" value="Click"  />
</form>
<%
if(request.getParameter("btnsubmit")!=null)
{
out.print("Selected Course is "+ request.getParameter("course"));
}
%>
</body>
</html>

TextArea Example in JSP:-
It is used to take input with multiple lines statement,  
<%@ 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>
<form action="" method="post">
Enter post content
<br>
<textarea rows="3" cols="20" name="txtarea">
</textarea>
<br><br>
<input type="submit" name="btnsubmit" value="Click"  />
</form>
<%
if(request.getParameter("btnsubmit")!=null)
{
out.print("Message is "+ request.getParameter("txtarea"));
}
%>
</body>
</html>



Comments

Popular posts from this blog

DSA in C# | Data Structure and Algorithm using C#

  DSA in C# |  Data Structure and Algorithm using C#: Lecture 1: Introduction to Data Structures and Algorithms (1 Hour) 1.1 What are Data Structures? Data Structures are ways to store and organize data so it can be used efficiently. Think of data structures as containers that hold data in a specific format. Types of Data Structures: Primitive Data Structures : These are basic structures built into the language. Example: int , float , char , bool in C#. Example : csharp int age = 25;  // 'age' stores an integer value. bool isStudent = true;  // 'isStudent' stores a boolean value. Non-Primitive Data Structures : These are more complex and are built using primitive types. They are divided into: Linear : Arrays, Lists, Queues, Stacks (data is arranged in a sequence). Non-Linear : Trees, Graphs (data is connected in more complex ways). Example : // Array is a simple linear data structure int[] number...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

Conditional Statement in Python

It is used to solve condition-based problems using if and else block-level statement. it provides a separate block for  if statement, else statement, and elif statement . elif statement is similar to elseif statement of C, C++ and Java languages. Type of Conditional Statement:- 1) Simple if:- We can write a single if statement also in python, it will execute when the condition is true. for example, One real-world problem is here?? we want to display the salary of employees when the salary will be above 10000 otherwise not displayed. Syntax:- if(condition):    statements The solution to the above problem sal = int(input("Enter salary")) if sal>10000:     print("Salary is "+str(sal)) Q)  WAP to increase the salary of employees from 500 if entered salary will be less than 10000 otherwise the same salaries will be displayed. Solution:- x = int(input("enter salary")) if x<10000:     x=x+500 print(x)   Q) WAP to display th...