SlideShare ist ein Scribd-Unternehmen logo
1 von 33
Java server pages
(jsp)
SN.Bhurt
Java Server Pages (JSP)
• JSP (Java Server Pages) is a standard for developing interactive Web
applications (pages containing dynamic content).
• A JSP web page (recognizable by the .jsp extension) may display
different content based on certain parameters.
• JSPs are integrated in a web page in HTML using special tags which will
notify the Web server that the code included within these tags are to be
interpreted.
• The result (HTML codes) will be returned to the client browser .
Java Server Pages (JSP)
• JSP is a server-side programming technology
• Enables the creation of dynamic, platform-independent method for
building Web-based applications.
• JSP have access to the entire family of Java APIs, including the JDBC
API to access enterprise databases.
• JSP support dynamic content which helps developers insert java code in
HTML pages by making use of special JSP tags, most of which start with
<% and end with %>.
Advantage of JSP
• Easy to maintain
• High Performance and Scalability.
• JSP is built on Java technology, so it is platform independent.
• JSPs are multithreaded.
• JSPs are portable.
• JSPs are object-oriented.
• JSPs are secure.
JSP Processing:
Web Server: Tomcat
• Apache Tomcat is an open source software implementation of the JSP
and Servlet technologies and can act as a standalone server for testing
JSP and Servlets and can be integrated with the Apache Web Server.
• The JSP container is responsible for intercepting requests for JSP
pages.
• JSP container works with the Web server to provide the runtime
environment and other services a JSP needs.
• Container knows how to understand the special elements that are part of
JSPs.
JSP Life Cycle
• JSP life cycle can be defined as the entire process from its creation till
the destruction.
• A JSP page is converted into Servlet in order to service requests. The
translation of a JSP page to a Servlet is called Lifecycle of JSP. JSP
Lifecycle consists of following steps.
1. Translation of JSP to Servlet code.
2. Compilation of Servlet to bytecode.
3. Loading Servlet class.
4. Creating servlet instance.
5. Initialization by calling
jspInit() method
6. Request Processing by calling
_jspService() method
7. Destroying by calling jspDestroy() method
JSP Project Stucture
JSP Compilation
• JSP pages are converted into Servlet by the Web Container.
• The Container translates a JSP page into servlet class source(.java) file and then
compiles into a Java Servlet class.
Elements of a JSP page
• A JSP page can contain four types elements (excluding the HTML
code):
 Statements: To declare methods and attributes
 Scriptlets: Java code that will be translated into code in the
service() method
 Expressions: To easily send dynamically created string to the
browser
 Directives: Comprehensive information on the page
JSP Declarations Tag
• A declaration is a block of code to define methods and class variables to
be used throughout the page.
• The syntax for a declaration is as follows:
Syntax:
<%! declaration %>
Example:
<%!String str = "Hi every one…..";
int Number = 10;
public static int add(int a, int b) {
return a+b;
}%>
JSP Expressions Tag:
• Expression is used to print all Literals, Method return values, & variable
values.
• Whatever u like to print, same thing we can keep inside a Expression
• The code placed within expression tag is written to the output stream of
the response. So you need not write out.print() to write data. It is mainly
used to print the values of variable or method.
• Whatever statements we're keeping inside a Scriptlet, those should not
end with a semicolon (;)
JSP Expressions Tag
• JSP expressions can insert strings (dynamically generated) in HTML
page. The syntax of a JSP expression is as follows:
• It can be used instead of the following scriptlet:
Syntax:
<%= Expression %>
Example:
<%= "Welcome to Hidaya Institute of Science
and Technology" %>
Syntax:
<% Statement %>
Example:
<%out.println(showData());%>
Scriptlet Tag:
• Inside Scriptlet, we're keeping the java statements.
• Scriptlets are almost look like a statements which are inside a method.
• Whatever statements we're keeping inside a method, same statements,
we can keep inside a Scriptlet.
• Whatever statements we're keeping inside a Scriptlet, those should end
with a semicolon (;)
Syntax :
<% java source code; %>
Example:
<% out.println("Welcome in JSP"); %>
JSP comments
• With JSP you can add comments in two ways.
 Generate a comment visible in the HTML source code (HTML
comment) of the client with the following syntax:
 Create a comment in the JSP code for the purpose of
