JSP Introduction,JSP Tutorials

0

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>



Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)