Refactoring of Server Pages

Thomas Bayer
Orientation in Objects GmbH
Tobias Kieninger
Orientation in Objects GmbH
Thomas Bayer
Thomas Bayer
Tobias Kieninger
Tobias Kieninger
Datum:Juni 2002

A Server Page contains a lot of Java code between template text.

Extract code from the Server Page to a bean. Refactor the bean and move template text back to the server page.

<%@ page import="java.util.*" %>
	GregorianCalendar cal =  new GregorianCalendar();

	int month = cal.get(Calendar.MONTH);
	int year = cal.get(Calendar.YEAR);	

	cal.set(Calendar.DAY_OF_MONTH, 1);

	int dow = cal.get(Calendar.DAY_OF_WEEK);

	String mns[] = { "January", "February", "March", "April",
					 "May", "June", "July", "August", "September",
					 "October", "November", "December" };

	%><h1><%=mns[month]%> <%=year%>


	int ld[] = { 31, 28, 31, 30, 31, 31, 30, 31, 30, 31 };

	if (month == 1 && cal.isLeapYear(year))
		ld[1] = 29;

	for (int i = 1; i < ld[month] + dow + 1; i++) {
		if (i - dow > 0) {
			%><td><%=i - dow%></td><%
		} else {
		if ( i % 7 == 0) { 


<%@ page import="java.util.*" %>
<jsp:useBean id="calendarBean" scope="request" class="CalendarBean"/>
	<% calendarBean.init( request, out); %>
			for (int i = 1; calendarBean.continueMonthLoop(i); i++) {




Java Code and HTML template text are mixed up in one server page file. Presentation and logic are often not clearly separated. Such server pages tend to be difficult to read and understand. A lot of changes during the lifetime of the page may have caused duplicate code and markup.

Separating code from the markup makes the server page easier to understand and modify. The separated code could be further refactored using Extract Method, Extract Class, Move Method ...


This refactoring uses a lot of small refactorings, and is related to the Replace Method With Method Object.

import javax.servlet.jsp.*;
import javax.servlet.http.*;

public class CalendarBean
	private JspWriter out;
	private HttpServletRequest request;

	public void init( HttpServletRequest requestArg,
					  JspWriter outArg)
		out = outArg;
		request = requestArg;

	public void doSomething() throws IOException


Let´s refactor a simple calendar page. You can see a screenshot at figure 1.

JSP Kalender

Figure 1.)

Let´s reflect about the refactoring we have done. The server page only contains the needed Java code and allmost all markup. The JSP is much more understandable. The layout and behaviour is now easier to modify. Large chunks of code are encapsulated in the Bean. Refactoring parts of the server page inside an Method Object facilitates further refactorings like Extract Method, Move Method, Extract Class and Replace TypeCode with Strategy or State.

Addional Comments

In addition to beans you can use taglibs to refactor JaveServer Pages or eXtensible Server Pages.

If your server pages contains only short fragments of Java code, you can use a modification to this refactoring. Instead of extracting a block from the server page to the doSomething() method you extract only short blocks in separate methods.

Zum Geschaeftsbreich Competence Center
Apache Tomcat Administration und Konfiguration:
Lernen Sie mehr über Tomcat, die Referenzimplementierung für JSP und Servlets.