java server pages
play

Java Server Pages Jay Urbain, Ph.D. Credits: Official - PowerPoint PPT Presentation

Java Server Pages Jay Urbain, Ph.D. Credits: Official Tutorial: h?p://docs.oracle.com/javaee/6/tutorial/doc/bnafd.html Basham, Sierra, Bates, Bert. Head First Servlets


  1. Java ¡Server ¡Pages ¡ ¡ Jay ¡Urbain, ¡Ph.D. ¡ Credits: ¡ Official ¡Tutorial: ¡h?p://docs.oracle.com/javaee/6/tutorial/doc/bnafd.html ¡ Basham, ¡Sierra, ¡Bates, ¡Bert. ¡Head ¡First ¡Servlets ¡and ¡JSP. ¡O'Reilly. ¡ 1

  2. HTML/JSPs ¡are ¡intended ¡to ¡be ¡used ¡for ¡ the ¡View ¡in ¡an ¡MVC-­‑based ¡web ¡ applicaPon ¡ Model ¡– ¡represents ¡an ¡applicaPon’s ¡state, ¡business ¡ logic, ¡or ¡data ¡ Model (Command) POJO (java classes) ¡ Input from views View ¡– ¡responsible ¡for ¡ ¡ Changes to model Controller – displaying ¡output ¡to ¡the ¡user ¡ (Java servlet) displays ¡ Controller ¡– ¡ ¡ User input – accepts ¡input ¡from ¡user ¡ ¡ View (HTML/JSP) – instructs ¡the ¡model ¡to ¡perform ¡acPons ¡on ¡the ¡input ¡ – decides ¡what ¡view ¡to ¡display ¡for ¡output ¡ – does ¡not ¡ ¡generate ¡HTML ¡ 2

  3. JSP ¡code ¡looks ¡like ¡HTML, ¡with ¡the ¡addiPon ¡of ¡ scrip2ng ¡elements ¡ <%@ ¡ page ¡ contentType="text/html;charset=UTF-­‑8" ¡language="java" ¡%> ¡ <%@ ¡ page ¡ import="java.uPl.Date, ¡java.text.*" ¡%> ¡ <html> ¡ ¡ ¡<head> ¡ ¡ ¡ ¡ ¡<Ptle>$Title$</Ptle> ¡ ¡ ¡</head> ¡ JSP’s may look like HTML to non-Java webpage authors ¡ ¡<body> ¡ ¡ ¡ <% ¡ ¡ ¡ ¡ ¡ Date ¡yourDate= ¡new ¡Date(); ¡ ¡ ¡ ¡ ¡out.print(yourDate.toString()); ¡ ¡ ¡ %> ¡ ¡ ¡ </body> ¡ </html> ¡ 3

  4. JSP’s ¡are ¡automaPcally ¡translated ¡into ¡Servlets ¡ 4

  5. Navigate to your Tomcat installation work directory Example: /Applications/apache-tomcat-7.0.40/work/ Catalina/localhost/Weather_vollerj/org/apache/ jsp/weather_jsp.java 5

  6. … ¡ public ¡void ¡_jspService(final ¡javax.servlet.h?p.H?pServletRequest ¡request, ¡final ¡javax.servlet.h?p.H?pServletResponse ¡response) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡throws ¡java.io.IOExcepPon, ¡javax.servlet.ServletExcepPon ¡{ ¡ ¡ ¡ ¡ ¡ ¡final ¡javax.servlet.jsp.PageContext ¡pageContext; ¡ ¡ ¡ ¡ ¡javax.servlet.h?p.H?pSession ¡session ¡= ¡null; ¡ ¡ ¡ ¡ ¡final ¡javax.servlet.ServletContext ¡applicaPon; ¡ ¡ ¡ ¡ ¡final ¡javax.servlet.ServletConfig ¡config; ¡ ¡ ¡ ¡ ¡javax.servlet.jsp.JspWriter ¡out ¡= ¡null; ¡ ¡ ¡ ¡ ¡final ¡java.lang.Object ¡page ¡= ¡this; ¡ ¡ ¡ ¡ ¡javax.servlet.jsp.JspWriter ¡_jspx_out ¡= ¡null; ¡ ¡ ¡ ¡ ¡javax.servlet.jsp.PageContext ¡_jspx_page_context ¡= ¡null; ¡ ¡ ¡ ¡ ¡ ¡try ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡response.setContentType("text/html; ¡charset=ISO-­‑8859-­‑1"); ¡ ¡ ¡ ¡ ¡ ¡ ¡pageContext ¡= ¡_jspxFactory.getPageContext(this, ¡request, ¡response, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡null, ¡true, ¡8192, ¡true); ¡ ¡ ¡ ¡ ¡ ¡ ¡_jspx_page_context ¡= ¡pageContext; ¡ ¡ ¡ ¡ ¡ ¡ ¡applicaPon ¡= ¡pageContext.getServletContext(); ¡ ¡ ¡ ¡ ¡ ¡ ¡config ¡= ¡pageContext.getServletConfig(); ¡ ¡ ¡ ¡ ¡ ¡ ¡session ¡= ¡pageContext.getSession(); ¡ ¡ ¡ ¡ ¡ ¡ ¡out ¡= ¡pageContext.getOut(); ¡ ¡ ¡ ¡ ¡ ¡ ¡_jspx_out ¡= ¡out; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡out.write("\n"); ¡ ¡ ¡ ¡ ¡ ¡ ¡out.write(" ¡ ¡ ¡ ¡ ¡\n"); ¡ ¡ ¡ ¡ ¡ ¡ ¡out.write("<!DOCTYPE ¡html>\n"); ¡ ¡ ¡ ¡ ¡ ¡ ¡out.write("<html>\n"); ¡ ¡ ¡ ¡ ¡ ¡ ¡out.write("<head>\n"); ¡ ¡ ¡ ¡ ¡ ¡ ¡out.write("<meta ¡h?p-­‑equiv=\"Content-­‑Type\" ¡content=\"text/html; ¡charset=ISO-­‑8859-­‑1\">\n"); ¡ ¡ ¡ ¡ ¡ ¡ ¡out.write("<Ptle>Stock ¡Quoter</Ptle>\n"); ¡ ¡ ¡ ¡ ¡ ¡ ¡out.write("</head>\n"); ¡ ¡ ¡ ¡ ¡ ¡ ¡out.write("<body>\n"); ¡ ¡ ¡ ¡ ¡ ¡ ¡out.write("\n"); ¡ 6 ¡ ¡ ¡ ¡ ¡ ¡out.write("Date: ¡\n"); ¡ … ¡

  7. There ¡are ¡6 ¡different ¡kinds ¡of ¡JSP ¡ elements ¡ 1. <% ¡out.println(“hello”); ¡ %> ¡ ¡Scriptlets ¡ 2. <%@ ¡page ¡import=“java.uPl.*” ¡ %> ¡DirecPves ¡ 3. <%= ¡“hello” ¡ %> ¡ ¡ ¡ ¡String ¡expressions ¡ 4. <%! ¡int ¡count ¡= ¡0; ¡ %> ¡ ¡ ¡DeclaraPons ¡ 5. <%-­‑-­‑ ¡A ¡JSP ¡comment ¡ -­‑-­‑%> ¡ ¡Comments ¡ 6. ${ ¡…} ¡Expression ¡Language ¡code ¡ 7. <jsp:… ¡/> ¡ jsp ¡tags ¡ 7

  8. Scriplets ¡contain ¡plain ¡old ¡java ¡code ¡ <% // The code within this tag is a java scriptlet - plain old java. java.util.Date d = new java.util.Date(); java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); // out is the PrintWriter output stream out.println("Today is " + df.format(d)); %> Every JSP Servlet has a _jspService() method that is generated by the JASPER translator. _jspService() is the method called by Tomcat as a substitute for service(), the method that calls doGet() and doPost(). Scriptlet code is inserted into the body of the _jspService() method 8

  9. DirecPves ¡tell ¡the ¡translator ¡what ¡to ¡do ¡ <%@ ¡ page ¡ contentType="text/html;charset=UTF-­‑8" ¡language="java" ¡%> ¡ <%@ ¡ page ¡ import="java.uPl.Date, ¡java.text.*" ¡%> Three ¡types ¡of ¡direcPves: ¡page, ¡include, ¡taglib ¡ The ¡page ¡direcPve ¡can ¡have ¡10 ¡addiPonal ¡a?ributes: ¡ • ¡isThreadSafe[true], ¡ ¡contentType[text/html], ¡errorPage, ¡ isErrorPage[true ¡if ¡referred], ¡ ¡session[true], ¡ ¡autoFlush[true], ¡ ¡ buffer, ¡info, ¡ ¡IsELIgnored, ¡ ¡extends, ¡isScripPngEnabled ¡ 9

  10. Expressions ¡become ¡the ¡argument ¡to ¡ out.print() ¡ <%= “hello” %> Is ¡the ¡same ¡as ¡ <% out.print(“hello”); %> ¡ ¡ Note: the absence of the semicolon in the expression. The expression can evaluate to any type of object, which is then rendered to a String using class toString() method. 10

  11. DeclaraPons ¡ <%! private int count = 0; %> <%! // a helper method called from scriplet code private int doubleCount() { count = count*2; return count; } %> Normally, scriptlet code (i.e. <% … %>) gets put into the body ¡ of the _jspService() method. Declarations are inserted before the body of the _jspService() method. Thus, Declarations can be used to declare Servlet class attributes and methods. 11

  12. JSP’s ¡are ¡translated ¡into ¡Servlets ¡ having ¡the ¡methods: ¡ _ ¡jspService() ¡ – This ¡is ¡where ¡the ¡translated ¡HTML ¡and ¡your ¡<%...%> ¡ scriptlet ¡code ¡gets ¡put ¡ jspDestroy() ¡ – You ¡can ¡override ¡this ¡in ¡a ¡<%! ¡… ¡%> ¡declaraPon ¡ jspInit() ¡ – Similar ¡in ¡funcPon ¡to ¡the ¡regular ¡init() ¡method ¡ – You ¡can ¡override ¡this ¡too ¡in ¡a ¡<%! ¡… ¡%> ¡declaraPon ¡ 12

  13. Comments ¡ <%-- A JSP comment %> <!-- An HTML comment --> ¡ HTML ¡comments ¡are ¡passed ¡into ¡the ¡generated ¡HTML ¡ ¡ JSP ¡comments ¡are ¡removed. ¡ 13

  14. JSP ¡Servlets ¡can ¡have ¡init ¡parameters ¡like ¡regular ¡ servlets ¡ <?xml version= "1.0" encoding="UTF-8"?> ... Note: the tag <jsp-file> <servlet> replaces the <servlet- <servlet-name>HelloWorld</servlet-name> class> tag <jsp-file> /hello.jsp </jsp-file> <init-param> <param-name>max_value</param-name> <param-value>10</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> ... </web-app> 14

  15. A ¡JSP ¡has ¡access ¡to ¡various ¡implicit ¡ objects ¡ 1. request ¡– ¡an ¡H?pServletRequest ¡ 2. response ¡– ¡an ¡H?pServletResponse ¡ 3. out ¡– ¡a ¡JspWriter ¡(same ¡api ¡as ¡PrintWriter) ¡ 4. session ¡– ¡an ¡H?pSession ¡ 5. config ¡– ¡a ¡ServletConfig ¡(for ¡this ¡JSP) ¡ 6. applicaPon ¡– ¡a ¡ServletContext ¡ 7. page ¡– ¡an ¡Object ¡used ¡for ¡custom ¡tags ¡ 8. pageContext ¡– ¡a ¡PageContext ¡(similar ¡to ¡config) ¡ 9. excepPon ¡– ¡a ¡Throwable ¡(available ¡to ¡error ¡pages ¡only) ¡ 15

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend