Session 3 - Servlet
Session 3 - Servlet
Session 3 - Servlet
1
Agenda
• What servlets are all about
• Advantages of servlets
• What JSP is all about
• Free servlet and JSP engines
• Compiling and invoking servlets
• Servlet structure
• A few basic servlets
• Servlet lifecycle
• Initializing servlets
• Debugging servlets
2 Servlet Web Programming – IU – IT – 2010
A Servlet’s Job
• Read explicit data sent by client (form data)
• Read implicit data sent by client
(request headers)
• Generate the results
• Send the explicit data back to client (HTML)
• Send the implicit data to client
(status codes and response headers)
Get
on
board
or
get
out
of
the
way
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
29
Agenda
• Why form data is important
• Processing form data in traditional CGI
• Processing form data in servlets
• Reading individual request parameters
• Reading all request parameters
• Real-life servlets: handling malformed data
• Filtering HTML-specific characters
<FORM ACTION="http://localhost:8088/SomeProgram">
<CENTER>
First name:
<INPUT TYPE="TEXT" NAME="firstName" VALUE="Joe"><BR>
Last name:
<INPUT TYPE="TEXT" NAME="lastName" VALUE="Hacker"><P>
<INPUT TYPE="SUBMIT"> <!-- Press this to submit form -->
</CENTER>
</FORM>
</BODY></HTML>
• See Chapter 16 for details on forms
32 Servlet Web Programming – IU – IT – 2010
Aside: Installing HTML Files
• Tomcat
– install_dir\webapps\ROOT\Form.html or
– install_dir\webapps\ROOT\SomeDir\Form.html
• JRun
– install_dir\servers\default\default-app\Form.html or
– install_dir\servers\default\default-app\SomeDir\Form.html
• URL
– http://localhost/Form.html or
– http://localhost/SomeDir/Form.html
• Custom Web applications
– Use a different directory with the same structure as the default Web
app
– Use directory name in URL (http://host/dirName/…)
– See Chapter 4 of More Servlets & JSP for details.
33 Servlet Web Programming – IU – IT – 2010
HTML Form: Initial Result
<FORM ACTION="http://localhost:8088/SomeProgram"
METHOD="POST">
<CENTER>
First name:
<INPUT TYPE="TEXT" NAME="firstName" VALUE="Joe"><BR>
Last name:
<INPUT TYPE="TEXT" NAME="lastName" VALUE="Hacker"><P>
<INPUT TYPE="SUBMIT">
</CENTER>
</FORM>
</BODY></HTML>
36 Servlet Web Programming – IU – IT – 2010
Sending POST Data
out.println(ServletUtilities.headWithTitle(title) +
"<BODY>\n" +
"<H1>" + title + "</H1>\n" +
"<PRE>\n" +
getCodeFragment() +
"</PRE>\n" +
"Note that you <I>must</I> use curly braces\n" +
"when the 'if' or 'else' clauses contain\n" +
"more than one expression.\n" +
"</BODY></HTML>");
}
55 Servlet Web Programming – IU – IT – 2010
Servlet That Fails to Filter
(Result)
59
Agenda
• Idea of HTTP request headers
• Reading request headers
from servlets
• Example: printing all headers
• Common HTTP 1.1 request headers
• Example: compressing Web pages
• Example: password-protecting Web pages
83
Agenda
• Idea of HTTP status codes
• Setting status codes from servlets
• Common HTTP 1.1 status codes
• A common front end to various Web search
engines
• Idea of HTTP response headers
• Setting response headers from servlets
• Common HTTP 1.1 response headers
• Persistent servlet state and auto-reloading
pages
<!DOCTYPE ...>
<HTML>
...
</HTML>
• Changing the status code lets you perform a
number of tasks not otherwise possible
– Forward client to another page
– Indicate a missing resource
– Instruct browser to use cached copy
• Set status before sending document
85 Servlet Web Programming – IU – IT – 2010
Setting Status Codes
• response.setStatus(int statusCode)
– Use a constant for the code, not an explicit int.
Constants are in HttpServletResponse
– Names derived from standard message.
E.g., SC_OK, SC_NOT_FOUND, etc.
• response.sendError(int code,
String message)
– Wraps message inside small HTML document
• response.sendRedirect(String url)
– Relative URLs permitted in 2.2 and later
– Sets Location header also
• Fix:
Tools, Internet Options,
deselect "Show 'friendly' HTTP error messages"
Not a real fix -- doesn't help unsuspecting users of your pages
93 Servlet Web Programming – IU – IT – 2010
Generating the Server Response:
HTTP Response Headers
• Purposes
– Give forwarding location
– Specify cookies
– Supply the page modification date
– Instruct the browser to reload the page after a designated
interval
– Give the document size so that persistent HTTP
connections can be used
– Designate the type of document being generated
– Etc.