DOC PREVIEW
Penn CIT 597 - An Example Servlet

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

An Example ServletCreditsIt starts with an HTML form...The HTML page, 1The HTML page, 2The deployment descriptorweb.xml 1 -- boilerplateweb.xml 2 -- actual workBeerSelect.java 1BeerSelect.java 2MVCThe model classBeerExpert.javaThe JSP fileresult.jspDirectory structureAccessing the class serverThe EndJan 13, 2019An Example ServletPutting it all togetherCreditsThis is the first example in Head First Servlets & JSP by Brian Basham, Kathy Sierra, and Bert BatesThis is an excellent book, and goes into considerably more detail than we will in this courseIt starts with an HTML form...The HTML page, 1<html> <head> <title>Beer Selection</title> </head> <body> <h1 align="center">Beer Selection Page</h1> ...the form (on the next slide)... </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.xmlweb.xml 1 -- boilerplate<?xml version="1.0" encoding="ISO-8859-1"?><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"> ...important stuff goes here... </web-app>web.xml 2 -- actual work<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 1package com.example.web;import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.util.*;import com.example.model.BeerExpert; // notice thispublic class BeerSelect extends HttpServlet { ... doPost method goes here. ..}BeerSelect.java 2 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String c = request.getParameter("color"); BeerExpert be = new BeerExpert(); List result = be.getBrands(c); 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 JSPBeerExpert.javapackage com.example.model;import java.util.*;public class BeerExpert { public List getBrands(String color) { List brands = new ArrayList(); if (color.equals("amber")) { brands.add("Jack Amber"); brands.add("Red Moose"); } else { brands.add("Jail Pale Ale"); brands.add("Gout Stout"); } return brands; }}The JSP fileThe JSP file must have the extension .jspIt is basically HTML, plus a few JSP directivesIt receives the HttpServletRequest and the HttpServletResponse objectsThe HttpServletResponse object may have been partially written by the servlet (but it’s a bad idea)The resultant HTML page goes back to the userresult.jsp<%@ page 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 areAccessing 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 directoryC:\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 withhttp://m174pc4.cis.upenn.edu:8080/yourLastName/index.htmlThe


View Full Document

Penn CIT 597 - An Example Servlet

Documents in this Course
DOM

DOM

21 pages

More DOM

More DOM

11 pages

Rails

Rails

33 pages

DOM

DOM

21 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

Rake

Rake

12 pages

Ruby

Ruby

58 pages

DOM

DOM

21 pages

Tomcat

Tomcat

16 pages

DOM

DOM

21 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Html

Html

27 pages

DOM

DOM

22 pages

RELAX NG

RELAX NG

30 pages

Servlets

Servlets

28 pages

XHTML

XHTML

13 pages

DOM

DOM

21 pages

DOM

DOM

21 pages

Servlets

Servlets

26 pages

More CSS

More CSS

18 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Load more
Download An Example Servlet
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view An Example Servlet and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view An Example Servlet 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?