Professional Documents
Culture Documents
Scripting Elements
Scripting elements allow Java code to be written
directly into the JSP page. It is compiled and
available within the page. There are three kinds of
scripting elements:
• declarations
• expressions
• scriplets
1.Declarations
Declarations are used to define variables and
methods -- both instance and static -- that are likely
to be repeated. Thus, they correspond to writing
code at the Java Class level. They can also be used
to overwrite jspInit or jspDestroy methods that are
created when the JSP is compiled.
The basic form is:
<%! Java variable or method %>
Example:
<%! String message; %>
2. Expressions
Expressions are references to variables, methods, or
composable structures of such. They are most often
used as a way of dynamically inserting a value into
a parameter slot. You may also think of them as
representing a call to a static method. They are
evaluated at run time and the results inserted into
the output stream at the location of the expression.
The basic form is:
<%= Java expression %>
Example:
<h2><font color= " #AA0000 " > <%=message
%></font></h2>
Note that expressions do not include a semicolon(;).
3. Scriplets
Scriplets are sections of Java code that are executed
in place. They can be as simple as a declaration of
Directives
Directives are instructions to the JSP compiler. The
three types of directives are:
UseBean
The useBean action is by far the most powerful and
the most complex of the JSP actions. It allows a
JSP to create an instance or receive an instance of a
Java Bean.
The JSP could provide typical servlet controller
functions by passing the request object to the bean
and allowing it to extract user parameters and call
back-end functions, just like a servlet. However,
supporting those functions in a servlet is generally
considered a better practice. However, rather than
transferring result values to another type, such as a
Hash table, and passing that object to a JSP, if a
back-end process returned a result bean back to a
controller servlet, it could simply pass this bean to
the relevant JSP.
In fact, so long as the Java server supports it and is
properly configured, it permits beans to be passed
from servlet to JSP, to another JSP, to another
servlet, etc. This is called chaining and is an
Programs
-------------
1. HelloWorld.jsp
2. a.jsp
-----------
<body>
<% @ page language="Java" %>
<% out.println("hello world");%>
</body>
<BODY>
<H1>JSP Declarations</H1>
</BODY>
</HTML>
5.Loop.jsp
-------------
(or)
<body>
<% for (int i=0;i<5;i++){
out.println("<b>Hello</b><br>");
}%>
</body>
6.Expr1.jsp
-------------
<body>
<h1>Data in various sizes</h1>
<%for(int i=1;i<7;i++){%>
<font size=<%=i%>>Hello in size<%=i%>
</font>
<br>
<%}%>
</body>
7.Expr2.jsp
9.Ex.html
----------
<body>
here specify the format either "excel" or "html"
<form name="f1" action="ApplesAndOranges.jsp">
<br><input type="text" name="format" ><br>
Ex.jsp
-----------
<HTML>
<HEAD>
<TITLE>Comparing Apples and
Oranges</TITLE>
</HEAD>
<BODY>
<CENTER>
<H2>Comparing Apples and
Oranges</H2>
<%
String format =
request.getParameter("format");
if ((format != null) &&
(format.equals("excel"))) {
response.setContentType("applicat
ion/vnd.ms-excel");
}
%>
<TABLE BORDER=1>
<TR><TH></TH><TH>Apples<TH>Oranges
<TR><TH>First
Quarter<TD>2307<TD>4706
<body>
<%! public int mysum(int a,int b)
{
return a+b;
%>
<%out.println("<h1>sample method
invocation</h1>");
int a=mysum(10,20);
out.println("data is "+a);%><br><b>
<%out.println(" sum is "+mysum(100,300);%>
</b>
</body>
11. page1.html
---------
<body>
<form action="rec.jsp">
Name:-<input type="text" name="t1"><br>
<input type="submit" name=b1
value="send">
</form>
</body>
rec.jsp
-----------
<body>
Html prg:-
<html>
<head>
<title> Insert data into the database </title>
</head>
<body bgcolor="grey" background="a.jpg">
<form name="f1" action="r.jsp">
<div align="center">
<h2>Inserting Data into the data
base</h2><hr>
<table border=0 >
<tr><td>No:- <input type="text"
name="t1"></td>
JSP prg:-
<body background="a.jpg">
<%@ page language="java"
contentType="text/html" %>
<%@ page import ="java.sql.*" %>
<%! public int insertRec (String a,String b)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c =
DriverManager.getConnection("jdbc:odbc:mydsn1");
Statement st = c.createStatement();
int i = st.executeUpdate("insert into e
values("+a+",'"+b+"')");
st.close();
c.close();
int i = insertRec(s1,s2);
HTML PRG:-
<html>
<head>
<title> Read data from data base </title>
</head>
<body bgcolor="grey" background="a.jpg">
<form name="f2" action="r1.jsp">
<div align="center">
JSP PRG:-
<html>
<head><title >Table values are</title>
</head>
<body background="a.jpg">
<%@ page language="java"
contentType="text/html" %>
<%@ page import ="java.sql.*" %>
<h1><center> Tables values are</h1>
<hr>
<table border="2" cellspacing="10"
cellpadding="10">
<tr><th>Eno</th><th>Ename</th></tr>
<%
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
<%--
<!--
Menu.html
-->
<OL>
<LI>Home</LI>
<LI>AppServer</LI>
<LI>Examples</LI>
<LI>About Us</LI>
</OL>
<!--
<!--
Footer.html
-->
<TR><TD>
<CENTER>Griet</CENTER>
</TD></TR></TABLE></BODY></HTML>
<!--
CompositeView.jsp
-->
<%@ include file="Header.html"%>
<TR>
<TD width="20%"><%@ include
file="Menu.html"%></TD>
<TD><%@ include
file="BodyContent.html"%></TD>
</TR>
<jsp:include page="Footer.html"/>
/*
Example on jsp:useBean
*/
jsp\
EmpQuery.jsp
\WEB-INF\classes\beans\
EmpBean.java
<!--
EmpQuery.html
-->
<FORM action="./EmpQuery.jsp">
Empno <INPUT type="text" name="empno"
value="">
<INPUT type="submit" name="submit"
value="Submit">
</FORM>
<!--
// EmpBean.java
import java.io.*;
public class EmpBean implements Serializable
{
private int empno;
private String ename, job;
private double sal;
public EmpBean()
{}
/*
public void setEmpno(int empno)
java.sql.Connection
con=java.sql.DriverManager.getConnection("jdbc:od
bc:mydsn", "scott", "tiger");
java.sql.Statement stmt=con.createStatement();
java.sql.ResultSet rs=stmt.executeQuery("select
empno, ename, job, sal from emp where
empno="+empno);
if(rs.next())
{
this.ename=rs.getString(2);
this.job=rs.getString(3);
this.sal=rs.getDouble(4);
}// if()
rs.close();
<!--
15.This application seperates business logic and
control layers from presentation layer
-->
<!--
EmpQuery.html
-->
<FORM action="./EmpQuery1.jsp">
Empno <INPUT type="text" name="empno"
value="">
<INPUT type="submit" name="submit"
value="Submit">
</FORM>
<!--
EmpQuery1.jsp
<!--
EmpResult1.jsp
-->
<%
beans.EmpBean
emp=(beans.EmpBean)pageContext.getAttribute("em
p",pageContext.SESSION_SCOPE);
%>
<TABLE border="1" align="center">
<TR>
<TH>EMPNO</TH>
<TH>ENAME</TH>
<TH>SAL</TH>
<TH>JOB</TH>
</TR>
<TR>
<TD><%=emp.getEmpno()%></TD>
<TD><%=emp.getEname()%></TD>
// EmpBean.java
import java.io.*;
public class EmpBean implements Serializable
{
private int empno;
private String ename, job;
private double sal;
public EmpBean()
{}
/*
public void setEmpno(int empno)
{
this.empno=empno;
}
*/
public void setEname(String ename)
{
this.ename=ename;
}
public void setJob(String job)
{
this.job=job;
java.sql.Statement stmt=con.createStatement();
java.sql.ResultSet rs=stmt.executeQuery("select
empno, ename, job, sal from emp where
empno="+empno);
if(rs.next())
{
this.ename=rs.getString(2);
this.job=rs.getString(3);
this.sal=rs.getDouble(4);
}// if()
rs.close();
stmt.close();
con.close();
}// try
catch(Exception e)
{
e.printStackTrace();
}// catch()
}// setEmpno()
}// class
jsp\
EmpQuery.html
EmpResult.jsp
\WEB-INF\web.xml
.\classes\EmpQueryServlet
.\beans\EmpBean.java
<!--
EmpResult.jsp
-->
<%
beans.EmpBean
bean=(beans.EmpBean)session.getAttribute("emp");
%>
<TABLE border="20" align="center">
<TR>
<TD><%=bean.getEmpno()%></TD>
<TD><%=bean.getEname()%></TD>
<TD><%=bean.getSal()%></TD>
<TD><%=bean.getJob()%></TD>
</TR>
</TABLE>
// EmpQueryServlet.java
req.getSession().setAttribute("emp", bean);
getServletContext().getRequestDispatcher("/EmpRes
ult.jsp").forward(req,resp);
}// service()
}// class
web.xml
---------
<web-app>
<servlet>