Sie sind auf Seite 1von 6

What is JSP?

 JSP technology-enabled engine will process elements on a .jsp page.


 A JSP based page includes JSP technology-specific tags, declarations, and
possibly scriptlets, in combination with other static (HTML or XML) tags.
 Java Server Pages technology offers a simple way to create dynamic Web pages
that are both platform-independent and server-independent
 Java technology pages are typically compiled into Java platform servlet classes

 Java Server PagesTM (JSP) technology provides a simplified, fast way to create
web pages that display dynamically-generated content.
 Some JSP technology-enabled engines may support other languages, but the JSP
1.0 specification only specifies a scripting language based on Java
programming language.
 The JSP specification, developed through an industry-wide initiative led by Sun
Microsystems, defines the interaction between the server and the JSP technology-
based page.

How JSP works?


 It passes any formatting (HTML or XML) tags directly back to the response page.
In this way, JSP technology-based pages separate the page logic from its design
and display.
 It uses a Java programming language-based scripting language
 JSP technology-based pages may call JavaBeans technology-based components
(beans) or Enterprise JavaBeans technology-based components (EJB) to
perform processing on the server.
 JSP technology-based pages are not restricted to any specific platform or web
server.
How JSP page invoked and compiled?
 Pages built using JSP technology are typically implemented using a translation
phase that is performed once, the first time the page is called.
 The page is compiled into a Java Servlet class and remains in server’s memory, so
subsequent calls to the page have very fast response times.

Why JSP?
 JSP technology-based pages are compiled into servlets, so theoretically you could
write servlets to support your web-based applications.
 However, JSP technology was designed to simplify the process of creating pages
by separating web presentation from web content.
 In many applications, the response is a combination of template data and
dynamically-generated data. In this situation, it is much easier to work with JSP
based pages than to do everything with Servlets.
Reference implementation for JSP
 The reference implementation is called as JavaServerTM Web Development Kit
1.0. Based on the 1.0 version of the JSP specification
 It includes
u a simple HTTP web server,
u a JSP technology-enabled engine
u a Java Servlet engine.
How JSP different from other products?
u JSP technology is the result of industry collaboration and is designed to be
an open, industry-standard method.
u It supports numerous servers, browsers and tools.
u JSP technology speeds development with reusable components and
tags, instead of relying heavily on scripting within the page itself.
u All JSP implementations support a Java programming language-based
scripting language, which provides inherent scalability and support for
complex operations.
Comparison of JSP and ASP
 JSP and Microsoft ASP technologies have many similarities. Both are designed to
create interactive pages as part of a Web-based application
 Advantages of JSP’s are over ASP
 Platform and Server Independence
 Extensible JSP Tags
 Reusability Across Platforms
 The Java Advantage
 Easier Maintenance
 Scalability in the Enterprise
JSP Element Basics
 JSP syntax is similar to XML and is actually based on XML
 The container tags of JSP are represented as follows
<somejsptag attributename=“attribute
value”>
body
</somejsptag>
 The empty tags of JSP are represented as follows
<somejsptag attributename=“attribute value”/>

Elements of a Java Server Page


 The Java code in the page includes
 Directives
 Declaratives
 Scripts
 Expressions
Session Management using Cookies
 Cookies can be used for session management in JSP
 Cookies are introduced to the client by including a Set-Cookie HTTP header as
part of an HTTP resonse
u Set-Cookie: Name=value; expires=Date; path=Path;
domain=Domain_Name; secure
u Creating a Cookie :
 Cookie info = new Cookie(“My Cookie”, “My Value”);
 info.setMaxAge(60*10);
 info.setPath(“/”);
 response.addCookie(info);

<%@ page language=“java” import=“java.text.DateFormat, java.util.Date” %>


<% boolean found = false;
Cookie info = null;
String msg= “This is the first time you’ve visited this page”;

Cookie[] cookies = request.getCookies();


