06-10-2016, 09:43 AM
1457961234-jsp.ppt2.pptx (Size: 191.58 KB / Downloads: 4)
The JSP Framework
Idea:
Use regular HTML for most of page
Mark servlet code with special tags
Entire JSP page gets translated into a servlet (once), and servlet is what actually gets invoked (for each request)
Example:
JSP
Thanks for ordering <I><%= request.getParameter("title") %></I>
URL
http://host/OrderConfirmation.jsp?title=...rogramming
Result
Thanks for ordering Core Web Programming
Setting Up Your Environment
Set your CLASSPATH.
Compile your code.
Use packages to avoid name conflicts.
Put JSP page in special directory.
tomcat_install_dir/webapps/ROOT
Use special URL to invoke JSP page.
Caveats
Previous rules about CLASSPATH, inst
Most Common Misunderstanding:Forgetting JSP is Server-Side Technology
Very common question
I can’t do such and such with HTML. Will JSP let me do it?
Similar questions
How do I put an applet in a JSP page?Answer: send an <APPLET…> tag to the client
How do I put an image in a JSP page?Answer: send an <IMG …> tag to the client
How do I use JavaScript/Acrobat/Shockwave/Etc?Answer: send the appropriate HTML tags
JSP Components
There are three main types of JSP constructs that you embed in a page.
Scripting elements
You can specify Java code
Expressions, Scriptlets, Declarations
Directives
Let you control the overall structure of the servlet
Page, include, Tag library
Actions
Enable the use of server side Javabeans
Transfer control between pages
Types of Scripting Elements
You can insert code into the servlet that will be generated from the JSP page.
Expressions: <%= expression %>
Evaluated and inserted into the servlet’s output. i.e., results in something like out.println(expression)
Scriptlets: <% code %>
Inserted verbatim into the servlet’s _jspService method (called by service)
Declarations: <%! code %>
Inserted verbatim into the body of the servlet class, outside of any existing methods
Predefined Variables(Implicit Objects)
They are created automatically when a web server processes a JSP page.
request: The HttpServletRequest (1st arg to doGet)
response: The HttpServletResponse (2nd arg to doGet)
session
The HttpSession associated with the request (unless disabled with the session attribute of the page directive)
out
The stream (of type JspWriter) used to send output to the client
application
The ServletContext (for sharing data) as obtained via getServletConfig().getContext().
page, pageContext, config, exception
JSP Directives
Affect the overall structure of the servlet
Two possible forms for directives
<%@ directive attribute=“value” %>
<%@ directive attribute1=“value1”
attribute2=“value2”
….
attributeN=“valueN” %>
There are three types of directives
Page, include, and taglib