Transcript ppt
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
1
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 pages2
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
3
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
4
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
5
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
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
7
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 8
F15 Class
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class F15 extends HttpServlet
{
9
F15 doGet Method
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
this.doPost(request, response);
}
10
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
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 + “<HTML>\n” +
“<HEAD><TITLE>F15 Response</TITLE></HEAD>” +
“ \n<BODY>”);
12
F15 doPost Method
// now compose our response based on the form input data
out.println("To: " + name + "<br><br>" + title);
out.println("<p>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("</BODY></HTML>");
}
}
13
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
14
from any debug write statements you use in your code
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
15
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
16
Tomcat Configuration (tomcat-users.xml)
• User account names, passwords and privileges
<tomcat-users>
<role rolename=“manager”/>
<role rolename=“admin”/>
<user username=“admin”
password=“********”
roles=“admin,manager”/>
</tomcat-users>
17
Tomcat Configuration (context.xml)
• Context Useful for Development Activity
<Context reloadable="true" privileged="true">
• 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
18
Tomcat Configuration (web.xml)
• In folder conf/web.xml
• Invoker Servlet Definition / Initialization
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet
</servlet-class>
<init-param>
<param-name>fork</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>xpoweredBy</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
19
Tomcat Configuration (web.xml)
• In folder conf/web.xml
• Invoker Servlet Mapping to URL
<servlet-mapping>
<servlet-name>invoker</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
20
Tomcat Configuration (web.xml)
• In folder myapp/WEB-INF/web.xml
• F15 Servlet Definition / Initialization
<servlet>
<servlet-name>F15</servlet-name>
<servlet-class>F15</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
</servlet>
21
Tomcat Configuration (web.xml)
• In folder myapp/WEB-INF/web.xml
• F15 Servlet Mapping to URL
<servlet-mapping
<servlet-name>F15</servlet-name>
<url-pattern>/F15</url-pattern>
</servlet-mapping>
22
Configure Tomcat Service
Can use buttons to start and stop the Tomcat Service
23