top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is Scriptlet, Expression and Declaration in JSP?

+1 vote
465 views
What is Scriptlet, Expression and Declaration in JSP?
posted Oct 3, 2017 by Supriya Jain

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

There are three Scripting Elements

  1. Expressions of the form <%= expression %> that are evaluated and inserted into the output,
    A JSP expression is used to insert Java values directly into the output. It has the following form:

    <%= Java expression %>

The Java expression is evaluated, converted to a string, and inserted in the page

Current time: <%= new java.util.Date() %>

predefined variables that you can use request, the HttpServletRequest; response, the HttpServletResponse; session, the HttpSession associated with the request (if any); and out, the PrintWriter (a buffered version of type JspWriter) used to send output to the client.

Example :Your hostname: <%= request.getRemoteHost() %> 

2 Scriptlets of the form <% code %> that are inserted into the servlet's service method, and
If you want to do something more complex than insert a simple expression, JSP scriptlets insert code into the service() method of the generated servlet.

Scriptlets have the following form:

<% Java Code %> Scriptlets have access to the same automatically defined variables as expressions 
<% 
String name = request.getParameter("name"); 
out.println("name: " + name); 
%> 

3 Declarations of the form <%! code %> that are inserted into the body of the servlet class, outside of any existing methods.
A JSP declaration define methods or fields that get inserted into the main body of the servlet class (outside of the service method processing the request). It has the following form:

<%! Java Code %> 
or
<%!
public int getValue(){
    return 78;
    }
%>
The method we can use in Expression also.
<%=getValue()%>
answer Oct 3, 2017 by Manikandan J
...