Documente Academic
Documente Profesional
Documente Cultură
</body>
</html>
The HTML page, 2
<form method="POST" action="SelectBeer.do">
Select beer characteristics:<p>
Color:
<select name="color" size="1">
<option>light</option>
<option>amber</option>
<option>brown</option>
<option>dark</option>
</select>
<br>
<br>
<center>
<input type="SUBMIT">
</center>
</form>
The deployment descriptor
The request goes to the server, with the action
<form method="POST" action="SelectBeer.do">
The name "SelectBeer.do" is not the name of an
actual file anywhere; it is a name given to the user
Partly, this is for security; you don’t want the user to have
access to the actual file without going through your form
The extension .do is just a convention used by this
particular book; no extension is necessary
It is up to the deployment descriptor to find the correct
servlet to answer this request
The deployment descriptor must be named web.xml
web.xml 1 -- boilerplate
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>Ch3 Beer</servlet-name>
<servlet-class>
com.example.web.BeerSelect
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Ch3 Beer</servlet-name>
<url-pattern>/SelectBeer.do</url-pattern>
</servlet-mapping>
BeerSelect.java 1
package com.example.web;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
}
BeerSelect.java 2
String c = request.getParameter("color");
request.setAttribute("styles", result);
RequestDispatcher view =
request.getRequestDispatcher("result.jsp");
view.forward(request, response);
}
MVC
BeerSelect.java acts as the controller
It delegates the actual work to a model,
BeerExpert.java
It delegates (forwards) the information to a JSP page
that will provide the view
RequestDispatcher view =
request.getRequestDispatcher("result.jsp");
view.forward(request, response);
The model class
BeerExpert is the model class; it computes results and
adds them to the HttpServletRequest object
Not the HttpServletResponse object; that’s the HTML
output
It returns, in the usual fashion, to the BeerSelect class,
which will then forward it to the JSP
BeerExpert.java
package com.example.model;
import java.util.*;
<html>
<body>
<h1 align="center">Beer Recommendations JSP</h1>
<p>
<%
List styles = (List)request.getAttribute("styles");
Iterator it = styles.iterator();
while (it.hasNext()) {
out.print("<br>TRY: " + it.next());
}
%>
</body>
</html>
Directory structure
jakarta-tomcat-5.0.12/
| webapps/ this is http://m174pc4.cis.upenn.edu:8080/
| | beerV1/
| | | form.html
| | | result.jsp
| | | WEB-INF/
| | | | web.xml
| | | | classes/
| | | | | com/
| | | | | | example/
| | | | | | | model/
| | | | | | | | BeerExpert.class
| | | | | | | web/
| | | | | | | | BeerSelect.class
| | | | lib/
| | yourLastName when you ftp, this is where you are
Accessing the class server
Tomcat should be running 24/7 on m174pc4.cis.upenn.edu
To try it, point your browser to:
http://m174pc4.cis.upenn.edu:8080/beerV1/form.html
When you ftp to m174pc4, pwd will tell you that you are in a
directory “/”, but you are really in a directory
C:\Tomcat\webapps\yourLastName
This is the top-level directory for your web applications
You should be able to put an HTML file here, say, index.html,
and access it with
http://m174pc4.cis.upenn.edu:8080/yourLastName/index.html
The End