JSP introduction - sheets

judgedrunkshipServers

Nov 17, 2013 (3 years and 8 months ago)

129 views

19 augustus
2003augustus 2003

JSP



Web Technology

Week 4

BICT

2

J2EE = Java 2 Enterprise Edition


Servlet


Server
-
side Java
-
programs


Process incoming requests from clients


Supported through servlet engine(s)


Example: Tomcat (Apache)


JSP = Java Server Pages


Mixes HTML and Java code


Extension of servlet model


JSP page is compiled into servlet


EJB = Enterprise Java Beans


See next section (part 2 course)


BICT

3

Market shares…..


JSP continues fast growth, on a surprisingly diverse set of
operating systems

Last
December

we reported that the number of JSP sites had grown quickly during 2002, albeit from a small starting point. Reviewing the gro
wt
h over the past year since
July 2002, JSP continues to enjoy fast growth with a 94% increase in ip addresses running JSP based sites to over 44,000 ip a
ddr
esses running some 105,000 active sites.

More surprising is the composition of these sites choice of operating systems. One might expect that by far the most common o
per
ating system amongst JSP based sites
would be Solaris, given JSP's links with application servers such as Weblogic, IBM Websphere, Oracle, and Apache Tomcat. Howe
ver
, Solaris is only placed 3rd with 17%
behind Linux with 40% and Windows with 26%.



BICT

4

Introduction to JSP


What is JavaServer Pages(JSP)?


A technology developed by Sun Microsystems that is used to create dynamic
web sites based on Java and Java Servlet technologies


Java, Servlet, and JSP


Both Servlet and JSP are based on
Java

technology.
Servlet

is one component
of a suit of Java products.
JSP

technology is an extension of the servlet
technology. One JSP will be interpreted as one class of Java/Servlet.


Java Server Pages (JSPs) provide a way to separate the generation of
dynamic content (java) from its presentation (html)


JSP specification builds on the functionality provided by the servlet
specification.



BICT

5

Getting Started with JSP


A JSP is a HTML page with scripting elements embedded in it.


A JSP is


first translated into a servlet and


then executed in the Servlet engine


BICT

6

How do JSP’s Work

Browser

(e.g., IE)

Servlet/JSP

Server
(e.g. Tomcat )

Generated

Servlet

index.jsp

index.java

compiles

checks

converts

Forwards to

DBMS

(e.g. MySQL)

BICT

7

Your First JSP

<HTML>

<BODY>

Hello!


The time is now

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


</BODY>

</HTML>


Save it as hello.jsp

BICT

8

Another example

<HTML>

<BODY>

<%
--

the comments go here
--
%>

<%

//The following code is used to demonstrate how java code works

String myClassName = "inls259";

out
.println("<B> demon of "+myClassName+"</B></br>");

java.util.Date date = new java.util.Date();

%>

Hello! The time is now
<%=

date
%>

<%

out
.println("</br>Host: "+
request
.getRemoteHost());


%>

</BODY>

</HTML>


out

is the
javax.servlet.jsp.JspWriter

while
request

is
javax.servlet.http.HttpServletRequest


BICT

9

Scriptlets


Java statements can be enclosed in <% … %> inside a jsp

<%



System.out.println( "Evaluating date now" );



java.util.Date date = new java.util.Date();

%>

BICT

10

Mixing Scriptlets and HTML

<HTML>

<HEAD>

<TITLE>Greetings</TITLE>

</HEAD>

<BODY>

<H2>Greetings</H2>

<% if (Math.random() < 0.5) { %>

Have a <B>nice</B> day!

<% } else { %>

Have a <B>lousy</B> day!

<% } %> </BODY>

</HTML>

BICT

11

Mixing Scriptlets and HTML


Everything that you want to be executed has to be in <% … %>


Everything that you want to be displayed as is in the browser
has to be outside any <% … %>



<TABLE BORDER=2>

<%


for ( int i = 0; i < n; i++ ) {


%>



<TR> <TD>Number</TD>


<TD>
<%= i+1 %>

</TD> /TR>

<%



}

%>

</TABLE>


BICT

12

JSP Declarations


JSP scriptlets are considered as one method in one class


Variables and Methods can also be added in declarations


add a declaration, you must use the <%! and %>


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

