Skip to content

Commit cc96602

Browse files
committed
Rearranged stuff in servlet
1 parent ae5e350 commit cc96602

File tree

2 files changed

+78
-54
lines changed

2 files changed

+78
-54
lines changed

src/com/sleightholme/jfs/ExtendedServlet.java

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.io.IOException;
44
import java.io.PrintWriter;
55

6-
import javax.servlet.ServletConfig;
76
import javax.servlet.ServletException;
87
import javax.servlet.annotation.WebServlet;
98
import 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
}

src/com/sleightholme/jfs/JFSServlet.java

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import javax.servlet.http.HttpServlet;
66

7+
import com.sleightholme.jfs.util.InvalidFileTypeException;
8+
79
/**
810
*
911
*/
@@ -14,49 +16,29 @@
1416
*/
1517
public class JFSServlet extends ExtendedServlet {
1618

17-
protected PrintWriter out;
18-
protected String title = "";
19-
20-
public void setTitle(String title){
21-
this.title = title;
22-
}
23-
24-
public String getTitle(){
25-
return title;
26-
}
27-
28-
/**
29-
* Prints out the head including title of the HTML
30-
*/
31-
public void doHeader(){
19+
public void doHeader(String...imports){
3220
preHeader();
33-
out.println("<title>" + title + "</title>");
21+
printTitle();
22+
imports(imports);
3423
postHeader();
24+
3525
}
3626

3727
/**
38-
* Prints out the opening html tags
39-
*/
40-
protected void preHeader(){
41-
out.println("<html>");
42-
out.println("<head>");
43-
}
44-
45-
/**
46-
* Prints out the end of the head and the start of the body
28+
* Imports CSS or Javascript to be added to the page
29+
* No other filetypes can currently be imported
30+
* @param imports
4731
*/
48-
protected void postHeader(){
49-
out.println("</head>");
50-
out.println("<body>");
32+
protected void imports(String...imports){
33+
for (String imported : imports){
34+
if (imported.endsWith(".css")){
35+
out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"" +imported +"\">");
36+
} else if (imported.endsWith(".js")){
37+
out.println("<script src=\"" + imported + "\"></script>");
38+
} else {
39+
throw new InvalidFileTypeException("Only .css or .js files can be imported");
40+
}
41+
42+
}
5143
}
52-
53-
/**
54-
* Prints out the end of body and html tags
55-
*/
56-
public void doFooter(){
57-
out.println("</body>");
58-
out.println("</html>");
59-
}
60-
61-
6244
}

0 commit comments

Comments
 (0)