documentation (not visible to the client) with the following syntax:
<!-- comments [<%= expression %>] -->
<%-- comments --%>
JSP Directives
• Directives control the processing of an entire JSP page. It gives
directions to the server regarding processing of a page.
• Directive Tag gives special instruction to Web Container at the
time of page translation.
Directive Description
<%@ page ... %>
Defines page dependent properties such as language,
session, errorPage etc.
<%@ include ... %> Defines file to be included.
<%@ taglib ... %> Declares tag library used in the page
Page Directive
Attribut Possible values Description
language java
Specifies the language to be used to
process to the instructions of the page.
import pakage.*
Allows you to import a list of classes or
packages
errorPage URL
Allows you to specify a JSP page to
manage unhandled exceptions.
contentType
text/html;charset=I
SO-8859-1
Indicates the MIME type of the page as
well as the character set used
• There are several attributes, which are used along with Page
Directives like
The import Attribute:
• The import attribute serves the same function as, and behaves like, the
Java import statement. The value for the import option is the name of the
package you want to import.
• To import java.sql.*, use the following page directive:
Syntax :
<%@ page import="java.sql.*" %>
Example:
<%@page import="java.util.Random" %>
<%@page import="java.util.Random" %>
<h1><%= "Hello World!"%></h1>
<% out.print("Welcome in JSP page");%><br />
<%
int radius = 7;
double pi = 3.1415;
out.print("Result: " + (radius + pi));
%>
<br />
<% Random rand = new Random();
int a = rand.nextInt();
out.print(a);
%>
Include Directive
• Used to copy the content of one JSP page to another. It’s like
including the code of one file into another.
• For merging external files to the current JSP page during
translation phase (The phase where JSP gets converted into the
equivalent Servlet).
Syntax:
<%@ include file="URL of the file" %>
Example:
<%@ include file=“header.html" %>
Deployment Descriptor: web.xml
• Java web applications use a deployment descriptor file to determine how
URLs map to servlets, which URLs require authentication, and other
information.
• This file is named web.xml, and resides in WEB-INF/ directory.
• web.xml is part of the servlet standard for web applications.
• IT describes the classes, resources and configuration of the application
and how the web server uses them to serve web requests.
• When the web server receives a request for the application, it uses the
deployment descriptor to map the URL of the request to the code that
ought to handle the request.
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>controller.MyServlet</servlet-
class>
</servlet>
<error-page>
<error-code>404</error-code>
<location>/error-404.jsp</location>
</error-page>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<display-name>MyTestingApp</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
</web-app>
• <form action="response.jsp" method="POST">
• <table width="350" border="1">
• <tr>
• <td><label>Enter Your Name</label></td>
• <td><input name="first_name"
type="text"></td>
• </tr>
• <tr>
• <td>Enter Father Name</td>
• <td><input name="last_name"
type="text"></td>
• </tr>
• <tr>
• <td>Gender</td>
• <td>
• <input type="radio" name="sex"
value="male" checked>Male
• <input type="radio" name="sex"
value="female">Female
• </td>
• </tr>
• <tr>
• <td>Your Course</td>
• <td>
• <select name="course">
• <option value="Not Selected"
selected>Select course</option>
• <option value="Java">Java</option>
• <option value="PHP">PHP</option>
• <option value="ASP">ASP</option>
• <option
value="System">System</option>
• <option
value="Networking">Network</option>
• </select>
• </td>
• </tr>
• <tr>
• <td colspan="2"><center><input name=""
type="submit" value="Send Value"></center></td>
• </tr>
• </table>
• </form>
• <h2>Using GET/POST Method to Read Form Data</h2>
• <ul>
• <li><p><b>First Name:</b>
• <%= request.getParameter("first_name")%>
• </p></li>
• <li><p><b>Last Name:</b>
• <%= request.getParameter("last_name")%>
• </p></li>
• <li><p><b>Gender:</b>
• <%= request.getParameter("sex")%>
• </p></li>
• <li><p><b>Course:</b>
• <%= request.getParameter("course")%>
• </p></li>
• </ul>
session
• Java have an HttpSession object which you can use to store state
information for a user.
• The session is managed on the client by a cookie (JSESSIONID) or can
be done using URL rewrites.
• The session timeout describes how long the server will wait after the last
request before deleting the state information stored in a HttpSession.
session.setAttribute("name", name);
session.getAttribute("name")
<%
session.setAttribute("username", request.getParameter("first_name"));
if (session.getAttribute("username") == "" ||
session.getAttribute("username") == null) {
%>
<h3>invalid data</h3>
<%
}
%>
DataBase
<%@ page contentType="text/html; charset=utf-8" language="java"
import="java.sql.*" errorPage="" %>
<%@page import="java.util.*" %>
<!doctype html>
<%!
public class Bean{
public int Id;
public String Name;
public String Fname;
public String Surname;
}
public Vector<Bean> getStudent()throws SQLException{
Statement stat = null;
ResultSet result = null;
Vector<Bean> vect = new Vector<Bean>();
String query = "Select StudentId, StudentName, FatherName, surname from student";
try{
stat = con.createStatement();
result=stat.executeQuery(query);
while(result.next()){
Bean std = new Bean();
std.Id = result.getInt("StudentId");
std.Name = result.getString("StudentName");
std.Fname = result.getString("FatherName");
std.Surname = result.getString("FatherName");
vect.addElement(std);
}//end loop
}finally{
if(stat!=null) stat.close();
}
return vect;
}//end getStudent
Add Method
public int addStudent(String name, String fname, String surname)throws
SQLException{
Statement stat = null;
int rows=0;
String query = "insert into student (StudentName, FatherName,
surname) VALUES ('"+name+"','"+fname+"','"+surname+"')";
try{
stat = con.createStatement();
rows = stat.executeUpdate(query);
return rows;
}finally{
if(stat!=null) stat.close();
}
}//end insert
}//end class
%>
Simple Form
html>
<body>
<form action="#" method="post">
<table width="350" border="1">
<tr>
<td><label>Enter Your Name</label></td>
<td><input name="first_name" type="text">&nbsp;</td>
</tr>
<tr>
<td>Enter Father Name</td>
<td><input name="last_name" type="text"></td>
</tr>
<tr>
<td>Enter Surname</td>
<td><input name="sur_name" type="text"></td>
</tr>
<tr>
<td colspan="2"><center><input name=""
type="submit"></center></td>
</tr>
</table>
</form>
<h2>Using GET/POST Method to Read Form Data</h2>
<%
Database db = new Database();
db.init();
if(request.getMethod().equalsIgnoreCase("POST") ){
try{
String Id = request.getParameter("stdId");
String name =
request.getParameter("first_name").trim();
String fname =
request.getParameter("last_name").trim();
String surname =
request.getParameter("sur_name").trim()
if(name.length() !=0 && fname.length() !=0){
db.addStudent(name, fname, surname);
}
Vector<Bean> vect = db.getStudent();
%>
<table width="350" border="1">
<tr>
<th>Id</th>
<th>Name</th>
<th>Father Name</th>
<th>Surname</th>
</tr>
<% for(Bean std : vect){ %>
<tr>
<td><%=std.Id%></td>
<td><%=std.Name%> </td>
<td><%=std.Fname%></td>
<td><%=std.Surname%></td>
</tr>
<% } %>
</table>
<%
}catch(Exception e){
out.println(e);
}
}//end if
else{
out.println("----------------------------"+);
}
%>
</body>
</html>

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE  AND JSP PROCESSINGINTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE  AND JSP PROCESSING
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
JSP Scope variable And Data Sharing
JSP Scope variable And Data SharingJSP Scope variable And Data Sharing
JSP Scope variable And Data Sharing
 
