821 likes | 1.1k Views
Servlets , JSP, MVC. Anti Orgla, Nortal AS. 18.03.2014. About me. Senior JAVA Developer @ Nortal AS 8 years of seeing this stuff Written : ~10 Servlets ~ Gazillion JSP files ~ Hypergazillion MVCs . anti.orgla@nortal.com. Agenda. JAVA EE + Web Containers Servlet API
E N D
Servlets, JSP, MVC Anti Orgla, Nortal AS 18.03.2014
About me • Senior JAVA Developer @ Nortal AS • 8 years of seeingthisstuff • Written: • ~10 Servlets • ~Gazillion JSP files • ~HypergazillionMVCs. • anti.orgla@nortal.com
Agenda • JAVA EE + WebContainers • Servlet API • JSP • MVC • Filters, Listeners • What’snext?
JAVA EE • Platform, thatprovidesAPIs fordeveloping and runningenterprisesoftware • WebApplications • WebServices • Messaging • Persistence • etc… • JEE 7 28.05.2013
API • Applicationprogramming interface • Specifies „groundrules“ • Specifieshowsoftwarecomponentsshouldinteractwitheach ohter
WebApplication • Applicationsoftware, thatrelies on webbrowsertorenderit • Buildingblocks in Java EE: • WebContainer • Servlet • JSP
WebContainer Manages component life cycles Accepts requests, sends responses Routes requests to applications http://tutorials.jenkov.com/java-servlets/overview.html
Web Containers • ApacheTomcat • JBoss • WebLogic • Jetty • Glassfish • Websphere • …
Web Containers Multiple applications inside one container http://tutorials.jenkov.com/java-servlets/overview.html
Application structure Java source files
Application structure Document root
Application structure Static content
Application structure Configuration, executable code
Application structure Deployment descriptor
Application structure Compiled classes
Application structure Dependencies (JAR-s)
Application structure Java Server Pages
Deployment descriptor (web.xml) Instructs the container how handlethis application <?xmlversion="1.0" encoding="UTF-8"?> <web-appxmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
web.xml In Servlet API version 3.0 most components of web.xml are replaced by annotations that go directly to Java source code. Conventionoverconfiguration! Examples later
Servlet • Java class, processesrequests (in) and returnsresponses (out) • Are managedby a webcontainer • javax.servlet.http.HttpServlet– abstractclass, describesdoXXXthat are usedfordifferenttype of HTTP method. • doGet(); • doPost();
Servlet example publicclassHelloServletextendsHttpServlet { @Override protectedvoiddoGet(HttpServletRequestreq, HttpServletResponse resp) throwsServletException, IOException { PrintWriterwriter = resp.getWriter(); writer.println("<html><head><title>Hello</title></head><body>"); writer.println("<p>HelloWorld!</p>"); writer.println("<p>Current time: " + new Date() + "</p>"); writer.println("</body></html>"); } }
Servlet mapping Before Servlet 3.0 web.xml (stoneagemode) <servlet> <servlet-name>hello</servlet-name> <servlet-class>example.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping>
Servlet Mapping In Servlet 3.0 via annotation @WebServlet("/hello") publicclassHelloServletextendsHttpServlet { ...
Servlet life cycle http://tutorials.jenkov.com/java-servlets/servlet-life-cycle.html
Sessions HTTP is a stateless protocol Howdoweremember a userbetweenrequests? Cookies URL rewriting
Java HttpSession • HttpSession is a common interface for accessing session context • Actualimplementationisprovidedby a WebContainer
Java HttpSession http://java.sun.com/developer/onlineTraining/JSPIntro/contents.html
HttpSession example HttpSessionsession = req.getSession(); intvisit; if (session.isNew()) { visit = 0; } else { visit = (Integer) session.getAttribute("visit"); } session.setAttribute("visit", ++visit);
HttpServletRequest Contains request information Parameters: String value = request.getParameter("name"); Attributes: request.setAttribute(“key", value); request.getAttribute(“key”);
HttpServletRequest Alsocontains META data… request.getMethod(); “GET”, “POST”, … request.getRemoteAddr(); Remote client’s IP request.getServletPath(); “/path/to/servlet” …
HttpServletRequest …and headers… request.getHeaderNames(); Enumeration<String> request.getHeader("User-Agent"); “Mozilla/5.0 (X11; Linux x86_64) …”
HttpServletRequest …and cookies Cookie[] cookies = request.getCookies(); cookie.getName(); cookie.getValue(); cookie.setValue(“new value”);
Cookie • Smallpiece of information (some ID, parameter, preferenceetc..) • Stored in browser • Usually sent by a server • Clientsendsonlyname-value pair • JSESSIONID = C687CC4E2B25B8A27DAB4A5F30980583 • language=en • Name • Value • Expirydate • Path • Domain • Secure (canbe sent oversshonly) • HttpOnly
HttpServletResponse Allows to set response information response.setHeader("Content-Type", "text/html"); response.addCookie(newCookie("name", "value"));
HttpServletResponse Addcontent response.getWriter().println("..."); Write text response.getOutputStream().write(...); Write binary
Servlets – should I writeone? Writing HTML in Java is hideous PrintWriterwriter = resp.getWriter(); writer.println("<html><head><title>Hello</title></head><body>"); writer.println("<p>HelloWorld!</p>"); writer.println("<p>Current time: " + new Date() + "</p>"); writer.println("</body></html>");
JSPtotherescue! • JSP (Java Server Pages) • Write HTML • +standard markuplanguage • Add dynamic scripting elements • Add Java code
JSP example war/WEB-INF/jsp/hello.jsp <%@pageimport="java.util.Date"%> <html> <head><title>Hello</title></head> <body> <p>HelloWorld!</p> <p>Current time: <%=new Date() %></p> </body> </html>
JSP mapping web.xml <servlet> <servlet-name>hello2</servlet-name> <jsp-file>/WEB-INF/jsp/hello.jsp</jsp-file> </servlet> <servlet-mapping> <servlet-name>hello2</servlet-name> <url-pattern>/hello2</url-pattern> </servlet-mapping>
JSP life-cycle http://www.jeggu.com/2010/10/jsp-life-cycle.html
Dynamic content Expression <p>Current time: <%=new Date()%></p> Scriptlet <p>Current time: <%out.println(new Date()); %></p>
Dynamic content Declaration <%! privateDatecurrentDate(){ returnnewDate(); } %> <p>Currenttime: <%=currentDate() %></p>
package org.apache.jsp.WEB_002dINF.jsp.document; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import java.util.Date; publicfinalclasstestdokument_jspextends org.apache.jasper.runtime.HttpJspBase implementsorg.apache.jasper.runtime.JspSourceDependent { privatestaticfinal javax.servlet.jsp.JspFactory _jspxFactory = javax.servlet.jsp.JspFactory.getDefaultFactory(); privatestaticjava.util.Map<java.lang.String,java.lang.Long> _jspx_dependants; private javax.el.ExpressionFactory _el_expressionfactory; private org.apache.tomcat.InstanceManager _jsp_instancemanager; publicjava.util.Map<java.lang.String,java.lang.Long> getDependants() { return_jspx_dependants; } publicvoid_jspInit() { _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig()); } publicvoid_jspDestroy() { } publicvoid_jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException { final javax.servlet.jsp.PageContext pageContext; javax.servlet.http.HttpSession session = null; final javax.servlet.ServletContext application; 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=UTF-8"); pageContext = _jspxFactory.getPageContext(this, request, response, null, true, 8192, true); _jspx_page_context = pageContext; application = pageContext.getServletContext(); config = pageContext.getServletConfig(); session = pageContext.getSession(); out = pageContext.getOut(); _jspx_out = out; out.write("\r\n"); out.write("\r\n"); out.write("<p>Currenttime: "); out.print(newDate() ); out.write("</p>"); } catch (java.lang.Throwable t) { if (!(t instanceofjavax.servlet.jsp.SkipPageException)){ out = _jspx_out; if (out != null &&out.getBufferSize() != 0) try { out.clearBuffer(); } catch (java.io.IOException e) {} if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); elsethrownewServletException(t); } } finally { _jspxFactory.releasePageContext(_jspx_page_context); } } }
Predefinedvariables • request – HttpServletRequest • response – HttpServletResponse • out– Writer • session– HttpSession • application– ServletContext • pageContext – PageContext
JSP actions • jsp:includeIncludes a file at the time the page is requested • jsp:forwardForwards the requester to a new page • jsp:getPropertyInserts the property of a JavaBean into the output jsp:setPropertySets the property of a JavaBean • jsp:useBeanFinds or instantiates a JavaBean
Expression Language (EL) Easy way to access JavaBeans in different scopes TotalSum: ${row.price *row.amount}
Basic OperatorsinEL http://www.tutorialspoint.com/jsp/jsp_expression_language.htm