JSP Notes
JSP Notes
Advantages of jsp
It gives tag based programming.Suitable for both java and non java programmers.
It gives 9 implicit objects(built-in object).An object which is used directly without configuration
explicitly.
Request and response is not a implicit objects.
super and this is a implicit objects/ref variable of java app.
It allows us to develop custom tags and to work with third party supplied tags.
Handling an exception is always optional.
Configuration of web.xml is optional.
Note:
All Industries uses jsp and servlets to develop web applications. Procedure to develop and
deployed JSP program based web application
1
Diagram:
ABC.jsp
<br><i>Date and Time </i></br>
<% java.util.Date d=new java.util.Date();
out.println(d.toString()); %>
<br><br>
End of jsp program.....
web.xml
<web-app/>
Note:
We don't need to compile jsp program so we don't need to add any jar file in "CLASSPATH"
environmental variables.
request url
http://localhost:2525/JspApp/ABC.jsp
2
<jsp-file>/index.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/test1</url-pattern>
</servlet-mapping>
</web-app>
How can we hide jsp programe getting accessible through file name(it means how can we access
jsp program by using url pattern)
Diagram:
In web.xml
We need to configure
<web-app>
<servlet>
<servlet-name>abc</servlet-name>
<jsp-file>/WEB-INF/index.jsp</jsp-file>
3
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/test1</url-pattern>
</servlet-mapping>
</web-app>
Note:
All life cycle methods of jsp program will executed through life cycle methods of Servlet
program.
4
Life cycle of jsp diagram
Diagram:1
_jspinit() is used to perform initialization like opening jdbc objects(Connection,Statement ,ResultSet and
etc).
In tomcat server JES class object will be created(ABC.jsp.class and ABC.jsp.java) in work folder
dynamically.
ex:
D:\Tomcat 6.0\work\Catalina\localhost\JspApp\org\apache\jsp.
By default JES class will give only one life cycle method that is _jspService(),but we can see _jspInit() and
_jspDestroy() in JEs class but these methods are helper methods.
5
Phases of Jsp program
Every jsp program having two phases
1)Translation phase
Diagram:2
Ans:
<web-app>
6
<servlet>
<servlet-name>abc</servlet-name>
<jsp-file>/ABC.jsp</jsp-file>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/test1</url-pattern>
</servlet-mapping>
</web-app>
Our Jsp program container will complete Translation phase during server startup or during deployment
of web application.
jsp container performs initialization and instantiation of JES class object during server startup or during
deployment of web application.
Jsp tags/elements
Scripting tags
It is used to place java code
1)scriptlet tag
ex:
---
<% %>
2)Declaration tag
ex:
---
<%! %>
3)Expression tag
ex:
7
--
<%= %>
jsp comments
<%-- %>
Directive tags
1)Page directive
ex:
<@page attribute>
2)Include directive
ex:
<@include attribute>
standard tags
<jsp:include>
<jsp:forward>
<jsp:getProperty>
<jsp:setProperty>
<jsp:param>
<jsp:params>
<jsp:body>
SP scriptlet tag
A scriptlet tag is used to execute java source code in JSP.
Syntax is as follows:
<% java source code %>
8
Example of JSP scriptlet tag that prints the user name
File: index.html
1. <html>
2. <body>
3. <form action="welcome.jsp">
4. Enter username:<input type="text" name="uname">
5. <input type="submit" value="go"><br/>
6. </form>
7. </body>
8. </html>
File: welcome.jsp
1. <html>
2. <body>
3. <%
4. String name=request.getParameter("uname");
5. out.print("welcome "+name);
6. %>
7. </form>
8. </body>
9. </html>
9
index.jsp
1. <html>
2. <body>
4. </body>
5. </html>
index.jsp
1. <html>
2. <body>
5. </body>
6. </html>
index.jsp
1. <html>
2. <body>
3. <%!
5. return n*n*n;
10
6. }
7. %>
9. </body>
10. </html>
• process.jsp for dividing the two numbers and displaying the result
• error.jsp for handling the exception
index.jsp
1. <form action="process.jsp">
5. </form>
process.jsp
11
2. <%
3.
4. String num1=request.getParameter("n1");
5. String num2=request.getParameter("n2");
6.
7. int a=Integer.parseInt(num1);
8. int b=Integer.parseInt(num2);
9. int c=a/b;
11. %>
error.jsp
1. <%@ page isErrorPage="true" %>
2.
4.
Specifying the <error-page> element in web.xml file.This approach is better because you don't
need to specify the errorPage attribute in each jsp page. Specifying the single entry in the
web.xml file will handle the exception
There are 4 files:
12
1) web.xml file if you want to handle any exception
1. <web-app>
2.
3. <error-page>
4. <exception-type>java.lang.Exception</exception-type>
5. <location>/error.jsp</location>
6. </error-page>
7.
8. </web-app>
• import
• contentType
• extends
• info
• buffer
• language
• isELIgnored
• isThreadSafe
• autoFlush
• session
13
• pageEncoding
• errorPage
• isErrorPage
1)import
The import attribute is used to import class, interface or all the members of a package.It is
similar to import keyword in java.
2)contentType
The contentType attribute defines the MIME
(Multipurpose Internet Mail Extension) type of the HTTP response.
The default value is "text/html".
Example of contentType attribute
1. <html>
2. <body>
3.
4. <%@ page contentType=application/msword %>
5. Today is: <%= new java.util.Date() %>
14
6.
7. </body>
8. </html>
3)extends
The extends attribute defines the parent class that will be inherited by the generated servlet.It is
rarely used.
4)info
This attribute simply sets the information of the JSP page which is retrieved later by using
getServletInfo() method of Servlet interface.
Example of info attribute
1. <html>
2. <body>
3.
6.
7. </body>
8. </html>
5)buffer
The buffer attribute sets the buffer size in kilobytes to handle output generated by the JSP page.
1. <html>
2. <body>
3.
6.
15
7. </body>
8. </html>
6)language
The language attribute specifies the scripting language used in the JSP page. The default value is "java".
Action tags
Action tags provides functionality with the support of servlet api features.
ex:
Action include
Note:Here two JES class will be create one is for A.jsp and another for B.jsp
16
Diagram:3
Diagram:4
17
web.xml
<web-app/>
A.jsp
<b>Beginning of A.jsp program</b>
<br>
<jsp:include page="B.jsp"/>
<br>
B.jsp
<br>
<br>
request url
http://localhost:2525/IncludeApp/A.jsp
Action forward
18
<jsp:forward> in source jsp program as conditional statement.Here seperate JES classes will be
created one is for A.jsp and another for B.jsp
Diagram:5
Diagram:6
19
web.xml
<web-app/>
A.jsp
<b>Beginning of A.jsp program</b>
<br>
<jsp:include page="B.jsp"/>
<br>
B.jsp
<b>Beginning of B.jsp program</b>
<br>
<br>
request url
http://localhost:2525/IncludeApp/A.jsp
we need to place java code in servlet class to create java bean object and to call setters and
getters method for Jsp to javaBean communication
1)<jsp:useBean>tag
20
2)<jsp:setProperty>tag
3)<jsp:getProperty>tag
<jsp:useBean>tag
It is used to create and locate java bean class object.
<jsp:setProperty>tag
it is used to call setXxx() method and to set the data to bean property.
<jsp:getProperty>tag
It is used to call getXxx() method and to read the data from bean property.
Diagram:7
21
Calculator.java
package com.vn;
class Calculator
{
public int cube(int n)
{
return n*n*n;
}
}
index.jsp
<jsp:useBean id="obj" class="com.vn.Calculator"/>
out.print("Cube of 5 is "+m);%>
web.xml
<web-app/>
Request url
http://localhost:2525/UseBeanApp1/index.jsp
index.html
For input of values.
22
process.jsp
File that sets the incoming values to the bean object and prints the one value.
User.java
Bean class that have setter and getter methods.
Index.html
<form action="process.jsp" method="post">
</form>
process.jsp
Record:<br>
User.java
class User
23
private String email;
this.name=name;
return name;
this.password=password;
return password;
this.email=email;
return email;
Request url
http://localhost:2525/UseBeanApp2/index.html
24
25