J S P - Java

  • JSP (Java Server Pages)
    • Boilerplate
          <html>
              <head>
                  <meta http-equiv="Content-Type" content="text/html;
                  charset=ISO-8859-1">
                  <title>Insert title here</title>
              </head>
              <body>
                  <h3>Welcome to JSP Programming</h3>
                  <%
                      java.util.Date date=new java.util.Date();
                      out.print(date);
                  %>
              </body>
          </html>
      
    • Life Cycle
      • Jasper converts requested JSP into Servelet class
      • If there no change in the JSP content then it will simple process the request by calling service method, else jsp will be converted into servelet again
      • Methods
        • jsplnit()
          • Invoked by the container to initialize the servlet instance, method is invoked only once
        • jspService()
          • It is invoked by the container each time when the request is received
        • jspDestroy()
          • Invoked by the container only once at the end of life cycle to do clean up
    • Scripting Element
      • Scriptlet tag => <% %>
      • Expression tag => <%= %>
        • Argument to out.print() method
      • Declaration tag => <%! %>
        • Used to declare methods other than service
        • Will be common to all requests
    • Implicit Objects
      • Created using translation phase, can use without declaring it
      • out => JSPWriter
      • request => HttpServeletRequest
      • reponse => HttpServeletResponse
        • response.sendRedirect(String)
        • response.setContentType(String)
        • response.sendError(int, String)
      • session => HttpSession
        • setAttribute(String, Object)
        • getAttribute(String)
        • getld()
        • Invalidate()
      • application => ServletContext
        • Used to read data from configuration file (web.xml)
      • config => ServletConfig
        • <%@ page errorPage="name.jsp" %>
        • <%@ page isErrorPage="true" %>
      • page => Object
        • Default Scope of a variable, available only within the page
      • exception => Throwable
      • pageContext => PageContext
    • Directive Elements
      • page Directive
        • Provides attributes that gets applied to entire JSP
        • <%@ page attributeName="value" %>
      • include Directive
        • To include other files
        • Only one servelet is created as content of other files gets pasted to main.jsp
      • taglib Directive
        • Used to define tag library
        • <%@ taglib uri="value" %>
    • Action Elements
      • Used to perform specific operations
      • Standard actions
        • <jsp:forward>
          • <jsp:forward page="name"></jsp:forward>
        • <jsp:include>
        • <jsp:param>
        • <jsp:useBean>
        • <jsp:setProperty>
        • <jsp:getProprerty>
  • JSTL (Java Standard Tag Library)
    • Collection of JSP tags, avoids using scriplet tag
    • Core Tags
      • Variable support, Flow control, URL management
      • Include => <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
      • <c:out value=""> </c:out> => Used to display on browser
      • <c:set>, <c:remove>, <c:catch>, <c:if>
      • <c:choose>, <c:when>, <c:otherwise>
      • <c:foeEach>
    • Formatting Tags
      • Format number, dates and messages
      • Include => <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
      • <fmt:formatNumber>, <fmt:formatDate>
    • Function Tags
      • String manipulation
    • XML Tags
    • SQL Tags
Share: