DOC PREVIEW
Penn CIT 597 - Directories and DDs

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 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 14 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 14 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 14 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 14 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Directories and DDsWeb appsDirectoriesPackagesSeparation of concernsTomcat directory structureMy filesmyWebForm.htmlHow Tomcat finds the servletThree namesweb.xmlThe ServletFlowThe EndJan 13, 2019Directories and DDs2Web appsA web application is basically a web site that:“Knows who you are”--it doesn’t just give you static pages, it interacts with youCan permanently change data (such as in a database)A web application can consist of multiple piecesStatic web pages (possibly containing forms)ServletsJSPTomcat organizes all these parts into a single directory structure for each web application...but you have to help with the organization3DirectoriesTo create servlets, you really should have two directory structures:A development directory, in which you can write and partially debug your codeA deployment directory, in which you put “live” codeTomcat requires a particular set of directories for your web applicationIt is extremely picky about having everything in the right place!Since your web application must typically co-exist with other web applications, you should use packages to avoid name conflictsThis further complicates the Tomcat directory structure4PackagesA package statement in Java must be the very first line of code in the fileExample:package com.example.model;import javax.servlet.*;import javax.servlet.http.*;import java.io.*;public class MyServlet extends HttpServlet { ... }This implies thatThis program is in a file named MyServlet.java, which isin a directory named model, which isin a directory named example, which isin a directory named comThat is, the file is something/com/example/model/MyServlet.java5Separation of concernsBusiness logic is the part of your application that does the actual computational workIt is a good idea to separate the business logic of any application from the part that communicates with the outside worldThis allows you to use the same business logic as an applet, a servlet, an application, or an API used by another programIn the following examples we will assume:The business logic is in the package com.example.modelThe servlet logic is in the package com.example.web6Tomcat directory structure jakarta-tomcat-5.5.9/ -- your Tomcat home directory (whatever it’s named) webapps/ myApplicationDirectory/ -- your name for your application directory myWebForm.html -- static HTML pages go in your application directory myJspResult.jsp -- JSP pages also go in your application directory WEB-INF/ -- must have this directory, named exactly like this web.xml -- this is the deployment descriptor, it must have this name lib/ -- you can use this optional directory for external .jar files classes/ -- must have this directory, named exactly like this com/ -- The com.example.model package directory example/ web/ myServlet.class --in package com.example.web; model/ myModel.class -- in package com.example.model;7My filesmyWebForm.htmlThis is the web page with a form that sends data to the servletcom/example/web/myServlet.classThis is the servlet I intend to use; it will use the myModel class, but to do this it needs an import statement:import com.example.model.myModel;com/example/model/myModel.classThis does the “business logic” it is good form to keep it separatemyJspPage.jspThe (optional) JSP page to create the HTML output (could be done directly by myServlet)web.xmlA file required by Tomcat to tell it what class to start with and how to refer to that class8myWebForm.html<html> ... <body> ... <form method="POST" action="NameSeenByUser.do"> ...various form elements... </form> ... </body></html>9How Tomcat finds the servletTomcat reads XML files that tell it about the servletsEach web application must have a Deployment Descriptor (DD) file, WEB-INF/web.xml fileThe root element of the DD file is web-app This element has a bunch of “boilerplate” attributesYou don’t have to know what any of it meansThe rest of the XML file gives names for the servlet, provides parameters, etc.We’ll cover only the most essential parts10Three namesEvery servlet has three names:The “real” name, given to it by the programmerAn “internal” name, used only within the web.xml fileThe name that the user (client) knows it byThe <servlet> element associates the fully-qualified class name with the internal nameThe <servlet-mapping> element associates the internal name with the name known to the clientThe reason for all this is to increase security by hiding the real name from the user11web.xml<?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"> <servlet> <servlet-name>Some internal name</servlet-name> <servlet-class>com.example.web.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Some internal name</servlet-name> <url-pattern>/NameSeenByUser.do</url-pattern> </servlet-mapping> </web-app>12The Servletpublic class MyServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String value = request.getParameter("name"); out.println("<html><body>I got: " + name + " = " + value + "</body></html>"); }}13FlowThe user submits an HTML formTomcat finds the servlet based on the URL and the deployment descriptor (web.xml) and passes the request to the servletThe servlet computes a responseEither:The servlet writes an HTML page containing the responseOr:The servlet forwards the response to the JSPThe JSP embeds the response in an HTML pageTomcat returns the HTML page to the user14The


View Full Document

Penn CIT 597 - Directories and DDs

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 Directories and DDs
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 Directories and DDs 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 Directories and DDs 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?