التخطي إلى المحتوى الرئيسي

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>



تعليقات

المشاركات الشائعة من هذه المدونة

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

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

JDBC using JSP and Servlet

JDBC means Java Database Connectivity ,It is intermediates from Application to database. JDBC has different type of divers and provides to communicate from database server. JDBC contain four different type of approach to communicate with Database Type 1:- JDBC-ODBC Driver Type2:- JDBC Vendor specific Type3 :- JDBC Network Specific Type4:- JDBC Client-Server based Driver  or JAVA thin driver:- Mostly we prefer Type 4 type of Driver to communicate with database server. Step for JDBC:- 1  Create Database using MYSQL ,ORACLE ,MS-SQL or any other database 2   Create Table using database server 3   Create Form according to database table 4  Submit Form and get form data into servlet 5  write JDBC Code:-     5.1)   import package    import java.sql.*     5.2)  Add JDBC Driver according to database ide tools     5.3)  call driver in program         ...