S Ltbi Servlet Basics: For Live Java Ee Training, Please See Training Courses
S Ltbi Servlet Basics: For Live Java Ee Training, Please See Training Courses
S
Servlet
l t Basics
B i
Originals of Slides and Source Code for Examples:
http://courses.coreservlets.com/Course-Materials/csajsp2.html
A Servlet’s Job
• Read explicit data sent by client (form data)
• Read implicit data sent by client
(request headers)
• Generate
G t the
th resultslt
• Send the explicit data back to client (HTML)
• Send
S d the
th implicit
i li it data
d t to
t client
li t
(status codes and response headers)
5
A Servlet That Generates Plain
Text (HelloWorld
(HelloWorld.java)
java)
import java.io.*;
import javax.servlet.
javax servlet *;
;
import javax.servlet.http.*;
7
A Servlet That Generates HTML
(Code)
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest request
request,
HttpServletResponse response)
throws ServletException, IOException {
response setContentType("text/html");
response.setContentType( text/html );
PrintWriter out = response.getWriter();
String docType =
<!DOCTYPE HTML PUBLIC \
"<!DOCTYPE \"-//W3C//DTD
//W3C//DTD HTML 4.0 "+
+
"Transitional//EN\">\n";
out.println(docType +
\
"<HTML>\n" +
"<HEAD><TITLE>Hello</TITLE></HEAD>\n"+
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
/ \
"<H1>Hello</H1>\n" +
"</BODY></HTML>");
}
8 }
9
Using Packages
• Create a package
–RR-click
li k on src folder
f ld
– New Package
• Dropping servlet in package
– Copy/paste from filesystem or existing project
– Eclipse will automatically change the package statement
at the top of the .java
java file
• Development strategy
– Start with existingg servlets and use them as the startingg
points for later servlets
• Start with HelloServlet2 at beginning
– Always
y use ppackages
g
– Do not do New Servlet
• Results in ugly code with unnecessary parts
10
Using Packages:
HelloServlet2 (Result)
If you made
d the
th web.xml
b l entries
t i ffrom th
the previous
i llecture,
t you could
ld also
l use
the URL http://localhost/intro/hi2
13
Some Simple HTML-Building
Utilities
public class ServletUtilities {
public static final String
p g DOCTYPE =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">";
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
16
18
Debugging Servlets
• Use print statements; run server on desktop
• Use Apache Log4J
• Integrated debugger in IDE
– Right-click in left margin in source to set breakpoint (Eclipse)
– R
R-click
click Tomcat and use “Debug”
Debug instead of “Start”
Start
• Look at the HTML source
• Return error pages to the client
– Plan ahead for missingg or malformed data
• Use the log file
– log("message") or log("message", Throwable)
• Separate
p the request
q and response
p data.
– Request: see EchoServer at www.coreservlets.com
– Response: see WebClient at www.coreservlets.com
• Make sure browser is not caching
– Internet Explorer: use Shift-RELOAD
– Firefox: use Control-RELOAD
19
• Stop and restart the server
Summary
Questions?