Java Reference
In-Depth Information
Of course, if a computer's Internet connection isn't working, the Web page and servlet can't be accessed and,
if the server malfunctions, no one can access the functions. So, there are some disadvantages to server-based
applications.
Notice that to display results, servlets require the programmer to enter the HTML; there is no GUI to generate the
HTML. For professional looking Web pages, this is not a trivial amount of coding. Fortunately, there is an alternative to
servlets called
Java Server Pages
. JSPs make defining a result page much easier. We will cover JSPs in the next chapter.
Results of the Tutorial
Time to go over the results:
1.
In Tutorials, a new package called c8.
2.
A new project called TutorialsWeb, with the following four files (that you created):
MyServlet.java MyServlet.class
Howdy.html EnterEmpInfoForm.html
3.
A copy of the Howdy.html file on one of the secondary storage drives (e.g., C:\ or G:\)
4.
When run on the server:
•
Howdy.html will display static text
•
EnterEmpInfoForm.html displays fields to enter employee information and,
when the submit button is pressed, invokes MyServlet
5.
When specified in the browser address field, MyServlet will display static text.
6.
When invoked from EnterEmpInfoForm, MyServlet will display static text and the
employee number from EnterEmpInfoForm.
7.
MyServlet's source code should look like the following:
import
java.io.IOException;
import
javax.servlet.ServletException;
import
javax.servlet.annotation.WebServlet;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
java.io.PrintWriter;
public class
MyServlet
extends
HttpServlet {
PrintWriter out;
private static final long
serialVersionUID
= 1L;
public
MyServlet() {
super
();
}
protected void
doGet(HttpServletRequest req,
HttpServletResponse resp)
throws
ServletException, IOException {
getStarted(resp);
out.println("<Title>MyServlet Get Response</Title>");