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. ¡
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
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. ¡
– displaying ¡output ¡to ¡the ¡user ¡ ¡
– accepts ¡input ¡from ¡user ¡ ¡ – instructs ¡the ¡model ¡to ¡perform ¡acPons ¡on ¡the ¡input ¡ – decides ¡what ¡view ¡to ¡display ¡for ¡output ¡ – does ¡not ¡ ¡generate ¡HTML ¡
2
Model (Command) POJO (java classes) View (HTML/JSP) Controller (Java servlet)
Changes to model displays Input from views User input
<%@ ¡page ¡contentType="text/html;charset=UTF-‑8" ¡language="java" ¡%> ¡ <%@ ¡page ¡import="java.uPl.Date, ¡java.text.*" ¡%> ¡ <html> ¡ ¡ ¡<head> ¡ ¡ ¡ ¡ ¡<Ptle>$Title$</Ptle> ¡ ¡ ¡</head> ¡ ¡ ¡<body> ¡ ¡ ¡<% ¡ ¡ ¡ ¡ ¡Date ¡yourDate= ¡new ¡Date(); ¡ ¡ ¡ ¡ ¡out.print(yourDate.toString()); ¡ ¡ ¡%> ¡ ¡ ¡</body> ¡ </html> ¡
3
JSP’s may look like HTML to non-Java webpage authors
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
… ¡
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"); ¡ ¡ ¡ ¡ ¡ ¡ ¡out.write("Date: ¡\n"); ¡ … ¡ 6
7
<% // 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
%>
8
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
<%@ ¡page ¡contentType="text/html;charset=UTF-‑8" ¡language="java" ¡%> ¡ <%@ ¡page ¡import="java.uPl.Date, ¡java.text.*" ¡%>
¡isThreadSafe[true], ¡ ¡contentType[text/html], ¡errorPage, ¡ isErrorPage[true ¡if ¡referred], ¡ ¡session[true], ¡ ¡autoFlush[true], ¡ ¡ buffer, ¡info, ¡ ¡IsELIgnored, ¡ ¡extends, ¡isScripPngEnabled ¡
9
10
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.
<%! private int count = 0; %> <%! // a helper method called from scriplet code private int doubleCount() { count = count*2; return count; } %>
11
Normally, scriptlet code (i.e. <% … %>) gets put into the body
Declarations are inserted before the body of the _jspService() method. Thus, Declarations can be used to declare Servlet class attributes and methods.
12
13
<?xml version="1.0" encoding="UTF-8"?> ... <servlet> <servlet-name>HelloWorld</servlet-name> <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
Note: the tag <jsp-file> replaces the <servlet- class> tag
15
16
RequestDispatcher view = request.getRequestDispatcher(“view.jsp"); view.forward( request, response );
17
RequestDispatcher view = request.getRequestDispatcher(“view.jsp"); // add an attribute to be attached to the request request.setAttribute(“foo”, “bar”); // Now, view.jsp will be able to get “bar” from the “foo” attribute attached to the request view.forward( request, response );
18
Show example
19
Add ¡the ¡following ¡code: ¡
<%@ ¡page ¡contentType="text/html;charset=UTF-‑8" ¡language="java" ¡%> ¡ ¡<%@ ¡page ¡import="java.u2l.*, ¡java.text.*" ¡%> ¡ ¡ <!DOCTYPE ¡html> ¡ <html> ¡ <head> ¡ <meta ¡h?p-‑equiv="Content-‑Type" ¡content="text/html; ¡charset=ISO-‑8859-‑1"> ¡ <Ptle>Stock ¡Quoter</Ptle> ¡ </head> ¡ <body> ¡ Date: ¡ ¡ <% ¡ Date ¡d ¡= ¡new ¡Date(); ¡ DateFormat ¡df ¡= ¡new ¡SimpleDateFormat("yyyy/MM/dd ¡HH:mm:ss"); ¡ // ¡out ¡is ¡a ¡PrinterWriter ¡output ¡stream ¡
%> ¡ </body> ¡ </html> ¡
¡
20
¡
work ¡directory. ¡See ¡earlier ¡slide. ¡
21