19servlets
19servlets19servlets
19servlets
 
20jsp
20jsp20jsp
20jsp
 
Jsp
JspJsp
Jsp
 
3.jsp tutorial
3.jsp tutorial3.jsp tutorial
3.jsp tutorial
 
Deploying java beans in jsp
Deploying java beans in jspDeploying java beans in jsp
Deploying java beans in jsp
 
Implementing jsp tag extensions
Implementing jsp tag extensionsImplementing jsp tag extensions
Implementing jsp tag extensions
 
Jsp
JspJsp
Jsp
 
Jsp
JspJsp
Jsp
 
Jsp abes new
Jsp abes newJsp abes new
Jsp abes new
 
Jsp basic
Jsp basicJsp basic
Jsp basic
 
Jsp presentation
Jsp presentationJsp presentation
Jsp presentation
 
Introduction to the Servlet / JSP course
Introduction to the Servlet / JSP course Introduction to the Servlet / JSP course
Introduction to the Servlet / JSP course
 
Unified Expression Language
Unified Expression LanguageUnified Expression Language
Unified Expression Language
 
Ch. 8 script free pages
Ch. 8 script free pagesCh. 8 script free pages
Ch. 8 script free pages
 
Being a jsp
Being a jsp     Being a jsp
Being a jsp
 
