JSP in Java II Unit
JSP in Java II Unit
A JSP page consists of HTML tags and JSP tags. The JSP pages are easier to maintain than
Servlet because we can separate designing and development. It provides some additional features
such as Expression Language, Custom Tags, etc.
Now let us discuss JSP which stands for Java Server Pages. It is a server-side
technology. It is used for creating web applications. It is used to create dynamic web
content. In this JSP tags are used to insert JAVA code into HTML pages. It is an
advanced version of Servlet Technology. It is a Web-based technology that helps us to
create dynamic and platform-independent web pages. In this, Java code can be inserted
in HTML/ XML pages or both. JSP is first converted into a servlet by JSP container
before processing the client’s request.
Servlet JSP
Writing code for servlet is harder than JSP JSP is easy to code as it is java in
as it is HTML in java. HTML.
Servlet plays a controller role in the MVC JSP is the view in the MVC
approach. approach for showing output.
Servlet can accept all protocol requests. JSP only accepts HTTP requests.
Everything in the page that is not a JSP element is called template text . Template
text can really be any text: HTML, WML, XML, or even plain text. Since HTML
is by far the most common web page language in use today, most of the
descriptions and examples in this book are HTML-based, JSP has no dependency
on HTML; it can be used with any markup language. Template text is always
passed straight through to the browser.
Figure: Template text and JSP elements
When a JSP page request is processed, the template text and the dynamic content
generated by the JSP elements are merged, and the result is sent as the response to
the browser.
JSP Elements
The directive elements, shown in Table are used to specify information about the
page itself that remains the same between page requests, for example, the scripting
language used in the page, whether session tracking is required, and the name of a
page that should be used to report errors, if any.
Scriptlet tag
This tag allow user to insert java code in JSP. The statement which is written will be moved to
jspservice() using JSP container while generating servlet from JSP. When client make a request, JSP
service method is invoked and after that the content which is written inside the scriptlet tag executes.
<html>
<body>
<% out.print("Welcome to jsp page"); %>
</body>
</html>
Explanation
The Syntax of JSP Scriptlet tag is begin with ”.
We can write our java code inside this tag.
In java we use System.out.println for printing anything on console. In JSP, we use only out.print to
write something on console because the out we’re referring to isn’t System.out, it’s a variable in the
effective method that wraps our JSP page.
System.out writes to the servlet container’s console (usually a log file); out is a different class
entirely which writes to the output stream for the generated response.
Expression tag is one of the scripting elements in JSP. Expression Tag in JSP is used for writing your
content on the client-side. We can use this tag for displaying information on the client’s browser. The
JSP Expression tag transforms the code into an expression statement that converts into a value in the
form of a string object and inserts into the implicit output object.
Syntax: JSP tag
In the Scriptlet tag, it’s Evaluated a Java expression. Does not display any result in the HTML
produced. Variables are declared to have only local scope, so cannot be accessed from elsewhere in
the .jsp. but in Expression Tag it’s Evaluates a Java expression. Inserts the result (as a string) into the
HTML in the .js
We don’t need to write out.println in Expression tag for printing anything because these are
converted into out.print() statement and insert it into the _jspService(-, -) of the servlet class by the
container.
<html>
<body>
</body>
</html>
JSP Declaration
A declaration tag is a piece of Java code for declaring variables, methods and classes. If we
declare a variable or method inside declaration tag it means that the declaration is made inside the
servlet class but outside the service method.
We can declare a static member, an instance variable (can declare a number or string) and
methods inside the declaration tag.
JavaBeans Properties
A JavaBean property is a named attribute that can be accessed by the user of the object. The
attribute can be of any Java data type, including the classes that you define.
A JavaBean property may be read, write, read only, or write only. JavaBean properties are
accessed through two methods in the JavaBean's implementation class −
setPropertyName()
JavaBeans Example
public StudentsBean() {
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public int getAge(){
return age;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public void setAge(Integer age){
this.age = age;
}
}
Accessing JavaBeans
The useBean action declares a JavaBean for use in a JSP. Once declared, the bean becomes a
scripting variable that can be accessed by both scripting elements and other custom tags used in
the JSP. The full syntax for the useBean tag is as follows −
<jsp:useBean id = "bean's name" scope = "bean's scope" typeSpec/>
Here values for the scope attribute can be a page, request, session or application based on your
requirement. The value of the id attribute may be any value as a long as it is a unique name
among other useBean declarations in the same JSP.
Following example shows how to use the useBean action −
<html>
<head>
<title>useBean Example</title>
</head>
<body>
<jsp:useBean id = "date" class = "java.util.Date" />
<p>The date/time is <%= date %>
</body>
</html>
You will receive the following result − −
The date/time is Thu Sep 30 11:18:11 GST 2010
<body>
<jsp:useBean id = "students" class = "com.tutorialspoint.StudentsBean">
<jsp:setProperty name = "students" property = "firstName" value = "Zara"/>
<jsp:setProperty name = "students" property = "lastName" value = "Ali"/>
<jsp:setProperty name = "students" property = "age" value = "10"/>
</jsp:useBean>
<p>Student Age:
<jsp:getProperty name = "students" property = "age"/>
</p>
</body>
</html>
Let us make the StudentsBean.class available in CLASSPATH. Access the above JSP. the
following result will be displayed −
Student First Name: Zara
Student Age: 10
Imagine a travel agency application. It's important to remember the dates and
destination entered to book the flight so that the customer doesn't have to reenter
the information when it's time to make hotel and rental car reservations. This type
of information, available only to requests from the same user, can be shared
through the session scope.
Some information is needed by multiple pages independent of who the current user
is. JSP supports access to this type of shared information through the application
scope. Information saved in the application scope by one page can later be
accessed by another page, even if the two pages were requested by different users.
Examples of information typically shared through the application scope are
database connection pool objects, information about currently logged-in users, and
cache objects that avoid unnecessary database queries for data that is the same for
all users.
Figure 10-4 shows how the server provides access to the two scopes for different
clients.
Figure 10-4. Session and application scopes