90 likes | 186 Views
CSC 485 IST 350. JSP. What Is JSP. JSP stands for JavaServer Page Available since 1999 (JSP 1.0), and now in JSP 1.2 Part of the J2EE API: considered as Web Components, together with Java Servlet
E N D
CSC 485IST 350 JSP
What Is JSP • JSP stands for JavaServer Page • Available since 1999 (JSP 1.0), and now in JSP 1.2 • Part of the J2EE API: • considered as Web Components, together with Java Servlet JavaServer Pages (JSP) technology provides a simplified, fast way to create web pages that display dynamically-generated content.
A Simple JSP Page • A JSP page is like a regular HTML page with special tags <html><head><title>Current Time</title> </head><body>Current time is <%= new java.util.Date() %></body><html>
How Is a JSP Processed? Web Server Host Sample URL http://www.server.com:8080/servlet/JSPFile Host Machine File System /servlet/JSPFile.jsp Send a request URL Web Server Process servlet Generate response Get JSP file Request servlet HTML returned Servlet Engine JSP Translator Web Browser Get servlet
How Does JSP Work? (older view) • A client requests a JSP page from a web server • The web server must support JSP (such as the Tomcat server) • The JSP pages must have a .jsp extension • The web server • Translates the JSP into a Java servlet • Compiles the servlet, and • Executes it • The result of the execution, an HTML page, will be sent back to the browser
How Does JSP Work? (current view) • JSP pages use XML tags and scriptlets written in the Java programming language to encapsulate the logic that generates the content for the page. • It passes any formatting (HTML or XML) tags directly back to the response page. In this way, JSP pages separate the page logic from its design and display. • JSP technology is part of the Java technology family. JSP pages are compiled into servlets and may call JavaBeans components (beans) or Enterprise JavaBeans components (enterprise beans) to perform processing on the server.
JSP Constructs • Scripting constructs • Expressions: <%= java-expr %> • Scriptlets: <% java-statement(s); %> • Declarations: <% java method or field declaration %> • Comments: <%-- JSP comment --%> <!-- HTML comment --> • Directives: <%@ directive attr1=“value1”[attr2=“value2”] %> • Actions
Using JavaBeans in JSP • A JavaBeans component may be used in a JSP to provide information needed in generating dynamic contents • A class is a JavaBeans component if • The class is public • The class has an no-args public constructor • The class is serializable • The syntax is<jsp:useBean id=“objName” scope=“...”class=“ClassName” />
Getting and Setting Properties • The JavaBeans components normally have getters and setters • These methods can be used as follows • Passing information into the page<jsp:getProperty id=“beanId” property=“sample” /> • Retrieving information from the page<jsp:setProperty id=“beanId” property=“sample” value=“test1”/> • Typically, these constructs will be missed up with HTML tags