servlets and (jsp)jdurrett.ba.ttu.edu/4383/notes/servlets_and_java_server_pages.pdf · servlets vs....
TRANSCRIPT
![Page 1: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/1.jpg)
T A K E I T T O T H E N T H
Servletsand
Java Server Pages (JSP)
![Page 2: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/2.jpg)
Andrew [email protected]
www.sun.com/developers/evangcentral
Senior Software EngineerSun Microsystems
T A K E I T T O T H E N T H
![Page 3: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/3.jpg)
Agenda What are Servlets? What are JSPs? Design issues Resources and Summary
![Page 4: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/4.jpg)
What are Servlets?
T A K E I T T O T H E N T H
![Page 5: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/5.jpg)
Server side Java™ program That respond to web server requests
Used to generate dynamic HTML
What Are Servlets?Servlets are Java’s answer to CGI/Perl...
![Page 6: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/6.jpg)
Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,
Initialized once and persists in memory for multiple requests, cached
Multi-threaded
CGIBased
Webserver
CGIBased
Webserver
Request CGI1 Child for CGI1CGI
BasedWebserver
Servlet Based Webserver
JVM
Request CGI1 Child for CGI1
Request Servlet1
CGIBased
Webserver
Servlet Based Webserver
JVMServlet1
Request CGI1 Child for CGI1
Request CGI2
Request Servlet1
CGIBased
WebserverChild for CGI2
Servlet Based Webserver
JVMServlet1
Request CGI1 Child for CGI1
Request CGI2
Request Servlet1
Request Servlet2
CGIBased
WebserverChild for CGI2
Servlet Based Webserver
JVMServlet1
Servlet2
Request CGI1 Child for CGI1
Request CGI2
Request CGI1
Request Servlet1
Request Servlet2
CGIBased
WebserverChild for CGI2
Child for CGI1
Servlet Based Webserver
JVMServlet1
Servlet2
Request CGI1 Child for CGI1
Request CGI2
Request CGI1
Request Servlet1
Request Servlet2
Request Servlet1
CGIBased
WebserverChild for CGI2
Child for CGI1
Servlet Based Webserver
JVMServlet1
Servlet2
Request CGI1 Child for CGI1
![Page 7: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/7.jpg)
Why Servlets?•Superior alternative to CGI:
– Multi-threaded, Cached, better security
•Easy to program, Simple APIs for input arguments, cookies
•Written in Java
– Can take advantage of JDBC, EJB, JMS, JavaMail, JavaIDL, RMI, APIs...
![Page 8: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/8.jpg)
Servlet and JSP™ Run in Web Container
JND
I
J2SE
JMS
RMI/I
IOP
JDBC
Database
AppClient
App Client Container
HTTP/HTTPS
J2SE
RMI
J2SE
JND
I
JMS
RMI/I
IOP
JDBC
JTA JavaMail
JAF JND
I
JMS
RMI/I
IOP
JDBC
JTA JavaMail
JAF
HTTP/HTTPS
Applet Container
Applet JSP Servlet EJB
Web Container EJB Container
RMI
J2SE
![Page 9: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/9.jpg)
Servlet APIs
GenericServlet
Servlet interface
HttpServlet
HelloWorldServlet
extends
implements
extends
Your Servlet
javax.servletpackage
![Page 10: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/10.jpg)
Servlet Example
service()
Request
Response
Server
Session
init()
destroy()
![Page 11: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/11.jpg)
Servlet Lifecycle
service()- doGet()- doPost()
init()
destroy()
Called by the servlet engine once
Called by the servlet engine to allow the servlet to respond to a request..
Called when the servlet is removed.
![Page 12: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/12.jpg)
Anatomy of a Request
•The servlet’s service() method is invoked with a Request Request and ResponseResponse object
•The servlet provides a response to the request
![Page 13: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/13.jpg)
Request and Response Parameters Request : Encapsulates all informationinformation fromfrom the clientclient
•HTTP request headers
•InputStream or reader
•Input parameters: Form data, cgi data
Response: Encapsulates all communication back to the communication back to the clientclient
•HTTP response headers
•OutputStream or writer
•Setting cookies, redirects, error pages
![Page 14: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/14.jpg)
public class ExampleServlet extends HttpServletextends HttpServlet{ public void doGetdoGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(“text/html”); PrintWriter outPrintWriter out = response.getWriterresponse.getWriter(); out.println(“Hello!<BR>”); }}
A Simple ServletSample Code
![Page 15: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/15.jpg)
What are Java Server Pages?
T A K E I T T O T H E N T H
![Page 16: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/16.jpg)
What is a Java Server Page?
JSP™ turns servlets inside out– Presentation centric
– Java code embedded in a HTML page
– JSP™ is for formatting output such as HTML and XML
![Page 17: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/17.jpg)
HTML to Java Bean Mapping
JSP™ Page
HTTPRequests
Java Bean
JSP™ (Developer View)
Java Bean
Java Bean
Web ServerHTTPRequests
HTTPResponse
JSP™ Page
JAVAServlet
Check if page needs to be recompiled.
Compile page into servletHTTP
Response
Java Bean
Java Bean
Java Bean
Load if neededAnd run servlet JSP™ (Physical
View)
![Page 18: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/18.jpg)
Example Servletpublic class HelloServlet extends HttpServlet{public void doGet(HttpServletRequest request,HttpServletResponse response) { response.setContentType("text/plain"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("Hello World!"); out.println("<br>"); JspCalendar clock = new JspCalendar(); out.println("Today is"); out.println("<ul>"); out.println("<li>Day of month: "); out.println("clock.getDayOfMonth()); out.println("</ul>"); out.println("</html>"); }}
![Page 19: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/19.jpg)
Example JSP<html>
Hello World!<br>
<jsp:useBean id="clock" class=“calendar.JspCalendar” />
Today is
<ul>
<li>Day of month:
<%= clock.getDayOfMonth() %>
</ul>
</html>
JSP™ Page
Java Bean
![Page 20: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/20.jpg)
Usage models of JSPsIntent from the original JSP specifications:
– Model 1:
– Model 2:
• Browser calls JSP directly• JSP uses JavaBeans to encapsulate business logic• JSP delivers the response to the browser
• Browser calls servlet as coordinator• Servlet invokes contains business logic or delegates to JavaBeans• Servlet stores result of business logic in embedded objects in Response• JSP interrogates the Response and creates a page to send the browser
MVC is here to stay!
JSP all the way!
![Page 21: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/21.jpg)
JSP Syntax Basics
•The JavaServer Pages specification includes:– Directives– Standard JSP™ Tags– Scripting elements– Implicit Objects– tag extensions
![Page 22: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/22.jpg)
JSP™ Standard Tags
Example JSP standard tags:
<jsp:useBean id="clock" class=“calendar.JspCalendar” />
<jsp:getProperty name=“customer” property=“name” />
<jsp:setProperty name=“customer” property=“name” param=“username” />
![Page 23: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/23.jpg)
JSP™ Standard Action Tags
• Standard Action tags used to access Java objects from the JSP™ page.
• Example JSP standard tag: <jsp:useBean id="clock" class=“calendar.JspCalendar” />
Like JspCalendar clock = new JspCalendar();The JSP page accesses a bean object via a tag. – If a bean doesn’t exist, it is instantiated.– If bean already exists, it is retrieved from the
session or the request context.
JSP™ Page
Java Bean
![Page 24: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/24.jpg)
What is a JavaBean?public class AccountBeanAccountBean { private String firstName; private String lastName; public AccountBean() {} public String getFirstName() { return firstName; } public void setFirstName(String f) { firstName = f; }. . . } JavaBeans components are Java objects which follow a well-defined design/naming pattern
Java Bean
![Page 25: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/25.jpg)
JSP™ Standard TagsExample JSP standard tags: <html>
<jsp:useBeanuseBean id="accountBeanaccountBean" scope="session" class="AccountBean" />
<jsp:setPropertysetProperty name="accountBeanaccountBean" property="*"/>
<form method=POST action=Account.jsp>
First Name
<INPUT type=text name="firstNamefirstName" >
<INPUT type=submit name="submit" value="Submit">
</form>
SetProperty: Automatically execute all setters in accountBean that match values provided as input
JSP™ Page
Java Bean
![Page 26: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/26.jpg)
JSP™ Standard TagsExample JSP standard tags: <html><jsp:useBeanuseBean id="accountBeanaccountBean" scope="session" class="AccountBean" />
First Name =
<jsp:getPropertygetProperty name="accountBeanaccountBean" property=“firstName" />"
</html>
getProperty: Retrieve value of named attribute
JSP™ Page
Java Bean
![Page 27: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/27.jpg)
JSP™ Scripting Elements Declarations: define page level variables
and methods
<%! QuoteBean q; %>
Scriptlets: JSP Java code fragments <% q = new QuoteBean(); %>
Expressions: results of evaluating the expression are converted to a string
<%= q.getQuote(“SUNW”); %>
![Page 28: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/28.jpg)
Implicit Objects•The JSP™ page has access to certain implicit objects that are always available
•They are the following:
– request - HttpServletRequest– Response –
HttpServletResponse– pageContext, HTTP session, – out, config, page, exception
![Page 29: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/29.jpg)
JSP™ Sample<%@ page info="My JSP™ example“ import="myclasses.*%>
<jsp:useBean id="event“ class="com.sun.jspdemo.webImpl.Event scope="session" />
<html>
<br>
<h3> The event name is:
<jsp:getProperty name="event" property="name" /></h3>
<h3> The event description is: <%= event.getDescription() %> </h3>
<br>
<br>
<% if (event.getName().equalsIgnoreCase("appointment")) {
event.processAppointment(request.getParameter(“name”); %>
<jsp:include page="appointment.html" />
<% }
else { %>
<jsp:include page="regular.html" />
<% } %>
</html>
Directive
ActionTag
Expression
Scriptlet
ImplicitObject
![Page 30: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/30.jpg)
Portable Tag Extension MechanismDesigning tag libraries allows content developers
to use custom tagscustom tags instead of java code.<%@ taglib uri="/WEB-INF/sqltaglib.tld" prefix="ora" %> <HTML> <HEAD> <TITLE>SQL Tags Example </TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF"> <HR> <ora:dbOpen URL="jdbc:oracle:oci8:@" user="scott"
password="tiger" connId="con1"> </ora:dbOpen> <ora:dbQuery connId="con1"> <!-- generates HTML table --> select * from EMP </ora:dbQuery> <ora:dbClose connId="con1" /> <HR> </BODY> </HTML>
![Page 31: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/31.jpg)
Design Issues
T A K E I T T O T H E N T H
![Page 32: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/32.jpg)
View Design Issues• View Consists of two parts
– Request Processing
• (Part of the controller)
– Response Generation
• Presenation Layout
![Page 33: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/33.jpg)
Tasks in Web Layer
Request Validation
Presentation
Control
Business Logic
• Validation of InputValidation of Input
– Is form data complete and well-formed?
•Interaction withInteraction with Business LogicBusiness Logic
•Presentation of contentPresentation of content
–Get Data from Model Value Object
–Present data to client
![Page 34: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/34.jpg)
ViewView: present User Interface (screen representation of model)
Customer
Controller ModelRequest
View
View Selection
State Query(read only)
Updates
Display
JSPCustom JSP ActionsJavaBeans Components
![Page 35: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/35.jpg)
When Servlets, When JSP?•Use servlets for dispatchingdispatching between the Web tier and EJB tier– Request processingRequest processing => use servlet
and/or JavaBeans•Use JSP™ technology for responseresponse
generationgeneration– Displaying HTML, XML => use
JSP™ technology
![Page 36: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/36.jpg)
How Do I Use All These ?• separation between content
and application logic:
– Move request processing out of JSP™ page.
– JSP™ page is primarily presentation layout. JSP
Page(View)
Java Bean(Model)
Sevlet(Controller)
2. Prep
are Jav
a Bea
ns
1. Parse Request
3. Forward Request to JSP™ Page
HTTPRequests
HTTPResponse
![Page 37: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/37.jpg)
View Design Guidelines Build View in the Web-tier
– Use JavaServer Pages™ (JSP™) components for presentation layout
– Custom tags and JavaBeans™ components for presentation logic
![Page 38: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/38.jpg)
All working togetherLogicLogic
Presentation
LayoutLayoutApplication Logic
BusinessBusinessServicesServices
BusinessBusinessDataData
#A
InventoryInventory
Entity BeanEntity Bean
OrderEJBOrderEJB
Entity BeanEntity Bean
mainmainServletServlet
ShowShowConfirmationConfirmation
PagePageJSP
ControllerController
Session BeanSession Bean
Validates inputCalls session bean. . .Calls JSP™ passingValue Model object
Formats HTMLResponds to client
purchaseItems()controls processEnforces transactions
update()Manages data
create()Manages data
View Selection
![Page 39: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/39.jpg)
Example Servlet#A
public void doPost (HttpServletRequest req, HttpServletResponse res) {String userId = req.getSession().getValue(“userId”);
IAccountHome accountHome;IAccountBean account;
Context initCtx = new InitialContext();accountHome = initCtx.lookup(“com/AccountHome”);account = accountHome.findByPrimaryKey(userId);AccountBean accountBean= account.getAccountDetails();
Writer out = res.getWriter();out.println(“<HTML><HEAD><TITLE>Your account</TITLE></HEAD>”);out.println(“<BODY>Name “ + accountBean.getName() + ”.<br>”);out.println(“Address: “ + accountBean.getAddress() );•••
}X
This is ugly — we need some help!
![Page 40: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/40.jpg)
Example JSP#A
<HTML><HEAD><TITLE>Your account</TITLE></HEAD><BODY> <%AccountBean accountBeanaccountBean=(AccountBean) req.getAttribute(“accountBean”); %> Name : <%=accountBean.getName()%> <br>
Address: <%=accountBean.getAddress()%> <br></BODY>
</HTML>
JSPs are a presentation layer for Servlets...
![Page 41: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/41.jpg)
Example JSP#A
<HTML><HEAD><TITLE>Your account</TITLE></HEAD>
<BODY> <jsp:useBeanuseBean id="accountBeanaccountBean" scope=“request" class="AccountBean"/> Name : <jsp:getPropertygetProperty name="accountBeanaccountBean" property=“name" />“
Address:<jsp:getPropertygetProperty name="accountBeanaccountBean “property=“address"/>“
</BODY></HTML>
JSPs are a presentation layer for Servlets...
![Page 42: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/42.jpg)
Example Servlet Dispatching to JSP
#A
public void doPost (HttpServletRequest req, HttpServletResponse res) {String userId = req.getSession().getValue(“userId”);
IAccountHome accountHome;IAccountBean account;
Context initCtx = new InitialContext();accountHome = initCtx.lookup(“com/AccountHome”);account = accountHome.findByPrimaryKey(userId);AccountBean accountBean= account.getAccountDetails();Writer out = res.getWriter();out.println(“<HTML><HEAD><TITLE>Your account</TITLE></HEAD>”);out.println(“<BODY>Welcome “ + accountBean.getName + ”.<br>”);out.println(“Address: “ + accountBean.getAddress() );•••
}
Servlets are Java’s answer to CGI/Perl...
XThis is ugly — we need some help!Java Server Pages to the rescue!
RequestDispatcher dispatcher;ServletContext context =getServletContext();dispatcher = context.getRequestDispatcher(“estore/Account.jsp”);req.setAttribute(“accountBean”, accountBean);dispatcher.forward(req, res);
Why do I care?• Enables separation of presentation logic from presentation layout• Enable your experts to play to their strengths
![Page 43: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/43.jpg)
Resources and Summary
T A K E I T T O T H E N T H
![Page 44: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/44.jpg)
Resources
• http://java.sun.com/j2ee• http://java.sun.com/j2ee/blueprints• http://java.sun.com/j2ee/tutorial• http://java.sun.com/products/servlet
![Page 45: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/45.jpg)
Additional Resources J2EE
java.sun.com/j2ee Sun ONE
www.sun.com/sunone Java Community Process (JCP)
jcp.org Java and XML
java.sun.com/xml Web Services
www.webservices.org
![Page 46: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/46.jpg)
SummaryThis Section Discussed design and implementation of the VIEW. You should now understand:
•View Design IssuesView Design Issues
•Guidelines for Architecting the "View"or Presentation logic
•Servlets for presentation requests
•JSP™ s for presentation layout
![Page 47: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/47.jpg)
Call for Action! Download J2EE 1.3 Reference
Implementation Start developing Servlets and JSPs
today!
![Page 48: Servlets and (JSP)jdurrett.ba.ttu.edu/4383/Notes/servlets_and_java_server_pages.pdf · Servlets vs. CGI Scalable, uses Lightweight threads: Doesn’t start new process for each request,](https://reader033.vdocument.in/reader033/viewer/2022060919/60ab331f5331a375ad172e2a/html5/thumbnails/48.jpg)
www.sun.com/developers/evangcentral
All presentations Audiocasts Codecamp materials Technology briefings code/articles/links/chats/resources
In pursuit of the best software in the universe