Java Server Pages or JSP

Hello Friends!

Here is a short introduction to Java Server Pages or JSP. I have used simple points to describe the basics of JSP and its uses.
  1. JSP is a web component.
  2. JSP pages are compiled to Servlet automatically whereas in Servlets we need to compile to obtain the .class or  java bytecodes.
  3. JSP Engine is a piece of Software that compiles the JSP programs.
  4. For JSP pages there is no need to make entry in web.xml but if required we can do so.
  5. Parallel development is possible in JSP.
  6. No need to start/stop server in case of changes made on JSP pages.
  7. Development time is reduced as compared to Servlets.
  8. Debugging is difficult in JSP.
  9. JSP is basically used to achieve the view part MVC (which is a design pattern).
  10. JSP pages are kept outside the WEB-INF folder.


JSP Elements:
  1. scriptlet
  2. expression
  3. declaration
  4. directive
  5. action
  6. el
Implicit Objects in JSP:
1. out                                        JspWriter
2. request                                  HttpServletRequest
3. response                                HttpServletResponse
4. application                            ServletContext
5. page                                      Object
6. pageContext                         PageContext
7. exception                              JspException
8. config                                   ServletConfig
9. session                                  HttpSession
SCRIPTLET:
out.println(“I am scriptlet”);
%> 
-We can use more then one scriptlets to write java code.

DECLARATION:

int n=10;
%>

– It does not execute everytime.
– We cannot use out in declaration as out is an implicit object.
e.g:
int n=1;
%>
out.println(n++);
%>
– The above combination of declaration and scriptlet will produce the output as:
1 (for the first time)
2 (on refreshing the page)
3 (for the third refresh) and so on….
– This means declaration will be executed only for the first time and scriptlet is executed everytime on page refresh.
EXPRESSION:
 

– It goes automatically to out.println() and it will display Blue Red Green
– Here we have not put ; as we do in out.println(“Blue Red Green”);

So in JSP we have basically scriptlet, declaration and expression.

– Comments in JSP can be given as:


//comments

–%>

DIRECTIVE:
1. Page Directive
2. Include Directive
3. Taglib Directive

e.g. of Page Directive:

– In case we want to import more files comma can be used to separate the two imports.

e.g. of Inlcude Directive:


“Wait for updates”