02 Servlet Basics
02 Servlet Basics
02 Servlet Basics
S
Servlet
l t Basics
B i
Originals of Slides and Source Code for Examples:
http://courses.coreservlets.com/Course-Materials/csajsp2.html
Overview
Simple Servlets
@WebServlet("/hello")
public class HelloWorld extends HttpServlet {
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
th
throws S
ServletException,
l tE ti IOE
IOException
ti {
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}
URL assumes you have deployed from a project named “test-app”. Code can be downloaded from
Web site. General form is http://hostName/appName/address-from-WebServlet-annotation.
8 Review previous tutorial section for info on how to deploy the app from Eclipse.
Caveat: As of 2010, it has become moderately conventional to use the HTML 5 DOCTYPE: <!DOCTYPE html>. Even though few browsers have full support for HTML 5, this
declaration is supported in practice by virtually all browsers. So, most validators will give some warnings or errors, and you have to search for the “real” errors in the list, or use a
different declaration. My examples use a mix of this doc type, the formal HTML 4 doc type, and the formal xhtml doc type.
10
Eclipse users can use the TestServlet code as a basis for their own servlets.
Avoid usingg “New Servlet” in Eclipse
p since it results in ugly
g y code.
12
@WebServlet("/test-with-utils")
public class TestServlet2 extends HttpServlet {
public void doGet(HttpServletRequest request
request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Test Servlet with Utilities";
out.println
(ServletUtilities headWithTitle(title) +
(ServletUtilities.headWithTitle(title)
"<body bgcolor=\"#fdf5e6\">\n" +
"<h1>" + title + "</h1>\n" +
"<p>Simple servlet for testing.</p>\n" +
"</body></html>");
/ /
}
}
16
TestServlet2: Result
17
© 2011 Marty Hall
Custom URLs
and web.xml
– Resulting URL
• http://hostName/appName/my-address
20
<
<servlet-name>Test</servlet-name>
l t >T t</ l t >
<url-pattern>/test2</url-pattern>
</servlet-mapping>
The part of the URL that comes after the app (project) name.
</web-app> Should start with a slash.
22
• Eclipse details
– Name off Eclipse
li project
j isi “test-app”
– Servlet is in src/testPackage/TestServlet.java
– Deployed by right-clicking on Tomcat,
Tomcat Add and Remove
Projects, Add, choosing test-app project, Finish,
23
right-clicking again, Start (or Restart)
© 2011 Marty Hall
Advanced Topics
26
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
27
• Stop and restart the server
© 2011 Marty Hall
Wrap-Up
Summary
• Main servlet code goes in doGet or doPost:
– The HttpServletRequest contains the incoming
information
– The HttpServletResponse lets you set outgoing
information
• Call setContentType to specify MIME type
• Call
C ll getWriter
tW it tot obtain
bt i a Writer
W it pointing
i ti tto client
li t (browser)
(b )
• Make sure output is legal HTML
• Give address with @WebServlet or web.xml
@WebServlet("/some-address")
public class SomeServlet extends HttpServlet { … }
• Resulting URL
– http://hostName/appName/some-address
29
© 2011 Marty Hall
Questions?