We have discussed earlier that a JSP file is converted to Servlet. Here with the following example, we will try to understand the conversion technique. During the conversion, JSP expressions are sent to the PrintWriter, so that the value of the expression is printed on the Web page. The lines of code written in JSP Scriptlet are added to the service() method. The variable declarations, which are written in JSP Declarations, are added to the generated Servlet as instance variables.
Hence, the variables, which are declared in JSP Scriptlet, are local variables to the service method. But the variables, declared in JSP Declarations, are added to the Servlet as instance variables.
We can't write any logical expressions in JSP Declarations as logical expressions can not be written in a Class (logical expressions can be written in methods only).
Similarly, if you want to define a method, it should be written in JSP Declarations because, during interpretation, the code block will be added in Servlet as a method. We can't define any method within JSP Scriptlet because that code will be added in service() method but a method does not contain any other method definition.
The following code is self-explanatory, so read the comments carefully.
Source Code of index.jsp
Let's compare JSP and Servlet side by side.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.