Presentation is loading. Please wait.

Presentation is loading. Please wait.

111 Java Servlets Dynamic Web Pages (Program Files) Servlets versus Java Server Pages Implementing Servlets Example: F15 Warranty Registration Tomcat Configuration.

Similar presentations


Presentation on theme: "111 Java Servlets Dynamic Web Pages (Program Files) Servlets versus Java Server Pages Implementing Servlets Example: F15 Warranty Registration Tomcat Configuration."— Presentation transcript:

1 111 Java Servlets Dynamic Web Pages (Program Files) Servlets versus Java Server Pages Implementing Servlets Example: F15 Warranty Registration Tomcat Configuration for Servlet Processing Starting and Stopping Tomcat Service

2 222 Dynamic Web Pages The URL points to an “executable program” file instead of to a “static” HTML document file The program generates response based on input request parameters and data stored on the server It writes response in HTML format to stdout These programs can be scripts PERL, PHP, etc or compiled C CGI programs They can also be Java Programs, e.g. Servlets Servlets can be auto-generated from JSP pages

3 33 Servlets versus Java Server Pages A servlet is a.java source file with code that reads user submitted parameters and writes HTML formatted text back to the user It can also access or update files on the server Much like any Java program: –It is compiled to a.class file –Data is displayed to user based on java statements

4 44 Servlets versus Java Server Pages A Java Server Page is a “markup document” with snippets of Java code included to control processing/generation of response to the user It can also access or update files on the server Much like any HTML page: –It is not compiled by the developer (a servlet is generated and compiled behind the scenes) –Data is displayed to user based on markup text

5 55 Servlets versus Java Server Pages There are advantages and disadvantages for using a servlet or a Java Server Page (JSP) Primarily consider the ratio of code to markup: –The more code the greater the case for a servlet –The more markup the greater the case for a JSP The code sections of a JSP may become difficult to debug because the compiler works on the generated ".java" source file - not directly on the JSP For a page with simple server-side functions, such as altering output based on a few request parameters, a JSP can be much simpler to build than a servlet

6 66 Java Servlets Import javax.servlet.* and javax.servlet.http.* Class extends httpServlet (An abstract class) No need to implement a constructor method Use methods init and destroy – like Applets Implement methods doPost and/or doGet Compile your servlet source code as usual Save class file in myapp/WEB-INF/classes 6

7 777 Java Servlets GET method –If your form has METHOD="Get" in its FORM tag, implement the doGet method POST method –If your form has METHOD="Post" in its FORM tag, implement the doPost method For flexibility, implement both methods –Have one method call the other passing the received arguments so you don’t need to write the code twice

8 888 Java Servlets Example F15.html sends either Get or Post request To localhost/myapp/F15 (a Servlet) Tomcat servlet container invokes either the doGet method or doPost method depending on the type of request received doGet/doPost obtains the input data via calls to the request object’s getParameter method Generates HTML response via response object’s methods and PrintWriter object obtained from response.getWriter method

9 9 F15 Class import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; public class F15 extends HttpServlet { 9

10 10 F15 doGet Method public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } 10

11 11 F15 doPost Method public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // get the input data from the form String name = request.getParameter("Name"); String title = request.getParameter("Title"); String model = request.getParameter("Model");... 11

12 12 F15 doPost Method // start the usual stuff for the response response.setContentType("text/html"); PrintWriter out = response.getWriter(); // PrintWriter is the same class as System.out String docType = “<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0” + “Transitional//EN\”>\n”; out.println(docType + “ \n” + “ F15 Response ” + “ \n ”); 12

13 13 F15 doPost Method // now compose our response based on the form input data out.println("To: " + name + " " + title); out.println(" We at MakDonut-Duglass wish you the ” + “best of luck using your F15 " + model + " model to " +... // and end with the usual stuff for the response out.println(" "); } 13

14 14 Tomcat File Directory Structure Path to Tomcat in Program Files C:\Program Files\Apache Software Foundation\Tomcat 6.0 Top Level Subdirectories of Interest \conf – contains configuration files in xml format \webapps – contains ROOT and “myapp” sub-directories for the top level pages of web applications \work – contains source code and class files for servlets generated from JSP pages (many levels below) \logs – contains log files with error messages or output from any debug write statements you use in your code

15 15 Tomcat Configuration Tomcat has overall configuration files in: –Tomcat 6.0\conf folder server.xml tomcat-users.xml context.xml web.xml Tomcat has configuration files for individual web applications in: –Tomcat 6.0\webapps\myapp\WEB-INF web.xml

16 16 Tomcat Configuration (server.xml) Contains configuration for port number on which the service will be offered <Connector port=“80” protocol=“HTTP/1.1” connectionTimeout=“20000” redirectPort=“8443” /> Default file came set up with port 8080

17 17 Tomcat Configuration (tomcat-users.xml) User account names, passwords and privileges <user username=“admin” password=“********” roles=“admin,manager”/>

18 18 Tomcat Configuration (context.xml) Context Useful for Development Activity Reloadable = Enables monitoring of servlet class files for reloading without server restart Privileged = Allows Use of Invoker Servlet –Allows access to servlets without a definition and a mapping in the web.xml configuration file(s) Both normally set to false for production

19 19 Tomcat Configuration (web.xml) In folder conf/web.xml Invoker Servlet Definition / Initialization jsp org.apache.jasper.servlet.JspServlet fork false xpoweredBy false 3

20 20 Tomcat Configuration (web.xml) In folder conf/web.xml Invoker Servlet Mapping to URL invoker /servlet/*

21 21 Tomcat Configuration (web.xml) In folder myapp/WEB-INF/web.xml F15 Servlet Definition / Initialization F15 debug 2

22 22 Tomcat Configuration (web.xml) In folder myapp/WEB-INF/web.xml F15 Servlet Mapping to URL <servlet-mapping F15 /F15

23 23 Configure Tomcat Service Can use buttons to start and stop the Tomcat Service


Download ppt "111 Java Servlets Dynamic Web Pages (Program Files) Servlets versus Java Server Pages Implementing Servlets Example: F15 Warranty Registration Tomcat Configuration."

Similar presentations


Ads by Google