Download www.coreservlets.com

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
How to Write MVC
Webapps
Core Servlets & JSP book: www.coreservlets.com
More Servlets & JSP book: www.moreservlets.com
Servlet and JSP Training Courses: courses.coreservlets.com
1
Slides © Marty Hall, http://www.coreservlets.com, book © Sun Microsystems Press
Agenda
g
•
•
•
•
2
How to design a webapp using MVC
Development and Deployment Strategies
Understand how a webapp works
Learn how to write a webapp using MVC
Form Data
www.coreservlets.com
The Design
g Phase
• Generate the User View
– Decide how it is to appear and what it does
– Write it (mostly) in HTML
– Decide what the overall response will be
• Design the Architecture: here MVC
– Label the components
p
appropriately
pp p
y
– Show interactions between components
3
Form Data
www.coreservlets.com
WineApp
pp Architecture
Container
2
Servlet
3
1
4
5
Model
6
request
form.html
4
result.jsp
jp
1. Container retrieves form.html
4. Result into request
2. Container calls action
q
to JSP
5. Forward request
3. Servlet calls model
6. JSP obtains result from
request
www.coreservlets.com
Form Data
Set up Development
Environment
• Make a root directory, call it, e.g. wine
• Make the following subdirectories
– etc: For your web.xml
– lib:
lib For
F thi
thirdd party
t JAR files
fil
– src: Remember packaging strategies! Also separate
model and controller code!
– classes: Ditto
– web: For your HTML’s and JSP’s
5
Form Data
www.coreservlets.com
Set up Deployment
Environment
• Make a new subdirectory under webapps,
h i
having
same name as your app, i.e.
i wine
– Put your HTML’s and JSP’s in here
– This is Tomcat
Tomcat’ss “context
context root
root” for resolving URL
URL’ss
• Mirror your development classes directory
structure,
st
uctu e, sepa
separating
at g controllers
co t o e s and
a d
models
– Note the extra subdirectory model for POJ
– Note the
h extra subdirectory
bdi
web for
f servlets
l
– Be sure to maintain your package structure
6
Form Data
www.coreservlets.com
The Implementation
p
Phase
•
•
7
Start with the initial HTML Form page
Develope incrementally
1. Write the form.html
2 Do
2.
D th
the web.xml
b
l thing
thi
3. Test
4. Write the servlet action code
5. Compile & Test
6. Write the model in POJ (and modify the servlet)
7 Compile
7.
C
il & Test
T t
8. Modify the servlet to call the model
9. Co
Compile
p e & Test
est
10. Add on the JSP (and modify the servlet)
11. Compile & Test
www.coreservlets.com
Form Data
Write the form.html
<html><body>
<h1 align="center">Wine
align= center >Wine Selection Page</h1>
<form method="POST"
action="SelectWine.do">
Select
Se
ect Wine
e C
Characteristics<p>
a acte st cs p
Color:
<select name="color" size="1">
<option value="red"> red </option>
<option value="white"> white </option>
<option value="rose"> </option>
</select>
<br><br>
<center>
<input type="SUBMIT">
</center>
</form></body></html><
8
Form Data
www.coreservlets.com
Comments
• Note we are using POST
• The action routine is the servlet
WineSelect.do
– Note
N t the
th .do
d suffix.
ffi This
Thi is
i a convention
ti
• We are using the input type select to
choose one value from a scrolled list
• This is only one possibility, depending on
your app.
y
pp Your choice may
y be different
9
Form Data
www.coreservlets.com
Write the web.xml
• You already know how
• Pay attention to the name in the action
routine
10
Form Data
www.coreservlets.com
Test
• There is no action routine yet
• Just want to see if the initial HTML page is
correct and appears as desired
• Start
St t it from
f
your browser
b
• Will be demonstrated in class
11
Form Data
www.coreservlets.com
Write the Action Servlet for
form html
form.html
package com.example.web;
import com
com.example.model.*;
example model *;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
public class WineSelect extends HttpServlet{
public void doPost(HttpServletRequest request
request,
HttpServletResponse response)
throws IOException, ServletException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String c = request.getParameter("color");
out.println(”Wine Selection Advice<br>");
out.println("<br>Got
out.println(
<br>Got Wine color " + c);
}}
12
Form Data
www.coreservlets.com
Compile
p and Test
• Remember to shutdown and startup Tomcat
each
h time
i
you run a webapp
b
• When “Submit” is clicked the output of the
servlet should be something like
Wine Selection Advice
Got wine color white
13
Form Data
www.coreservlets.com
Write the Model
package com.example.model;
import java
java.util.
util *;
;
public class WineExpert{
public
pub
c List
st get
getLabels(String
abe s(St
g co
color){
o ){
List labels = new ArrayList();
if (color.equals("red")){
labels.add("Chateau Fight Club");
labels.add("Dracula Dark");}
else if (color.equals("white")){
labels.add("Gutter Delight");
labels.add("Pale Amoeba");}
else{
labels.add("Hemingway Knockout");
labels add("Vampire Lite")
labels.add("Vampire
Lite");}
}
return (labels);}}
14
Form Data
www.coreservlets.com
Remarks
• The model is just a Java program and
k
knows
nothing
hi
about
b
the
h app from
f
which
hi h it
i
will be called
• It communicates with the app by means of a
list of labels
• Of course the servlet will have to be
modified to make this call (next incremental
stage)
g )
15
Form Data
www.coreservlets.com
Modify the Servlet to Call the
Model
16
package com.example.web;
import com.example.model.*;
i
import
j
javax.servlet.*;
l
*
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
i
import
t j
java.sql.*;
l *
public class WineSelect extends HttpServlet{
public void doPost(HttpServletRequest request,
HttpServletResponse response)
th
throws
IOE
IOException,
ti
S
ServletException{
l tE
ti {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String c = request.getParameter("color");
Wi E
WineExpert
t we = new WineExpert();
Wi E
t()
List result = we.getLabels(c);
out.println("Wine Selection Advice<br>");
Iteratot iter = result.iterator();
while
hil (it
(iter.hasNext()){
h N t()){
out.print("<br>try: " + iter.next());}
}}
www.coreservlets.com
Form Data
Remarks
• Note the import statement for the model
• We cycled through the arrayList
generated by the model using an iterator
• The
Th list
li t became
b
available
il bl by
b the
th call
ll
mechanism
String c =request.getParameter(“color”);
List result = getLabels(c);
17
Form Data
www.coreservlets.com
Compile
p and Test
• Nothing new, but oh so necessary
18
Form Data
www.coreservlets.com
Add on a JSP for Output
p
<%@ page import="java.util.*" %>
<html>
ht l
<body>
<h1 align="center">Wine Recommendations JSP Style</h1>
<p>
<%
List styles = (List)request.getAttribute(
(List)request.getAttribute("styles");
styles );
Iterator it = styles.iterator();
while (it.hasNext()){
out.print("<br>try: " + it.next());
}
%>
</body>
</html>
19
Form Data
www.coreservlets.com
And Modify the Servlet
Accordingly:
package com.example.web;
i
import
t com.example.model.*;
l
d l *
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
public class WineSelect extends HttpServlet{
public void doPost(HttpServletRequest request
request,
HttpServletResponse response)
throws IOException, ServletException{
String c = request.getParameter("color");
WineExpert we = new WineExpert();
List = we.getLabels(c);
request.setAttribute("styles", result);
RequestDispatcher view =
request.getRequestDispatcher("result.jsp");
view.forward(request, response);}}
20
Form Data
www.coreservlets.com
Remarks
• Note the dispatching mechanism for
communication
i i between
b
the
h servlet
l and
d the
h
JSP
– Just copy this for now
– Note that you literally make available the Request and
Response objects to the JSP. These objects are now
within
i hi the
h JSP’s
JSP’ requestt scope.
– Note the getAttribute call in the JSP
21
Form Data
www.coreservlets.com
Compile
p and Test
• You should see a JSP with something like:
Wine Recommendations JSP
Style
try: Chateau Fight Club
try: Dracula Dark
• If you do, open a bottle and celebrate!
22
Form Data
www.coreservlets.com
Questions?
Core Servlets & JSP book: www.coreservlets.com
More Servlets & JSP book: www.moreservlets.com
Servlet and JSP Training Courses: courses.coreservlets.com
23
Slides © Marty Hall, http://www.coreservlets.com, book © Sun Microsystems Press