Interview Question in JSP


 

Interview Question :: What is a Scriptlet?

 What is a Scriptlet?

by ksk
VoteNowAnswers to "What is a Scriptlet?"

 A scriptlet can contain any number of language statements, variable or method declarations, or expressions that are valid in the page scripting language.Within scriptlet tags, you can

1.Declare variables or methods to use later in the file (see also Declaration).
 
2.Write expressions valid in the page scripting language (see also Expression). 
 
3.Use any of the JSP implicit objects or any object declared with a <jsp:useBean> tag. 
You must write plain text, HTML-encoded text, or other JSP tags outside the scriptlet.
 
Scriptlets are executed at request time, when the JSP engine processes the client request. If the scriptlet produces output, the output is stored in the out object, from which you can display it.
by ksk

Scriptles:

Syntax of JSP Scriptles are:

  <%
  //java codes
   %>

JSP Scriptlets begins with <% and ends %> .We can embed any amount of java code in the JSP Scriptlets. JSP Engine places these code in the _jspService() method. Variables available to the JSP Scriptlets are:

  • request:
    request represents the clients request and is a subclass of HttpServletRequest. Use this variable to retrieve the data submitted along the request.
    Example:
      <%
      //java codes
       String userName=null;
       userName=request.getParameter("userName");
       %>
  • response:
    response is subclass of HttpServletResponse.
     
  • session:
    session represents the HTTP session object associated with the request.
     
  • out:
    out is an object of output stream and is used to send any output to the client.

Other variable available to the scriptlets are pageContext, application,config and exception

by ksk