Skip to content

Instantly share code, notes, and snippets.

@bjorn-ali-goransson
Last active August 29, 2015 13:57
Show Gist options
  • Save bjorn-ali-goransson/9439940 to your computer and use it in GitHub Desktop.
Save bjorn-ali-goransson/9439940 to your computer and use it in GitHub Desktop.
Students application (demo of spaghetti code)
<%@page import="java.sql.*" %>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html><head>
<meta charset="utf-8">
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
</head><body>
<div class="container">
<h1>List of students</h1>
<%
String order = request.getParameter("order") != null ? request.getParameter("order") : "id";
String dir = request.getParameter("dir") != null ? request.getParameter("dir") : "asc";
int pageNo = request.getParameter("page") != null ? Integer.parseInt(request.getParameter("page")) : 0;
%>
<table class="table table-bordered">
<thead>
<tr>
<th width="25%">
<% if("id".equals(order)){ %>
#
<% if("asc".equals(dir)){ %>
<i class="fa fa-long-arrow-up"></i>
<% } else { %>
<i class="fa fa-long-arrow-down"></i>
<% } %>
<a href="?order=<%= request.getParameter("order") %>&dir=<%= "asc".equals(dir) ? "desc" : "asc" %>" class="btn btn-default btn-xs btn-primary pull-right" title="Change sort order"><i class="fa fa-random"></i></a>
<% } else { %>
<a href="?order=id">#</a>
<% } %>
</th>
<th width="25%">
<% if("name".equals(order)){ %>
Name
<% if("asc".equals(dir)){ %>
<i class="fa fa-long-arrow-up"></i>
<% } else { %>
<i class="fa fa-long-arrow-down"></i>
<% } %>
<a href="?order=<%= request.getParameter("order") %>&dir=<%= "asc".equals(dir) ? "desc" : "asc" %>" class="btn btn-default btn-xs btn-primary pull-right" title="Change sort order"><i class="fa fa-random"></i></a>
<% } else { %>
<a href="?order=name">Name</a>
<% } %>
</th>
<th width="25%">
<% if("surname".equals(order)){ %>
Surname
<% if("asc".equals(dir)){ %>
<i class="fa fa-long-arrow-up"></i>
<% } else { %>
<i class="fa fa-long-arrow-down"></i>
<% } %>
<a href="?order=<%= request.getParameter("order") %>&dir=<%= "asc".equals(dir) ? "desc" : "asc" %>" class="btn btn-default btn-xs btn-primary pull-right" title="Change sort order"><i class="fa fa-random"></i></a>
<% } else { %>
<a href="?order=surname">Surname</a>
<% } %>
</th>
<th width="25%">
<% if("icon".equals(order)){ %>
Icon
<% if("asc".equals(dir)){ %>
<i class="fa fa-long-arrow-up"></i>
<% } else { %>
<i class="fa fa-long-arrow-down"></i>
<% } %>
<a href="?order=<%= request.getParameter("order") %>&dir=<%= "asc".equals(dir) ? "desc" : "asc" %>" class="btn btn-default btn-xs btn-primary pull-right" title="Change sort order"><i class="fa fa-random"></i></a>
<% } else { %>
<a href="?order=icon">Icon</a>
<% } %>
</th>
</tr>
</thead>
<tbody>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "");
int count = 0;
ResultSet rsCount = con.createStatement().executeQuery("SELECT COUNT(id) as i FROM students");
while (rsCount.next()) {
count = rsCount.getInt("i");
}
String orderBy = " ORDER BY " + order;
orderBy += " " + dir.toUpperCase();
String offset = "";
if(count != 0){
if(request.getParameter("page") != null){
offset = " OFFSET " + (pageNo * 5);
}
}
ResultSet rs = con.createStatement().executeQuery("SELECT * FROM students" + orderBy + " LIMIT 5" + offset);
while (rs.next()) {
%>
<tr>
<th><%= rs.getInt("id") %></th>
<td><%= rs.getString("name") %></td>
<td><%= rs.getString("surname") %></td>
<td><code><%= rs.getString("icon") %></code> <i class="fa fa-<%= rs.getString("icon") %> fa-2x"></i></td>
</tr>
<%
}
%>
</tbody>
</table>
<ul class="pagination">
<% for(int i = 0; i < Math.ceil(count / 5.0); i++){ %>
<li class="<%= (pageNo == i) ? "active" : "" %>"><a href="?order=<%= order %>&dir=<%= dir %>&page=<%= i %>"><%= i + 1 %></a></li>
<% } %>
</ul>
</div>
</body></html>
DROP TABLE IF EXISTS `students`;
CREATE TABLE `students` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` longtext,
`surname` longtext,
`icon` longtext,
PRIMARY KEY (`id`)
);
LOCK TABLES `students` WRITE;
/*!40000 ALTER TABLE `students` DISABLE KEYS */;
INSERT INTO `students` VALUES (1,'Rachel','Morgan','wrench');
INSERT INTO `students` VALUES (2,'Craig','Taylor','minus');
INSERT INTO `students` VALUES (3,'Jean','King','legal');
INSERT INTO `students` VALUES (4,'Jacqueline','Stewart','key');
INSERT INTO `students` VALUES (5,'Jerry','Thompson','microphone-slash');
INSERT INTO `students` VALUES (6,'Annie','Allen','leaf');
INSERT INTO `students` VALUES (7,'Doris','Bailey','gift');
INSERT INTO `students` VALUES (8,'Judith','Wood','calendar-o');
INSERT INTO `students` VALUES (9,'Harry','Reed','flag-checkered');
INSERT INTO `students` VALUES (10,'Sara','Edwards','flag');
INSERT INTO `students` VALUES (11,'Kenneth','Patterson','ellipsis-h');
INSERT INTO `students` VALUES (12,'James','Jones','sort-numeric-asc');
INSERT INTO `students` VALUES (13,'Susan','Wright','download');
INSERT INTO `students` VALUES (14,'Todd','Martinez','external-link');
INSERT INTO `students` VALUES (15,'Kevin','Howard','anchor');
INSERT INTO `students` VALUES (16,'Ruth','Williams','thumbs-o-up');
INSERT INTO `students` VALUES (17,'Heather','Smith','fighter-jet');
INSERT INTO `students` VALUES (18,'Irene','Hughes','square');
INSERT INTO `students` VALUES (19,'Kelly','Nelson','retweet');
INSERT INTO `students` VALUES (20,'Mildred','Robinson','circle-o');
INSERT INTO `students` VALUES (21,'Patrick','White','phone-square');
INSERT INTO `students` VALUES (22,'Deborah','Simmons','square-o');
INSERT INTO `students` VALUES (23,'Benjamin','Butler','rocket');
INSERT INTO `students` VALUES (24,'Scott','James','power-off');
INSERT INTO `students` VALUES (25,'Arthur','Ross','level-up');
INSERT INTO `students` VALUES (26,'Joseph','Lee','video-camera');
INSERT INTO `students` VALUES (27,'Gregory','Harris','print');
INSERT INTO `students` VALUES (28,'Judy','Barnes','code-fork');
INSERT INTO `students` VALUES (29,'Debra','Hill','bell-o');
INSERT INTO `students` VALUES (30,'Douglas','Watson','tag');
INSERT INTO `students` VALUES (31,'Janet','Lewis','fire-extinguisher');
INSERT INTO `students` VALUES (32,'Martha','Washington','cloud-upload');
INSERT INTO `students` VALUES (33,'Katherine','Ramirez','star-half-full');
INSERT INTO `students` VALUES (34,'Helen','Bennett','minus-square-o');
INSERT INTO `students` VALUES (35,'Justin','Davis','lemon-o');
INSERT INTO `students` VALUES (36,'Anthony','Gonzales','fire');
INSERT INTO `students` VALUES (37,'Andrew','Cox','location-arrow');
INSERT INTO `students` VALUES (38,'Jeffrey','Foster','cloud-download');
INSERT INTO `students` VALUES (39,'Mary','Rodriguez','gears');
INSERT INTO `students` VALUES (40,'David','Carter','picture-o');
INSERT INTO `students` VALUES (41,'Johnny','Gray','quote-right');
INSERT INTO `students` VALUES (42,'Charles','Walker','bolt');
INSERT INTO `students` VALUES (43,'George','Hall','shopping-cart');
INSERT INTO `students` VALUES (44,'Bobby','Thomas','plus-square');
INSERT INTO `students` VALUES (45,'Karen','Green','star-o');
INSERT INTO `students` VALUES (46,'Walter','Henderson','share-square');
INSERT INTO `students` VALUES (47,'Andrea','Anderson','keyboard-o');
INSERT INTO `students` VALUES (48,'Christina','Bell','mobile');
INSERT INTO `students` VALUES (49,'Amanda','Evans','search');
INSERT INTO `students` VALUES (50,'Ronald','Perez','crosshairs');
INSERT INTO `students` VALUES (51,'Brenda','Murphy','toggle-left');
INSERT INTO `students` VALUES (52,'Angela','Perry','refresh');
INSERT INTO `students` VALUES (53,'Earl','Lopez','mail-reply');
INSERT INTO `students` VALUES (54,'Lois','Campbell','barcode');
INSERT INTO `students` VALUES (55,'Joe','Gonzalez','frown-o');
INSERT INTO `students` VALUES (56,'Janice','Price','plane');
INSERT INTO `students` VALUES (57,'Daniel','Sanchez','eye');
INSERT INTO `students` VALUES (58,'Donald','Mitchell','meh-o');
INSERT INTO `students` VALUES (59,'Frank','Cooper','ellipsis-v');
INSERT INTO `students` VALUES (60,'Jane','Clark','spinner');
INSERT INTO `students` VALUES (61,'Joyce','Phillips','sitemap');
INSERT INTO `students` VALUES (62,'Brian','Richardson','tasks');
INSERT INTO `students` VALUES (63,'Jennifer','Russell','credit-card');
INSERT INTO `students` VALUES (64,'Howard','Hernandez','sort-down');
INSERT INTO `students` VALUES (65,'Tammy','Peterson','cutlery');
INSERT INTO `students` VALUES (66,'Stephen','Coleman','globe');
INSERT INTO `students` VALUES (67,'Carl','Morris','bars');
INSERT INTO `students` VALUES (68,'Gary','Ward','times-circle-o');
INSERT INTO `students` VALUES (69,'Adam','Long','male');
INSERT INTO `students` VALUES (70,'Russell','Brooks','group');
INSERT INTO `students` VALUES (71,'Eugene','Wilson','superscript');
INSERT INTO `students` VALUES (72,'Lori','Rivera','caret-square-o-up');
INSERT INTO `students` VALUES (73,'Terry','Griffin','pencil-square');
INSERT INTO `students` VALUES (74,'Anna','Young','plus-circle');
INSERT INTO `students` VALUES (75,'Louis','Martin','eye-slash');
INSERT INTO `students` VALUES (76,'Anne','Flores','info-circle');
INSERT INTO `students` VALUES (77,'Cheryl','Sanders','rss-square');
INSERT INTO `students` VALUES (78,'Michelle','Kelly','folder-o');
INSERT INTO `students` VALUES (79,'Jesse','Bryant','check-circle-o');
INSERT INTO `students` VALUES (80,'Randy','Alexander','volume-off');
INSERT INTO `students` VALUES (81,'Billy','Parker','thumb-tack');
INSERT INTO `students` VALUES (82,'Jeremy','Baker','gamepad');
INSERT INTO `students` VALUES (83,'Teresa','Rogers','question-circle');
INSERT INTO `students` VALUES (84,'Ryan','Cook','toggle-up');
INSERT INTO `students` VALUES (85,'Jose','Collins','bookmark-o');
INSERT INTO `students` VALUES (86,'Patricia','Moore','umbrella');
INSERT INTO `students` VALUES (87,'Sandra','Turner','share-square-o');
INSERT INTO `students` VALUES (88,'Joan','Garcia','unlock-alt');
INSERT INTO `students` VALUES (89,'Beverly','Powell','sort-numeric-desc');
INSERT INTO `students` VALUES (90,'Nicole','Diaz','caret-square-o-right');
INSERT INTO `students` VALUES (91,'Rose','Adams','magic');
INSERT INTO `students` VALUES (92,'Joshua','Scott','pencil-square-o');
INSERT INTO `students` VALUES (93,'Philip','Roberts','eraser');
INSERT INTO `students` VALUES (94,'Kathleen','Jenkins','hdd-o');
INSERT INTO `students` VALUES (95,'Larry','Brown','puzzle-piece');
INSERT INTO `students` VALUES (96,'Frances','Jackson','comment');
INSERT INTO `students` VALUES (97,'Julia','Torres','thumbs-down');
INSERT INTO `students` VALUES (98,'Kathryn','Johnson','external-link-square');
INSERT INTO `students` VALUES (99,'Sarah','Miller','mobile-phone');
/*!40000 ALTER TABLE `students` ENABLE KEYS */;
UNLOCK TABLES;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment