QB Ans AWT
QB Ans AWT
QB Ans AWT
A JSP expression is used to insert values directly into the output. It has the following
syntax;
<%= Java Expression %>
The expression is evaluated, converted to a string, and inserted in the page. This
evaluation is
performed at runtime (when the page is requested) and thus has full access to
information about
the request.
Ex: To show the Date/Time that the page has requested
Current Time: <%= new java.util.Date() %>
JSP EXPRESSION
The code placed within JSP expression tag is written to the output stream of the
response. So
you need not write out.print() to write data. It is mainly used to print the values of
variable or
method.
Syntax of JSP expression tag
<%= statement %>
Example of JSP expression tag
In this example of jsp expression tag, we are simply displaying a welcome message.
<html>
<body>
<%= "welcome to jsp" %>
</body>
</html>
Note: Do not end your statement with semicolon in case of expression tag.
Example of JSP expression tag that prints current time
To display the current time, we have used the getTime() method of Calendar class.
The
getTime() is an instance method of Calendar class, so we have called it after getting
the instance
of Calendar class by the getInstance() method.
index.jsp
<html>
<body>
Current Time: <%= java.util.Calendar.getInstance().getTime() %>
</body>
</html>
Example of JSP expression tag that prints the user name
In this example, we are printing the username using the expression tag. The
index.html file gets
the username and sends the request to the welcome.jsp file, which displays the
username.
File: index.jsp
<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname"><br/>
<input type="submit" value="go">
</form>
</body>
</html>
File: welcome.jsp
<html>
<body>
<%= "Welcome "+request.getParameter("uname") %>
</body>
</html>
There are many JSP action tags or elements. Each JSP action tag is used to
perform some
specific tasks.
The action tags are used to control the flow between pages and to use Java Bean.
The Jsp action
tags are given below.
JSP Action Tags Description
jsp:forward forwards the request and response to another resource.
jsp:include includes another resource.
jsp:useBean creates or locates bean object.
jsp:setProperty sets the value of property in bean object.
jsp:getProperty prints the value of property of the bean.
jsp:plugin embeds another components such as applet.
jsp:param sets the parameter value. It is used in forward and include mostly.
jsp:fallback can be used to print the message if plugin is working.
It is used in jsp:plugin.
The jsp:useBean, jsp:setProperty and jsp:getProperty tags are used for bean
development. So we
will see these tags in bean developement.
jsp:forward action tag
The jsp:forward action tag is used to forward the request to another resource it may
be jsp, html
or another resource.
Syntax of jsp:forward action tag without parameter
<jsp:forward page="relativeURL | <%= expression %>" />
Syntax of jsp:forward action tag with parameter
<jsp:forward page="relativeURL | <%= expression %>">
<jsp:param name="parametername" value="parametervalue | <%=expression%>" />
</jsp:forward>
Example of jsp:forward action tag without parameter
In this example, we are simply forwarding the request to the printdate.jsp file.
index.jsp
<html>
<body>
<h2>this is index page</h2>
<jsp:forward page