for(int I=0; I<cookies.length; I++){
info = cookies[I];
if(info.getName().equals(“MyCookie”)){
found = true;
break;
}}
String newValue = “”+ System.currentTimeMillis();
if(! Found){
info = new Cookie(“MyCookie”), newValue);
info.setMaxAge(60*10);
info.setPath(“/”);
response.addCookie(info);
} else {
long conv = new Long(info.getValue()).longValue();
msg = “You last visited this site on “ + new Date(conv);
info.setValue(newValue);
info.setMaxAge(10*24*60*60);
info.setPath(“/”);
response.addCookie(info);
} %>
<html><body><h2><%= msg %>
<h2> Current system date is <%= new Date().toString() %> </h2>
</body></html>
Session Management using Session Object
 Session Object is implicitly available for a jsp page
 Session objects are handled in the same way as in a servlet as it is an instance of
HttpSession
 Methods available :
 long getCreationTime()
 String getId()
 long getLastAccessedTime()
 void setMaxInactiveInterval(int interval)
 int getMaxInactiveInterval()
 void invalidate()
 boolean isNew()
 Object getAtrribute(String name)
 String[] getAttributeNames()
 void setAttribute(String name, Object value)
 void removeAttribute(String name)
Session Management with URL rewriting
 URL rewriting involves appending identification information to the end of every
link on the page and uses the PATH-INFO header to extract it
 <a href=“/mypage.jsp?login=login_name&item=book1”> Next
</a>
 In order to support URL rewriting one should avoid writing a URL straight to the
output stream
 The HttpServletResponse.encodeURL() method can be used instead
 <a href=<%=response.encodeURL(“/mypage.jsp”) %> > Next
</a>
 response.sendRedirect(response.encodeURL(“/myhome/mydir/my
page.jsp”))
 This determines if the URL needs to be rewritten, and if so, it rewrites it by
including the sessionID in the URL
JSP with Databases
 JSPs can access a variety of data sources like relational directory proprietary
databases servers file formats
 XML formatted documents object databases.
 JSPs most likely use jdbc packages to connect to different data sources .
 JDBC API is made up of the classes and interfaces found in the java.sql and
java.text packages.
Connection to the Database
<%@ page language=“java” import=“java.sql.*” %>
<body>
<% Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection conn = DriverManager.getConnection(“jdbc:odbc:dsn”);
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery(“Select * from table”);
while(rset.next()){
out.println(“<tr><td>”+rset.getSring(1)+”<td>”+rset.getSring(1)+”</tr>
}
%>
</body>
Using JavaBeans to get connection
 Java Bean components can be used to get the database connection
import java.sql.*;
import java.io.*;
public class sqlBean{
String driver = “sun.jdbc.odbc.JdbcOdbcDriver”);
Connection conn=null
void makeConnection(){
Class.forName(driver);
conn = DriverManager.getConnection(“jdbc:odbc:dsn”);
}
}
Import java.sql.*;
import java.io.*;
public class empBean extends sqlBean{
String myEmpSql = “select * from emp”;
ResultSet rset = null;
Statement stmt = null;
public boolean getNextEmployee() throws Exception{
return rset.next();
}
public boolean getEmployees() throws Exception{
rset = stmt.executeQuery(myEmpSql);
return (rset != null);
}
public String getColumn(String incol) throws Exception{
return rset.getString(incol);
}
}
<%@ page language=“java” import=“java.sql.*” %>
<jsp:useBean id=“empbean” class=“empBean” scope=“page” />

<%
empbean.makeConnection();
if(empbean.getEmployees()){
while(empbean.getNextEmployee()){
String eid = empban.getColumn(“empid”);
String ename = empban.getColumn(“ename”);
String job = empban.getColumn(“job”);
String sal = empban.getColumn(“sal”);
%>
<tr><td><%=empid%><td><%=emname%><td>
<td><%=job%><td><%=sal%><td></tr>
<% }}
%>

Pooling Connection Objects


* Slowest step in running a database-enabled JSP is initial connection to the database,
which causes a performance hit only the first time the JSP is loaded.
* Serving clients as efficient as possible is important
* Reusing a set of objects is often called Object Pooling
* Classic connection pool
* With the classic model, an application
* Gets a reference to the pool or an object managing many pools.
* Gets a connection from a pool,
* Uses the connection,
* Returns the connection to the pool

Das könnte Ihnen auch gefallen