Java Reference
In-Depth Information
However, the next servlet needs to output a FORM tag with an ACTION attribute
specifying the address of another servlet. This address, if we follow the convention
from our previous examples, will already be enclosed by speech marks. If we try to
include both sets of speech marks, then an error will be generated, since what is
intended to be opening of the inner speech marks will be taken as closure of the
outer speech marks. Here is an example of such invalid code:
out.println("<FORM METHOD=POST ACTION='AnyServlet'");
One solution to this apparent problem is to use inverted commas, instead of
speech marks, for the inner enclosure:
out.println("<FORM METHOD=POST ACTION=AnyServlet");
However, provided that we have no spaces within the address that we are using,
we do not actually need either speech marks or inverted commas to enclose the
address, so the following is perfectly acceptable:
out.println("<FORM METHOD=POST ACTION=AnyServlet");
If we wish to enclose any attributes explicitly, though, we must use inverted
commas .
The code for the Selection servlet is shown below.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
@WebServlet("/Selection")
public class Selection extends HttpServlet
{
private fi nal fl oat APPLES_PRICE = 1.45F;
private fi nal fl oat PEARS_PRICE = 1.75F;
//In a real application, above prices would
//be retrieved from a database, of course.
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException,ServletException
{
String currentProduct =
request.getParameter("Product");
HttpSession cart = request.getSession();
cart.putValue("currentProd",currentProduct);
//Places user's selected product into the session
//variable called 'cart'.
Search WWH ::




Custom Search