Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 66

JSP Advantage

• Develop dynamic content web


pages
• Simple, Powerful technology
• Portability
• Open-standard
• Mature re-usable component
model.
1
JSP
• Enables the separation of content
generation from content
presentation.
• Web designers concentrate on how
to present the information.
• Web Application Programmers
concentrate on how to
process/build the information.
2
What is a JSP page ?
• A JSP Page is a text-based
document.
• Describes how to process a
request to create a response.

3
First JSP page
<HTML>
<HEAD>
<TITLE>Welcome to JSP</TITLE>
<BODY>
<%
out.println("<H1>My First JSP Example</H1>");
%>
</BODY>
</HTML>
4
JSP Working Model
• A Java Server Pages file is an HTML
document with JSP scripting or
tags.
• It may have associated
components in the form of .class,
.jar files.
• Has a .jsp extension

5
JSP Working Model
• The JSP syntax is parsed and
processed into a servlet on the
server side.
• The output is generated as HTML
content.
• Dynamically this page is served to
the client who actually requested
it.
6
JSP Access Model - 1
• A Client request comes directly to
the server page.
• The page may contact a reusable
Java Bean components for
business logic.
• The results are stored as Bean
properties which the JSP can
access.
• Used to generate dynamic content
and present it back to the client. 7
JSP Access Model - 1

8
JSP access model - 2
• A request comes through a servlet.
• Servlet generates the dynamic
content.
• The servlet creates a bean and
stores the generated dynamic
content into the bean as
properties.
• The servlet invokes the JSP.
• JSP will get the results from the
9
JSP access model - 2

10
Which Model ?
• If the request and request parameters
are available to the servlet, but the
results of the servlet processing
requires a graphical interface to present
them--use a JSP file.
• If presentation layout is minimal and
you don't need to make that
presentation logic available to a
customer or your web page designer,
then a Servlet might suffice.
11
JSP Walkthrough :Emp.htm
<HTML><HEAD>
<TITLE>Employee Registration Form</TITLE>
</HEAD>
<BODY>
<FORM METHOD=GET
ACTION="http://localhost:8080/second.jsp">
<TABLE><TR><TD>Name:</TD><TD>
<INPUT TYPE="text" Name="n"></TD></TR>
<TR><TD>Address:</TD><TD>
<INPUT TYPE="text" Name="a"></TD></TR>
<TR><TD>City:</TD><TD>
<INPUT TYPE="text" Name="c"></TD></TR>
12
JSP Walkthrough:Emp.htm
<TR><TD>Pin:</TD><TD>
<INPUT TYPE="text" Name="p"></TD></TR>
<TR><TD>Telephone:</TD><TD>
<INPUT TYPE="text" Name="t"></TD></TR>
<TR><TD>
<INPUT TYPE="Submit"></TD><TD>
<INPUT TYPE="Reset"></TD></TR>
</FORM>
</HTML>

