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

java server pages
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

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. ¡

slide-2
SLIDE 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 ¡ ¡ View ¡– ¡responsible ¡for ¡ ¡

– displaying ¡output ¡to ¡the ¡user ¡ ¡

Controller ¡– ¡ ¡

– 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

slide-3
SLIDE 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> ¡ ¡ ¡<body> ¡ ¡ ¡<% ¡ ¡ ¡ ¡ ¡Date ¡yourDate= ¡new ¡Date(); ¡ ¡ ¡ ¡ ¡out.print(yourDate.toString()); ¡ ¡ ¡%> ¡ ¡ ¡</body> ¡ </html> ¡

3

JSP’s may look like HTML to non-Java webpage authors

slide-4
SLIDE 4

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

4

slide-5
SLIDE 5

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

slide-6
SLIDE 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"); ¡ ¡ ¡ ¡ ¡ ¡ ¡out.write("Date: ¡\n"); ¡ … ¡ 6

slide-7
SLIDE 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

slide-8
SLIDE 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

  • ut.println("Today is " + df.format(d));

%>

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

slide-9
SLIDE 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

slide-10
SLIDE 10

Expressions ¡become ¡the ¡argument ¡to ¡

  • ut.print() ¡

<%= “hello” %> Is ¡the ¡same ¡as ¡ <% out.print(“hello”); %>

¡ ¡

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.

slide-11
SLIDE 11

DeclaraPons ¡

<%! 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

  • f 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.

slide-12
SLIDE 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

slide-13
SLIDE 13

Comments ¡

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

13

slide-14
SLIDE 14

JSP ¡Servlets ¡can ¡have ¡init ¡parameters ¡like ¡regular ¡ servlets ¡

<?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

slide-15
SLIDE 15

A ¡JSP ¡has ¡access ¡to ¡various ¡implicit ¡

  • bjects ¡
  • 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

slide-16
SLIDE 16

Be ¡careful ¡of ¡how ¡JSP’s ¡handle ¡ sessions ¡

¡The ¡_jspService() ¡method ¡will ¡automaPcally ¡call ¡getSession() ¡and ¡

return ¡a ¡HRpSession ¡to ¡the ¡implicit ¡session ¡reference ¡ – A ¡new ¡session ¡may ¡be ¡created ¡if ¡one ¡doesn’t ¡yet ¡exist ¡– ¡is ¡that ¡ always ¡what ¡you ¡want? ¡Usually. ¡ To ¡suppress ¡this, ¡use: ¡ <%@ page session="false" %> Or, ¡use ¡a ¡Session ¡Listener ¡to ¡create ¡the ¡session ¡data. ¡

¡

16

slide-17
SLIDE 17

Dispatching ¡a ¡Request ¡from ¡a ¡Servlet ¡ to ¡a ¡JSP ¡

To ¡forward ¡a ¡Servlet’s ¡context ¡to ¡another ¡Servlet ¡or ¡JSP ¡page, ¡ ues ¡RequestDispatcher ¡in ¡your ¡ ¡Servlet ¡doGet(), ¡doPost(), ¡or ¡ service() ¡methods, ¡or ¡your ¡JSP ¡page ¡scriplet ¡(_jspService()) ¡ ¡

RequestDispatcher view = request.getRequestDispatcher(“view.jsp"); view.forward( request, response );

¡

17

slide-18
SLIDE 18

You ¡can ¡add ¡a?ributes ¡to ¡an ¡H?pServletRequest, ¡ effecPvely ¡enabling ¡you ¡to ¡“pass” ¡arguments ¡to ¡the ¡JSP ¡

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

slide-19
SLIDE 19

Remember: ¡You ¡can’t ¡forward ¡a ¡ Request ¡if ¡you’ve ¡already ¡commi?ed ¡a ¡ Response ¡

If ¡your ¡Servlet ¡has ¡already ¡generated ¡output, ¡you’ll ¡get ¡an ¡ IllegalStateExcepPon: ¡ ¡ PrintWriter pw = response.getWriter(); pw.println(“<html”>); … RequestDispatcher view = request.getRequestDispatcher("view.jsp"); view.forward( request, response );

19

slide-20
SLIDE 20
  • Create ¡a ¡dynamic ¡web ¡project ¡or ¡use ¡your ¡current ¡Servlet ¡lab ¡
  • Create ¡an ¡index.jsp ¡file ¡in ¡the ¡root ¡of ¡your ¡WebContent ¡directory. ¡Use ¡your ¡IDE. ¡ ¡

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 ¡

  • ut.println(df.format(d)); ¡

%> ¡ </body> ¡ </html> ¡

  • Test ¡in ¡IntelliJ ¡
  • Deploy ¡your ¡Web ¡app ¡and ¡test. ¡
  • Locate ¡your ¡generated ¡index_jsp.java ¡file ¡under ¡your ¡Tomcat ¡installaPon’s ¡work ¡
  • directory. ¡See ¡earlier ¡slide. ¡

¡

20

slide-21
SLIDE 21

¡

  • Deploy ¡your ¡Web ¡app ¡and ¡test. ¡
  • Locate ¡your ¡generated ¡index_jsp.java ¡file ¡under ¡your ¡Tomcat ¡installaPon’s ¡

work ¡directory. ¡See ¡earlier ¡slide. ¡

21