Ch. 9 jsp standard tag library
Ch. 9 jsp standard tag libraryCh. 9 jsp standard tag library
Ch. 9 jsp standard tag library
 
HTL(Sightly) - All you need to know
HTL(Sightly) - All you need to knowHTL(Sightly) - All you need to know
HTL(Sightly) - All you need to know
 

Andere mochten auch (11)

Jsp with mvc
Jsp with mvcJsp with mvc
Jsp with mvc
 
Implicit object.pptx
Implicit object.pptxImplicit object.pptx
Implicit object.pptx
 
Jsp element
Jsp elementJsp element
Jsp element
 
Implicit objects advance Java
Implicit objects advance JavaImplicit objects advance Java
Implicit objects advance Java
 
J2EE and layered architecture
J2EE and layered architectureJ2EE and layered architecture
J2EE and layered architecture
 
J2EE Introduction
J2EE IntroductionJ2EE Introduction
J2EE Introduction
 
JSP
JSPJSP
JSP
 
MVC ppt presentation
MVC ppt presentationMVC ppt presentation
MVC ppt presentation
 
Model View Controller (MVC)
Model View Controller (MVC)Model View Controller (MVC)
Model View Controller (MVC)
 
Mvc architecture
Mvc architectureMvc architecture
Mvc architecture
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessions
 

Ähnlich wie Java Server Pages

JSP AND XML USING JAVA WITH GET AND POST METHODS
JSP AND XML USING JAVA WITH GET AND POST METHODSJSP AND XML USING JAVA WITH GET AND POST METHODS
JSP AND XML USING JAVA WITH GET AND POST METHODSbharathiv53
 
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology Ayes Chinmay
 
JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...MathivananP4
 
Core web application development
Core web application developmentCore web application development
Core web application developmentBahaa Farouk
 
SCWCD : Java server pages CHAP : 9
SCWCD : Java server pages  CHAP : 9SCWCD : Java server pages  CHAP : 9
SCWCD : Java server pages CHAP : 9Ben Abdallah Helmi
 
JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESYoga Raja
 
Enterprise java unit-3_chapter-1-jsp
Enterprise  java unit-3_chapter-1-jspEnterprise  java unit-3_chapter-1-jsp
Enterprise java unit-3_chapter-1-jspsandeep54552
 
J2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsJ2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsKaml Sah
 
JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server PageVipin Yadav
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)Manisha Keim
 
Advance java session 10
Advance java session 10Advance java session 10
Advance java session 10Smita B Kumar
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...WebStackAcademy
 

Ähnlich wie Java Server Pages (20)

Jsp
JspJsp
Jsp
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
JSP.pptx
JSP.pptxJSP.pptx
JSP.pptx
 
JSP AND XML USING JAVA WITH GET AND POST METHODS
JSP AND XML USING JAVA WITH GET AND POST METHODSJSP AND XML USING JAVA WITH GET AND POST METHODS
JSP AND XML USING JAVA WITH GET AND POST METHODS
 
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
 
JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...
 
