Submitted by: Submitted by 11123
Views: 151
Words: 845
Pages: 4
Category: Other Topics
Date Submitted: 08/25/2013 04:18 PM
Java Server Pages (JSP)
What is JSP
JSP simply puts Java inside HTML pages. You can take any existing HTML page and change its extension to ".jsp" instead of ".html". Scripting elements are used to provide dynamic pages
JSP and Servlets
Each JSP page is turned into a Java servlet, compiled and loaded. This compilation happens on the first request. After the first request, the file doesn't take long to load anymore. Every time you change the JSP file, it will be re-compiled again.
Java Server Page translator Java servlet source code compiler Java Servlet class file
Generated servlets
You can examine the source code produced by the JSP translation process. There is a directory called generated in Sun Java J2EE Application Server where you can find the source code. Note that the _jspService method corresponds to the servlet service method (which is called by doGet or doPost)
JSP elements (overview)
Directives of the form Scripting elements
Expressions of the form Scriptlets of the form Declarations of the form JSP Comments
Standard actions
Example: ...
Implicit variables like request, response, out
JSP Directives
They have the form
page include taglib
Specify page properties Include a file at translation time Specify custom tags
JSP Directive Examples
Import java packages
Multiple import statements
Including file at translation time
For include the path is relative to the JSP file
JSP Scripting Elements: Expressions
For an expression scripting element like , expr is evaluated and the result is converted to a string and placed into the JSP's servlet output stream. In a Java servlet this would be equivalent to
PrintWriter out = response.getWriter(); ... out.print(expr);
JSP Expression Examples
Displaying request parameters (request is an implicit object available in a JSP)
Your name is and your age is
Doing calculations
The value of pi is and the square root of two is and today's date...