chap4 4 2

22
CHAPTER 4: SERVLETS AND JAVA SERVER PAGES (JSP) 4.2 Using Java Server Pages (JSP)

Upload: hemo-chella

Post on 26-Jul-2015

67 views

Category:

Education


0 download

Tags:

TRANSCRIPT

CHAPTER 4: SERVLETS AND JAVA SERVER PAGES

(JSP)

4.2 Using Java Server Pages (JSP)

Learning Outcomes

• At the end of the lectures, student should be able to:– Define JSP– Compare between Servlets and JSP– Explain JSP Scripting Elements– Describe Java Beans and JSP– Illustrate the process output generated by Servlets

process– Integrate Servlets and JSP

Introduction• JSP files are HTML files with special Tags containing

Java source code that provide the development of static and dynamic web content.

• Goal: Create static and dynamic web content for a Web Application

• Mostly HTML page, with extension .jsp

• JSP was developed by Sun Microsystems

Example firstJsp.jsp

<%@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>

<%

double num = Math.random();

if (num > 0.95) {

%>

<h2>You'll have a luck day!</h2><p>(<%= num %>)</p>

<%

} else {

%>

<h2>Well, life goes on ... </h2><p>(<%= num %>)</p>

<%

}

%>

<h3>Click <a href="firstJsp.jsp">Try Again</a><br></h3>

</body>

</html>

JSP interface

Servers that support JSP

• Apache Tomcat

• GlassFish Server

• Allaire Jrun

• JRun

• GNU JSP

Why use JSP• JSP is easy to learn and allows developers to quickly

produce web sites and applications in an open and standard way.

• JSP is based on Java, an object-oriented language.

• JSP offers a robust platform for web development.

• Main reasons to use JSP: - Multi platform - Advantages of Java.

Comparison Between JSP and Servlet

JSP SERVLET

JSP is Java inside HTMLJava codes are embedded inside an HTML page

Servlet is HTML inside JavaHTML page is produced using Java's out.println() in a Java program.

JSP Scripting Elements (JSP Tags)

• There are 5 JSP tags

1.JSP Scriptlet

2.JSP Expression

3.JSP Directive

4.JSP Comment

5.JSP Declaration

Explanation of JSP TagsTag Name Function

<% %> JSP scriptlet Untuk memasukkan blok kenyataan JavaTo insert a block of Java statements.

<%= %> JSP expression Pernyataan Java di mana outputnya di satukan ke dalam HTMLJava expression whose output is spliced into HTML

<%@ %> JSP directive Untuk menetapkan syarat yang digunakan untuk keseluruhan JSPTo set conditions that applies to the entire JSP.

<%-- --%> JSP comment Untuk memberitahu enjin JSP untuk mengabaikan kodTo tell the JSP engine to ignore code.

<%! %> JSP declaration Untuk mengisytiharkan pembolehubah dan method bagi JSPTo declare variables and methods for a JSP

JSP Scriptlet <% %><body>

<%

String user_id = request.getParameter("user_id");

int sumcount=0;

try {

Class.forName("com.mysql.jdbc.Driver");

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");

Statement st = conn.createStatement();

String sql = "DELETE FROM users WHERE user_id=+'"+(user_id)+"'";

st.executeUpdate(sql);

response.sendRedirect("application.jsp");

}

catch(Exception e){}

%>

</body>

JSP Expression <%=%>• <h2>You'll have a luck day!</h2><p>(<%= num %>)</p>

• <%= new java.util.Date() %>

JSP Directive <%@%><%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@page import="java.sql.*"%>

JSP Comment <%-- --%><%-- Document : index.jsp Author : User--%>

JSP Declaration <%!%><%!

int globalCounter = 0;

java.util.Date startDate;

public void jspInit( ) {

startDate = new java.util.Date( );

}

public void jspDestroy( ) {

ServletContext context = getServletConfig().getServletContext( );

context.log("test.jsp was visited " + globalCounter +

" times between " + startDate + " and " + (new Date( )));

}

%>

Standard action elements

Describe Java Beans and JSP

• JavaBean is any class that – Implements java.io.Serializable interface– No-argument constructor

• Provide a few getter and setter method

• Never required for use with JSP

JavaBean action elements

Integrate Servlets and JSP

Process output generated by Servlets process

Summary• JSP was developed by Sun Microsystems to allow server

side development.

• JSP files are HTML files with special Tags containing Java source code that provide the dynamic content.

• There are 5 JSP tags• JSP Scriplet

• JSP Expression

• JSP Directive

• JSP Comment

• JSP Declaration