Core web application development
Core web application developmentCore web application development
Core web application development
 
SCWCD : Java server pages CHAP : 9
SCWCD : Java server pages  CHAP : 9SCWCD : Java server pages  CHAP : 9
SCWCD : Java server pages CHAP : 9
 
Introduction to JSP.pptx
Introduction to JSP.pptxIntroduction to JSP.pptx
Introduction to JSP.pptx
 
Jsp in Servlet by Rj
Jsp in Servlet by RjJsp in Servlet by Rj
Jsp in Servlet by Rj
 
JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGES
 
Enterprise java unit-3_chapter-1-jsp
Enterprise  java unit-3_chapter-1-jspEnterprise  java unit-3_chapter-1-jsp
Enterprise java unit-3_chapter-1-jsp
 
Jsp Introduction Tutorial
Jsp Introduction TutorialJsp Introduction Tutorial
Jsp Introduction Tutorial
 
J2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsJ2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - Components
 
JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server Page
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
C:\fakepath\jsp01
C:\fakepath\jsp01C:\fakepath\jsp01
C:\fakepath\jsp01
 
Jsp tutorial
Jsp tutorialJsp tutorial
Jsp tutorial
 
Advance java session 10
Advance java session 10Advance java session 10
Advance java session 10
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
 

Kürzlich hochgeladen

Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 

Kürzlich hochgeladen (20)

Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 