13
JSP Walkthrough:Second.jsp
<HTML>
<HEAD>
<TITLE>Employee Registration</TITLE>
</HEAD>
<%
String name, address, city, pin, tele;
name = request.getParameter("n");
address = request.getParameter("a");
city = request.getParameter("c");
pin = request.getParameter("p");
tele = request.getParameter("t");
14
JSP Walkthrough:Second.jsp
out.println("Dear Mr./ Ms. " + name);
out.println("<BR><BR>Welcome to the JSP
Programming");
out.println("All your communication will be
routed to<BR>");
out.println("<BR>Address :" + address);
out.println("<BR>City :" + city);
out.println("<BR>PIN :" + pin);
out.println("<BR>Phone :" + tele);
%></HTML>
15
JSP Walkthrough
• Run the Java Web Server by typing
httpd in the command prompt.
• Place the htm and the jsp files in
the public_html directory.
• From the browser type
http://localhost:8080/emp.htm.
• Fill the form and submit.
• See the result.
16
Using POST
• In the HTML page change the
method from GET to POST and
view the page.

17
Another Example
<HTML>
<HEAD>
<TITLE>What is Today</TITLE>
</HEAD>
<%
out.println("Welcome to JSP Programming<br><br>
out.println("Now the time is : " + new java.util.Date(
%>
</HTML>

18
Server-Side Includes-
Walkthrough
%@ page info="First Example" %>
HTML><HEAD>
TITLE>What is Today</TITLE>
/HEAD>
BODY>
%@include file="emp.html" %>
%
ut.println("Welcome to JSP Programming");
ut.println("Now the time is : " + new java.util.Date()
%></BODY></HTML>
19
Using the include tag in JSP
<html>
<jsp:include page= "emp.html"
flush="true" />
<body>
<br><h4>
<%
out.println("This content is from the
original page");
%>
</h4></body></html> 20
The Page Directive
• Gives instructions to the JSP engine that
apply to the entire JSP source file.
• In this example, page specifies an
informative comment that will become
part of the compiled JSP file.
• In other cases, page might specify the
scripting language used in the JSP
source file, packages the source file
would import, or the error page called if
an error or exception occurs.
21
The Page Directive
• Use page directive anywhere in the JSP
page.
• It is a good coding style to place it in
the top of the JSP page.
• How to use ?
<%@ page info="First Example" %>

22
The Include Directive
• Inserts the contents of another file into
the JSP page.
• In this example we embed our
employee registration form.
• In include files no need to put
<HTML><BODY> tags.

23
Redirecting to another URL
• This example is to redirect the
query coming to one JSP to another
URL
• Steps
– HTML page
– JSP page
– Another JSP page (redirection)

24
Redirecting to another URL - HTML page

<html>
<body>
<form action="forward.jsp"
method="post">
Name : <input type="text"
name="sname">
<br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form> 25
Redirecting to another URL - JSP page
<html>
<body>
<h1>
<%!
String sname="";
%>
<%
sname = request.getParameter("sname");
if(sname.equals("")) { %>
26
Redirecting to another URL - JSP page
<jsp:forward page="another.jsp" />
<%
}
else
%>
<u>From the original page</u>
<br><br><%
out.println("Name is " +sname);
%></h1></html>
27
Redirecting to another URL -
another JSP page
<html>
<body>
<h4>
<u>From the redirected page</u>
<br>
<br>
Enter some value
</h4></body></html>
28
Redirecting to another URL
• This page displays the content of
the JSP page when some value is
populated in the text field
• If not, the query is redirected to
another.jsp

29
Beans - JSP
Using Beans in JSP
• Steps include
– creating a Bean class
– placing them in the web server
– creating the jsp page
– calling the bean from the jsp page

31
Step I - Creating Java file
package trial;

public class CheckBean


{
private String name;
public CheckBean() {
name=“Infy";
}
32
Step I - Creating Java file
public String getname() {
return name;
}
public void setname(String a) {
name=a;
}
}

33
Step II - Placing in the Web Server
• Compile the Java program
• Create a folder called as trial in the
classes directory of the Java Web Server
• Copy the CheckBean.class file to this
directory

34
Step III - Creating the JSP file
<html><head>
<title>HTML and Bean and scriptlet JSP
File</title></head>
<jsp:useBean id= "first" scope="page"
class="trial.CheckBean" />
<body><h1>
<p>Default name got is :
<jsp:getProperty name= "first"
property="name" /></p>
35
Step III - Creating the JSP file
<jsp:setProperty name= "first"
property="name" value="JSP
Programming " />
<p>Name got after modification :
<jsp:getProperty name="first"
property="name" />
</p>
</h1>
</body></html>
36
Step III - Creating the JSP file
• Save the file as UsingBean.jsp
• Place it in the public_html folder of
the Java Web Server
• Call the page using
http://localhost:8080/UsingBean.jsp

37
JDBC - Beans: JSP
JDBC Bean
• Create a table employee with two
fields
– name & address
• Create a DSN for this database as
sample

39
Step I - Creating a JDBC Bean
package trial;
import java.sql.*;

public class JDBCBean {


private String name;
private String address;
public JDBCBean( ) {
name = "abc";
address = "bangalore";
} 40
Step I - Creating a JDBC Bean
public void setname(String s) {
name=s;
}
public void setaddress(String s1) {
address=s1;
connect(name,address);
}

41
Step I - Creating a JDBC Bean
public void connect(String x, String y) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriv
er");
Connection con =
DriverManager.getConnection("jdbc:odbc:
sample");
PreparedStatement stmt =
con.prepareStatement("INSERT INTO
EMPLOYEE(NAME,ADDRESS)
VALUES(?,?)"); 42
Step I - Creating a JDBC Bean
stmt.setString(1,x);
stmt.setString(2,y);
stmt.executeUpdate();
}
catch(Exception err)
{ err.printStackTrace();
}
}}
43
Step II - Creating the HTML page
<html><body>
<form action="UsingJDBC.jsp"
method="post">
Name:<input
type="text"name="sname"><br>
Address : <input type="text"
name="saddress">
<br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
44
Step III - Creating the JSP
<html>
<body>
<jsp:useBean id='jdbc' scope='page'
class='trial.JDBCBean' />
<%!
String s ="";
String s1 ="";
%>
45
Step III - Creating the JSP
<%
s = request.getParameter("sname");
s1 = request.getParameter("saddress");
jdbc.connect(s,s1);
out.println
("<h3>Hi ! " +s + " your values are
inserted to the database ");
%>
</html>
46
Using Select Query
package trial;
import java.sql.*;
public class JDBCSelect {
public String connect(String name) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDri
ver");
Connection con =
DriverManager.getConnection("jdbc:odbc
:sample");
Statement stmt = con.createStatement();47
Using Select Query
ResultSet rs = ResultSet rs =
stmt.executeQuery("SELECT address
FROM EMPLOYEE where name Like '"
+name + "'");
String result = "";
while(rs.next())
result = rs.getString("address");
return result;}
catch(Exception err) {
err.printStackTrace();
return null; } } } 48
HTML page
<html>
<body>
<form action="UsingSelectJDBC.jsp"
method="post">
Name : <input type="text"
name="sname">
<br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form></body></html> 49
JSP
<html><body>
<jsp:useBean id='select' scope='page'
class='trial.JDBCSelect' />
<h3>The address got
<%!
String s ="";
%><%
s = request.getParameter("sname");
%><%= select.connect(s)
%></h3></html>
50
JSP & ASP

A Comparative study
Architecture
• JSP, ASP separates the presentation
from the server-side coding content.
• JSP pages are entirely converted to
servlets.
• Concurrent usage of JSP, threads are
instantiated to handle the request.
• Threads are managed by the Web
Server Process.
• Efficient procedure than CGI.
52
Performance Vs.
Platform Independence
• Performance wise JSP and ASP takes the
same time.
• Java has an edge over ASP in platform
independence.

53
Session Management
• ASP uses Session object to manage user
information
• JSP maintains session through the
HttpSession object.
– Session information is stored on the server,
and a session ID number is stored in the
cookie on the client machine.
– If cookies are disabled, the SessionID should
be stored as a URL string.

54
Session Management
• Sessions are of 30 minutes.
• Items stored and retrieved from the
Session object cannot be primitive data
types.
• Must be casted to their object form.
HttpSession session = request.getSession();
session.getId();
Integer item = (Integer) session.getValue(“item”
session.putValue(“ItemValue”, itemName);

55
Application Management
• ASP uses Application Object
• JSP uses the ServletContext Object

getServletContext().setAttribute(“Item”,ItemValue);
Integer i = (Integer)getServletContext().
getAttribute(“ItemName”);

56
Server Side Includes
• to include virtual files
• In ASP
<!-- #include file = “myfile.asp”-->
– Entire page

• In JSP
<%@ include file = “myfile.jsp” %>
– Compiled result

57
JSP Directives
• include
–include a static or dynamic file
<%@ include file = “myfile.jsp” %>
• import
–the servlet should import a package
<%@ import = “java.util.*” %>

58
JSP Directives
• extends
–servlet extends a class
<%@ extends = “java.util.Dictionary”
%>
• implements
–implements an interface
<%@ implements =
“java.util.Enumeration” %>
59
JSP Directives
• content_type
–tells what type content is in the page
<%@ content_type = “text/plain” %>

60
The Environment
• ASP uses VBScript or JavaScript
• JSP uses pure Java
– Takes advantage of OOP

61
Components
• ASP uses COM components
• JSP uses Java Beans
– reusable components developed in
Java
– can be deployed in JSP

62
Databases
• ASP uses ADO and ODBC
• JSP uses JDBC & Beans

63
JSP Vs ASP
ASP JSP
Technology Technology
Reusable, Yes .JavaBeans,
Cross-platform No Enterprise JavaBeans,
Components custom JSP tags
Security against
No Yes
system crashes
Memory Leak
No Yes
Protection
VBScript,
Scripting Language Java
JScript
Customizable Tags No Yes
64
JSP Vs ASP
ASP JSP
Technology Technology
Compatible with Yes (using JDBC
Yes (COM)
Legacy Databases API)
Works with any Works with any
Ability to integrate ODBC- ODBC- and JDBC
with Data Sources compliant technology-compliant
database database
COM JavaBeans, EJB or
Components
components extensible JSP tags
Extensive Tool
Yes Yes
Support
65
Thank You!

You might also like