Вы находитесь на странице: 1из 16

JSP Tutorial

Java Server Pages or JSP for short is


Sun's solution for developing dynamic
web sites.

JSP provide excellent server side


scripting support for creating database
driven web applications.

JSP

JSP enable the developers to directly


insert java code into jsp file, this makes
the development process very simple
and its maintenance also becomes very
easy.

Java Server Pages are saved with .jsp


extension.

JSP

JSP pages are efficient, it loads into the


web servers memory on receiving the
request very first time and the
subsequent calls are served within a
very short period of time.

Java is known for its characteristic of


"write once, run anywhere." JSP pages
are platform independent.

JSP scripting elements

There is more than one type of JSP tag,


depending on what you want done with the Java
<%=expression%>

The expression is evaluated and the result is inserted into


the HTML page

<%code%>
The code is inserted into the servlet's service method
This construction is called a scriptlet

<%!declarations%>
The declarations are inserted into the servlet class, not into a

method

Scriptlets

Scriptlets are enclosed in <% ... %> tags


Scriptlets do not produce a value that is inserted directly into

the HTML (as is done with <%= ... %>)


Scriptlets are Java code that may write into the HTML
Example:
<% String queryData = request.getQueryString();
out.println("Attached GET data: " + queryData); %>

Scriptlets are inserted into the servlet exactly as


written, and are not compiled until the entire servlet
is compiled
Example:

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


Have a <B>nice</B> day!
<% } else { %>
Have a <B>lousy</B> day!
<% } %>

Directives

Directives affect the servlet class itself


A directive has the form:
<%@ directive attribute="value" %>
or
<%@ directive attribute1="value1"
attribute2="value2"
...
attributeN="valueN" %>
The most useful directive is page, which
lets you import packages
Example: <%@ page import="java.util.*" %>

JSP
<%@page contentType="text/html" %>
<html>
<body>
<%!
int cnt=0;
private int getCount(){
//increment cnt and return the value

JSP
cnt++;
return cnt;
}
%>
<p>Values of Cnt are:</p>
<p><%=getCount()%></p>
<p><%=getCount()%></p>

JSP
<p><%=getCount()%></p>
<p><%=getCount()%></p>
<p><%=getCount()%></p>
<p><%=getCount()%></p>
</body>
</html>
<%= new java.util.Date() %>

JSP
<html>
<head>
<title>Enter your name</title>
</head>
<body>
<p>&nbsp;</p>
<form method="POST"
action="showname.jsp">

JSP
<p><font color="#800000" size="5">Enter
your name:</font><input type="text"
name="username" size="20"></p>
<p><input type="submit" value="Submit"
name="B1"></p>
</form>
</body>
</html>

JSP

The target of form is "showname.jsp",


which displays the name entered by the
user. To retrieve the value entered by
the user we uses the

request.getParameter("username");

JSP
Here is the code of "showname.jsp" file:
<%@page contentType="text/html" %>
<html>
<body>
<p><font size="6">Welcome :&nbsp; <
%=request.getParameter("username")
%></font></p>
</body>
</html>

JSP

The <%= ... %> tag is used, because we are


computing a value and inserting it into the HTML

JSP provides several predefined variables

request : The HttpServletRequest parameter


response : The HttpServletResponse parameter
session : The HttpSession associated with the

request, or null if there is none


out : A JspWriter (like a PrintWriter) used to send
output to the client

JSP in XML

JSP can be embedded in XML as well as in


HTML
Due to XMLs syntax rules, the tags must be
different (but they do the same things)
HTML: <%= expression %>

XML: <jsp:expression>expression</jsp:expression>
HTML: <% code %>
XML: <jsp:scriptlet>code</jsp:scriptlet>
HTML: <%! declarations %>
XML: <jsp:declaration>declarations</jsp:declaration>
HTML: <%@ include file=URL %>
XML: <jsp:directive.include file="URL"/>
16

Вам также может понравиться