Java Server Pages

  • 2. Java Server Pages (JSP) • JSP (Java Server Pages) is a standard for developing interactive Web applications (pages containing dynamic content). • A JSP web page (recognizable by the .jsp extension) may display different content based on certain parameters. • JSPs are integrated in a web page in HTML using special tags which will notify the Web server that the code included within these tags are to be interpreted. • The result (HTML codes) will be returned to the client browser .
  • 3. Java Server Pages (JSP) • JSP is a server-side programming technology • Enables the creation of dynamic, platform-independent method for building Web-based applications. • JSP have access to the entire family of Java APIs, including the JDBC API to access enterprise databases. • JSP support dynamic content which helps developers insert java code in HTML pages by making use of special JSP tags, most of which start with <% and end with %>.
  • 4. Advantage of JSP • Easy to maintain • High Performance and Scalability. • JSP is built on Java technology, so it is platform independent. • JSPs are multithreaded. • JSPs are portable. • JSPs are object-oriented. • JSPs are secure.
  • 6. Web Server: Tomcat • Apache Tomcat is an open source software implementation of the JSP and Servlet technologies and can act as a standalone server for testing JSP and Servlets and can be integrated with the Apache Web Server. • The JSP container is responsible for intercepting requests for JSP pages. • JSP container works with the Web server to provide the runtime environment and other services a JSP needs. • Container knows how to understand the special elements that are part of JSPs.
  • 7. JSP Life Cycle • JSP life cycle can be defined as the entire process from its creation till the destruction. • A JSP page is converted into Servlet in order to service requests. The translation of a JSP page to a Servlet is called Lifecycle of JSP. JSP Lifecycle consists of following steps. 1. Translation of JSP to Servlet code. 2. Compilation of Servlet to bytecode. 3. Loading Servlet class. 4. Creating servlet instance. 5. Initialization by calling jspInit() method 6. Request Processing by calling _jspService() method 7. Destroying by calling jspDestroy() method
  • 9. JSP Compilation • JSP pages are converted into Servlet by the Web Container. • The Container translates a JSP page into servlet class source(.java) file and then compiles into a Java Servlet class.
  • 10. Elements of a JSP page • A JSP page can contain four types elements (excluding the HTML code):  Statements: To declare methods and attributes  Scriptlets: Java code that will be translated into code in the service() method  Expressions: To easily send dynamically created string to the browser  Directives: Comprehensive information on the page
  • 11. JSP Declarations Tag • A declaration is a block of code to define methods and class variables to be used throughout the page. • The syntax for a declaration is as follows: Syntax: <%! declaration %> Example: <%!String str = "Hi every one….."; int Number = 10; public static int add(int a, int b) { return a+b; }%>
  • 12. JSP Expressions Tag: • Expression is used to print all Literals, Method return values, & variable values. • Whatever u like to print, same thing we can keep inside a Expression • The code placed within expression tag is written to the output stream of the response. So you need not write out.print() to write data. It is mainly used to print the values of variable or method. • Whatever statements we're keeping inside a Scriptlet, those should not end with a semicolon (;)
  • 13. JSP Expressions Tag • JSP expressions can insert strings (dynamically generated) in HTML page. The syntax of a JSP expression is as follows: • It can be used instead of the following scriptlet: Syntax: <%= Expression %> Example: <%= "Welcome to Hidaya Institute of Science and Technology" %> Syntax: <% Statement %> Example: <%out.println(showData());%>
  • 14. Scriptlet Tag: • Inside Scriptlet, we're keeping the java statements. • Scriptlets are almost look like a statements which are inside a method. • Whatever statements we're keeping inside a method, same statements, we can keep inside a Scriptlet. • Whatever statements we're keeping inside a Scriptlet, those should end with a semicolon (;) Syntax : <% java source code; %> Example: <% out.println("Welcome in JSP"); %>
  • 15. JSP comments • With JSP you can add comments in two ways.  Generate a comment visible in the HTML source code (HTML comment) of the client with the following syntax:  Create a comment in the JSP code for the purpose of documentation (not visible to the client) with the following syntax: <!-- comments [<%= expression %>] --> <%-- comments --%>
  • 16. JSP Directives • Directives control the processing of an entire JSP page. It gives directions to the server regarding processing of a page. • Directive Tag gives special instruction to Web Container at the time of page translation. Directive Description <%@ page ... %> Defines page dependent properties such as language, session, errorPage etc. <%@ include ... %> Defines file to be included. <%@ taglib ... %> Declares tag library used in the page
  • 17. Page Directive Attribut Possible values Description language java Specifies the language to be used to process to the instructions of the page. import pakage.* Allows you to import a list of classes or packages errorPage URL Allows you to specify a JSP page to manage unhandled exceptions. contentType text/html;charset=I SO-8859-1 Indicates the MIME type of the page as well as the character set used • There are several attributes, which are used along with Page Directives like
  • 18. The import Attribute: • The import attribute serves the same function as, and behaves like, the Java import statement. The value for the import option is the name of the package you want to import. • To import java.sql.*, use the following page directive: Syntax : <%@ page import="java.sql.*" %> Example: <%@page import="java.util.Random" %>
  • 19. <%@page import="java.util.Random" %> <h1><%= "Hello World!"%></h1> <% out.print("Welcome in JSP page");%><br /> <% int radius = 7; double pi = 3.1415; out.print("Result: " + (radius + pi)); %> <br /> <% Random rand = new Random(); int a = rand.nextInt(); out.print(a); %>
  • 20. Include Directive • Used to copy the content of one JSP page to another. It’s like including the code of one file into another. • For merging external files to the current JSP page during translation phase (The phase where JSP gets converted into the equivalent Servlet). Syntax: <%@ include file="URL of the file" %> Example: <%@ include file=“header.html" %>
  • 21. Deployment Descriptor: web.xml • Java web applications use a deployment descriptor file to determine how URLs map to servlets, which URLs require authentication, and other information. • This file is named web.xml, and resides in WEB-INF/ directory. • web.xml is part of the servlet standard for web applications. • IT describes the classes, resources and configuration of the application and how the web server uses them to serve web requests. • When the web server receives a request for the application, it uses the deployment descriptor to map the URL of the request to the code that ought to handle the request.
  • 23. • <form action="response.jsp" method="POST"> • <table width="350" border="1"> • <tr> • <td><label>Enter Your Name</label></td> • <td><input name="first_name" type="text"></td> • </tr> • <tr> • <td>Enter Father Name</td> • <td><input name="last_name" type="text"></td> • </tr> • <tr> • <td>Gender</td> • <td> • <input type="radio" name="sex" value="male" checked>Male • <input type="radio" name="sex" value="female">Female • </td> • </tr> • <tr> • <td>Your Course</td> • <td> • <select name="course"> • <option value="Not Selected" selected>Select course</option> • <option value="Java">Java</option> • <option value="PHP">PHP</option> • <option value="ASP">ASP</option> • <option value="System">System</option> • <option value="Networking">Network</option> • </select> • </td> • </tr> • <tr> • <td colspan="2"><center><input name="" type="submit" value="Send Value"></center></td> • </tr> • </table> • </form>
  • 24. • <h2>Using GET/POST Method to Read Form Data</h2> • <ul> • <li><p><b>First Name:</b> • <%= request.getParameter("first_name")%> • </p></li> • <li><p><b>Last Name:</b> • <%= request.getParameter("last_name")%> • </p></li> • <li><p><b>Gender:</b> • <%= request.getParameter("sex")%> • </p></li> • <li><p><b>Course:</b> • <%= request.getParameter("course")%> • </p></li> • </ul>
  • 25. session • Java have an HttpSession object which you can use to store state information for a user. • The session is managed on the client by a cookie (JSESSIONID) or can be done using URL rewrites. • The session timeout describes how long the server will wait after the last request before deleting the state information stored in a HttpSession. session.setAttribute("name", name); session.getAttribute("name")
  • 26. <% session.setAttribute("username", request.getParameter("first_name")); if (session.getAttribute("username") == "" || session.getAttribute("username") == null) { %> <h3>invalid data</h3> <% } %>
  • 27. DataBase <%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %> <%@page import="java.util.*" %> <!doctype html> <%! public class Bean{ public int Id; public String Name; public String Fname; public String Surname; }
  • 28. public Vector<Bean> getStudent()throws SQLException{ Statement stat = null; ResultSet result = null; Vector<Bean> vect = new Vector<Bean>(); String query = "Select StudentId, StudentName, FatherName, surname from student"; try{ stat = con.createStatement(); result=stat.executeQuery(query); while(result.next()){ Bean std = new Bean(); std.Id = result.getInt("StudentId"); std.Name = result.getString("StudentName"); std.Fname = result.getString("FatherName"); std.Surname = result.getString("FatherName"); vect.addElement(std); }//end loop }finally{ if(stat!=null) stat.close(); } return vect; }//end getStudent
  • 29. Add Method public int addStudent(String name, String fname, String surname)throws SQLException{ Statement stat = null; int rows=0; String query = "insert into student (StudentName, FatherName, surname) VALUES ('"+name+"','"+fname+"','"+surname+"')"; try{ stat = con.createStatement(); rows = stat.executeUpdate(query); return rows; }finally{ if(stat!=null) stat.close(); } }//end insert }//end class %>
  • 30. Simple Form html> <body> <form action="#" method="post"> <table width="350" border="1"> <tr> <td><label>Enter Your Name</label></td> <td><input name="first_name" type="text">&nbsp;</td> </tr> <tr> <td>Enter Father Name</td> <td><input name="last_name" type="text"></td> </tr> <tr> <td>Enter Surname</td> <td><input name="sur_name" type="text"></td> </tr>
  • 31. <tr> <td colspan="2"><center><input name="" type="submit"></center></td> </tr> </table> </form> <h2>Using GET/POST Method to Read Form Data</h2> <% Database db = new Database(); db.init(); if(request.getMethod().equalsIgnoreCase("POST") ){ try{ String Id = request.getParameter("stdId"); String name = request.getParameter("first_name").trim(); String fname = request.getParameter("last_name").trim(); String surname = request.getParameter("sur_name").trim()
  • 32. if(name.length() !=0 && fname.length() !=0){ db.addStudent(name, fname, surname); } Vector<Bean> vect = db.getStudent(); %> <table width="350" border="1"> <tr> <th>Id</th> <th>Name</th> <th>Father Name</th> <th>Surname</th> </tr> <% for(Bean std : vect){ %> <tr> <td><%=std.Id%></td> <td><%=std.Name%> </td> <td><%=std.Fname%></td> <td><%=std.Surname%></td> </tr>
  • 33. <% } %> </table> <% }catch(Exception e){ out.println(e); } }//end if else{ out.println("----------------------------"+); } %> </body> </html>