33import java .io .IOException ;
44import java .io .PrintWriter ;
55
6- import javax .servlet .ServletConfig ;
76import javax .servlet .ServletException ;
87import javax .servlet .annotation .WebServlet ;
98import javax .servlet .http .HttpServlet ;
@@ -18,6 +17,7 @@ public class ExtendedServlet extends HttpServlet {
1817 private static final long serialVersionUID = 1L ;
1918
2019 protected PrintWriter out ;
20+ protected String title = "" ;
2121
2222 /**
2323 * @see HttpServlet#HttpServlet()
@@ -27,21 +27,6 @@ public ExtendedServlet() {
2727 // TODO Auto-generated constructor stub
2828 }
2929
30- /**
31- * @see Servlet#init(ServletConfig)
32- */
33- public void init (ServletConfig config ) throws ServletException {
34- // TODO Auto-generated method stub
35- }
36-
37- /**
38- * @see Servlet#getServletConfig()
39- */
40- public ServletConfig getServletConfig () {
41- // TODO Auto-generated method stub
42- return null ;
43- }
44-
4530 /**
4631 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
4732 */
@@ -57,5 +42,62 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
5742 protected void doPost (HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException {
5843 out = response .getWriter ();
5944 }
45+
46+ /**
47+ * Sets the title of the servlet
48+ * @param title
49+ */
50+ public void setTitle (String title ){
51+ this .title = title ;
52+ }
53+
54+ /**
55+ * Gets the title of the servlet
56+ * @return
57+ */
58+ public String getTitle (){
59+ return title ;
60+ }
61+
62+ /**
63+ * Prints out the head including title of the HTML
64+ */
65+ public void doHeader (){
66+ preHeader ();
67+ printTitle ();
68+ postHeader ();
69+ }
70+
71+ /**
72+ * Prints out the opening html tags
73+ */
74+ protected void preHeader (){
75+ out .println ("<!DOCTYPE html>" );
76+ out .println ("<html>" );
77+ out .println ("<head>" );
78+ }
79+
80+ /**
81+ * Prints out the title of the servlet
82+ */
83+ protected void printTitle (){
84+ out .println ("<title>" + title + "</title>" );
85+ }
86+
87+ /**
88+ * Prints out the end of the head and the start of the body
89+ */
90+ protected void postHeader (){
91+ out .println ("</head>" );
92+ out .println ("<body>" );
93+ }
94+
95+ /**
96+ * Prints out the end of body and html tags
97+ */
98+ public void doFooter (){
99+ out .println ("</body>" );
100+ out .println ("</html>" );
101+ }
60102
61103}
0 commit comments