<HTML>

<BODY>

<%!




Date theDate = new Date();




Date getDate() {





return theDate;




}

%>

Hello!


The time is now <%= getDate() %>

</BODY>

</HTML>


BICT

13

JSP Directive


Directives provide global information to the JSP engine


For example, a directive can be used to import java classes.


Directive elements have a syntax of the form

<%@ directive … %>


Directives are JSP constructs that affect the jsp pages, but they are not
Java constructs



Example:


page directive:


<%@ page import="java.util.*, java.text.*" %>


include directive: physically includes content or a page


<%@ include file="hello1.jsp" %>



BICT

14

Scripting Elements



Allow java code


variable or method declarations, scriptlet, and
expressions.


Declaration tag <%! … %>


Scriptlet tag <% … %>


Expression tag <%= … %>

BICT

15

Declaration Tag


<%! … %>


Allows you to declare page wide variables and methods.


<%! int counter = 0; %>

<%! Vector beanList = new Vector(); %>



Methods and variables have class scope


Note, code must end with ; like any java code

BICT

16

Scriptlet Tag


<% … %>


Used to include small pieces of Java code


<% for(Enumeration e = beanList.elements(); e.hasMoreElements(); ) {

UserBean uBean = (UserBean) e.nextElement();

out.println( uBean.getUserName() );

}

%>

BICT

17

Expression Tag


<%= … %>


Accepts any Java expression, evaluates the expression, converts to a
String, and displays.

<%= counter %>

<%= uBean.getUserName() %>

Short hand for:

<% out.println( uBean.getUserName() ); %>

BICT

18

Page Directives


The page directive defines a number of page dependent attributes

<%@ page language=“Java”

[ extends=“className” ]

[ import=“importList” ]

[ session= “true|false” ]

[ buffer=“none|sizekb” ]

[ autoFlush=“true|false” ]

[ isThreadSafe=“true|false” ]

… %>

BICT

19

Page Directive


If language attribute is set, must be = “Java”


Default import list is java.lang.*, javax.servlet.*, javax.servlet.jsp.* and
javax.servlet.http.*.


If session = “true” then default session variable of type
javax.servlet.http.HttpSession references the current/new session for the
page.

BICT

20

JSP Sessions


Form page


Sent to other

page


Starts session


Next


Continues
session

<HTML>

<BODY>

<FORM METHOD=POST ACTION="SaveName.jsp">

What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20>

<P><INPUT TYPE=SUBMIT>

</FORM>

</BODY>

</HTML>


<%


String name = request.getParameter( "username" );


session.setAttribute( "theName", name );

%>

<HTML>

<BODY>

<A HREF="NextPage.jsp">Continue</A>

</BODY>

</HTML>


<HTML>

<BODY>

Hello, <%= session.getAttribute( "theName" ) %>

</BODY>

</HTML>


BICT

21

Include Directive


The include directive is used to inline text and/or code at JSP page
translation
-
time.



<%@ page include file=“relativeURL” %>

<%@ page include=“/navbar.html”%>

BICT

22

Implicit objects


out
,
request
,
response
,
application
,
config
,
exception
,
page
,
pageContext
,
and
session


Out

send output to client web browser


Request

store inf1112ormation supplied by a client, including form data


Response

store information generated by web server


Session

store information associated with a session


Application

store information about an application related to a specific
directory


Config

store information about configuration of the environment


Exception

handle errors


Page

typically not accessed from JSP


pageContext

access characteristics of a JSP page


BICT

23

Examples


application


application.setAttribute(“siteName”,”inls259”


application.getAttribute(“siteName”)


Request


String name =request.getParameter(“userName”)


String[] names =request.getParameterValues(“friends”);


Out


out.flush()


Response


Response.addCookie(myCookie)



BICT

24

Jbuilder and running JSP/Servlets


Tomcat server delivered with Jbuilder


Tomcat automatically


Started when <run program>


Stopped when program is stopped



Simple web
-
app


New project


New


web


JavaServerPage


(web module


new


empty web module)


whatever (first
page


submit form?)


finish



Add other JSP
-
pages


One .jsp
-
page is started as default


Run application


Application and classes copied to right location


While running: Tomcat server can be addressed from other machines


http://Ip
-
adres:8080