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 ...view middle of the document...
JSP Scripting Elements: Scriptlet
For a scriplet the Java statements are placed in the translated servlet's _jspService method body (which is called by either doGet or doPost)
public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException {... statements ... }
JSP Scriplet Examples
Check a request parameter
Please supply a name Hello
In this example, there are 3 scriptlets elements and one expression element
JSP Scripting Elements: Declaration
For a declaration the Java statements are placed in the class outside the _jspService method. Declarations can be Java instance variable declarations or Java methods
// declarations would go here public void _jspService(...) { ... }
JSP Declaration examples
Declaring instance variables
... The count is .
Declaring methods
Including Files
Including files at translation time (when JSP is translated to a servlet)
Including files at request time
A simple JSP
JSP Test JSP Test Time:
The expression scripting element is equivalent to the scriptlet
The Implicit out Object
In a scriptlet you can use the out object to write to the output stream. Example:
The Implicit request Object
In a scriptlet you can use the request object to access GET/POST parameters. Example:
... ...
Example: HTML Form Submission Using GET
JSP Processing ... JSP Processing form with GET First name: Last name:
Processing Submitted HTML Forms
ProcessForm.jsp:
JSP Form Results JSP Form Results Hello
Temperature Conversion Example
Fahrenheit ... Conversion Fahrenheit to Celsius Conversion Fahrenheit temperature:
Temperature Conversion Example (Contd.)
F is C Another conversion
Java Beans
Special classes that encapsulate some...