WWW Portal

JSP

Hopefully this will be a presentation of useful examples and possibly explanations of JSP and related code. So, for starters, this is a script I wrote for myself. It’s basically a bookmark page or WWW list portal. Data validation is very important, but, for now, it is not addressed here. It’s a simple application and it works.

Here is a database table that should be sufficient for the purpose. -- DROP TABLE WebLink ; CREATE TABLE WebLink ( 	id INTEGER, -- unique id 	type VARCHAR(55), -- page, domain, ??? 	url VARCHAR(255),  	description VARCHAR(255)  ) ;

Here is a screen shot of the main page.



Here is the code.

&lt;%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.text.*" import="java.io.*" import="java.sql.*" import="java.util.*" errorPage="" %> &lt;%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> &lt;% /* * Portal.jsp * * Copyright (c) 2013 Alfred J Wheeler * All rights reserved. * * This software is the proprietary * information of Alfred J Wheeler * */ /**  * The purpose of this program is to provide quick links to  * favorite or frequently visited WWW pages. * * USAGE *  Portal.jsp * * @author      7/19/2013 created by Alfred J Wheeler. * * @version     0.1 * * @see         PortalAdd, PortalView, PortalEdit, PortalRemove * */ /*  * Database : demonstration, Table : weblink *  * field name                        data type * ---  ---  * id                                integer * type                             character varying(55) * url                              character varying(255) * description                      character varying(255) */ /* * Initialize some variables */ String message = ""; long dt = 0; String title = null; Connection conn = null; Statement st = null; ResultSet rs = null; String sql = ""; /* * Get a date time stamp. Set a page title, if needed... */ dt = System.currentTimeMillis; title = "Portal"; /* * Get the database connection values, etc... from the properties file * ( This eventually will be externalized to reduce code clutter... ) */ String p = application.getRealPath("/") + "properties/Demo.properties"; InputStream propsFile; Properties appProp = new Properties; try { propsFile = new FileInputStream(p); appProp.load(propsFile); propsFile.close; } catch (IOException ioe) { out.println("&lt;PRE>"); out.println("I/O Exception."); ioe.printStackTrace; out.println("&lt;/PRE>"); } String dbDriver = "" + appProp.getProperty("dbDriver"); String dbUrl   = "" + appProp.getProperty("dbUrl"); String dbUid   = "" + appProp.getProperty("dbUid"); String dbUpw   = "" + appProp.getProperty("dbUpw"); String css     = "" + appProp.getProperty("css"); //out.println("&lt;PRE>"); //out.println(dbDriver); //out.println(dbUrl); //out.println(dbUid); //out.println(dbUpw); //out.println("&lt;/PRE>"); /* * Open the database */ try { Class.forName(dbDriver); conn = DriverManager.getConnection(dbUrl, dbUid, dbUpw); st = conn.createStatement; %> &lt;!DOCTYPE html> &lt;HTML> &lt;HEAD> &lt;link rel="stylesheet" type="text/css" href="&lt;%=css%>"> &lt;TITLE>&lt;%=title%>&lt;/TITLE> &lt;SCRIPT LANGUAGE="JavaScript" SRC="angular.js">&lt;/SCRIPT> &lt;/HEAD> &lt;BODY> &lt;H1>&lt;%=title%>&lt;/H1> &lt;br> &lt;div class="centered"> &lt;nobr> &lt;form action="PortalAdd.jsp" name=f> &lt;input name=btnG type=submit value=" Add New Link "> &lt;/form> &lt;/nobr> &lt;/div> &lt;br> &lt;TABLE border="1" cellspacing="0"> &lt;TBODY> &lt;th colspan="2">Web Sites / Pages&lt;/th> &lt;% sql = "SELECT " + "id, " + "type, " + "url, " + "description " + "FROM weblink " + "ORDER BY UPPER( description )"; //		out.print("sql = " + sql + " \n"); rs = st.executeQuery(sql); while(rs.next) { out.println( "&lt;tr>" ); out.println( "&lt;td>&lt;A HREF=" + rs.getString("url") + " target=\"_blank\" >" + rs.getString("description") + "&lt;/A>&lt;/td>" ); out.println( "&lt;td>" ); out.println( "&lt;table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">" ); out.println( "&lt;tr>" ); out.println( "&lt;td>" ); out.println( "&lt;form action=\"PortalView.jsp\" name=\"view\">" ); out.println( "&lt;input name=\"weblink_id\" type=\"hidden\" value=\"" + rs.getString("id") + "\">" ); out.println( "&lt;input name=\"weblink_url\" type=\"hidden\" value=\"" + rs.getString("url") + "\">" ); out.println( "&lt;input name=\"btnG\" type=\"submit\" value=\"View Link\">" ); out.println( "&lt;/form>" ); out.println( "&lt;/td>" ); out.println( "&lt;td>" ); out.println( "&lt;form action=\"PortalEdit.jsp\" name=\"edit\">" ); out.println( "&lt;input name=\"weblink_id\" type=\"hidden\" value=\"" + rs.getString("id") + "\">" ); out.println( "&lt;input name=\"weblink_url\" type=\"hidden\" value=\"" + rs.getString("url") + "\">" ); out.println( "&lt;input name=\"btnG\" type=\"submit\" value=\"Edit Link\">" ); out.println( "&lt;/form>" ); out.println( "&lt;/td>" ); out.println( "&lt;td>" ); out.println( "&lt;form action=\"PortalRemove.jsp\" name=\"remove\">" ); out.println( "&lt;input name=\"weblink_id\" type=\"hidden\" value=\"" + rs.getString("id") + "\">" ); out.println( "&lt;input name=\"weblink_url\" type=\"hidden\" value=\"" + rs.getString("url") + "\">" ); out.println( "&lt;input name=\"btnG\" type=\"submit\" value=\"Remove Link\">" ); out.println( "&lt;/form>" ); out.println( "&lt;/td>" ); out.println( "&lt;/tr>" ); out.println( "&lt;/table>" ); out.println( "&lt;/td>" ); out.println( "&lt;/tr>" ); } %> &lt;/tbody>&lt;/table> &lt;% } // end try catch ( SQLException ex ) { out.println ( "&lt;pre>" ); out.println ( "---" ); out.println ( "--                      FATAL ERROR IN SQL" ); out.println ( "---" ); out.println ( "-- " ); while ( ex != null ) { out.println ("-- SQLState: " + ex.getSQLState ); out.println ("-- Message: " + ex.getMessage ); out.println ("-- Vendor:  " + ex.getErrorCode ); ex = ex.getNextException ; out.println ( "-- " ); } 		out.println ( "---" ); } // end catch catch ( java.lang.Exception ex ) { /* 		 * Got some other type of exception. Dump it. */ 		out.println ( "&lt;pre>" ); out.println ( "---" ); ex.printStackTrace ; out.println("exception: " + ex.getMessage ); } // end catch finally { // No action for now } // end finally %> &lt;br>&lt;br>&lt;br>&lt;br> &lt;/body> &lt;/html>

You may have noticed a few buttons on the snapshot of the main page: “Add” (near the top), “View”, “Edit”, “Remove” (on the right). Of course, you have to be able to maintain your list–an empty list is, well, too short, and a long list of out dated items is more of a stroll down memory lane than a purposeful tool. My “Portal” started out empty or port-less, so I created an “Add New Link” page. Here is a screen shot of the "Add New Web Link" page.



Here is the code.

&lt;%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.io.*" import="java.sql.*" import="java.util.*" import="java.util.Properties" errorPage="" %> &lt;%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> &lt;% /* * PortalAdd.jsp * * Copyright (c) 2013 Alfred J Wheeler * All rights reserved. * * This software is the proprietary * information of Alfred J Wheeler * */ /**  * The purpose of this application is to add a record to table WebLink * * USAGE *  PortalAdd.jsp * * @author      7/10/2013 created by Alfred J Wheeler. * * @version     0.1 * * @see         Portal, PortalView, PortalEdit, PortalRemove * */ /*  * Database : demonstration, Table : weblink *  * field name                        data type * ---  ---  * id                                integer * type                             character varying(55) * url                              character varying(255) * description                      character varying(255) */ /* * Initialize some variables */ long dt = 0; String title = null; Connection conn = null; Statement st = null; ResultSet rs = null; String sql = ""; String posted = "false"; boolean populated = true; boolean duplicate = false; int records = 0; String message = ""; int errors = 0; //String tableName = null; String weblinkType = ""; String weblinkURL = ""; String weblinkDescription = ""; /* * Get a date time stamp. Set a page title, if needed... */ dt = System.currentTimeMillis; title = "Portal - Add New Web Link"; /* * Get the database connection values, etc... from the properties file * ( This eventually will be externalized to reduce code clutter... ) */ String p = application.getRealPath("/") + "properties/Demo.properties"; InputStream propsFile; Properties appProp = new Properties; try { propsFile = new FileInputStream(p); appProp.load(propsFile); propsFile.close; } catch (IOException ioe) { out.println("&lt;PRE>"); out.println("I/O Exception."); ioe.printStackTrace; out.println("&lt;/PRE>"); } String dbDriver = "" + appProp.getProperty("dbDriver"); String dbUrl   = "" + appProp.getProperty("dbUrl"); String dbUid   = "" + appProp.getProperty("dbUid"); String dbUpw   = "" + appProp.getProperty("dbUpw"); String css     = "" + appProp.getProperty("css"); //out.println("&lt;PRE>"); //out.println(dbDriver); //out.println(dbUrl); //out.println(dbUid); //out.println(dbUpw); //out.println("&lt;/PRE>"); /* * Open the database */ try { Class.forName(dbDriver); conn = DriverManager.getConnection(dbUrl, dbUid, dbUpw); st = conn.createStatement; /* 	 * Get the request values */ 	if ( request.getParameter( "posted" ) == null ) { posted = "false"; } 	else { posted = request.getParameter( "posted" ); } 	if ( posted.equals("true") ) { /* 		 * Are the fields populated? */ 		weblinkType = request.getParameter( "weblink_type" ); weblinkURL = request.getParameter( "weblink_url" ); weblinkDescription = request.getParameter( "weblink_description" ); populated = true; if ( (request.getParameter( "weblink_description" )).equals( "" ) ) { populated = false; message += "Blank description not allowed. Please enter some description.\n"; errors++; } 		if ( (request.getParameter( "weblink_url" )).equals( "" ) ) { populated = false; message += "Blank url not allowed. Please enter some url.\n"; errors++; } 		/* 		 * Does record exist already? */ 		duplicate = false; if ( populated ) { sql = "SELECT COUNT(*) AS records " + "FROM " + "weblink" + " " + // It may make sense to use a variable here at some point... "WHERE description = '" + request.getParameter( "weblink_description" ) + "' " + "AND url = '" + request.getParameter( "weblink_url" ) + "' " ; //			out.print("sql = " + sql + " \n"); rs = st.executeQuery(sql); if (rs.next) { records = Integer.parseInt( rs.getString("records") ); if ( records > 0 ) { duplicate = true; message += "Record already exists. Duplicate records not allowed. Please check your entries.\n"; errors++; } 			} 			sql = "SELECT COUNT(*) AS records " + "FROM " + "weblink" + " " + // It may make sense to use a variable here at some point... "WHERE description = '" + request.getParameter( "weblink_description" ) + "' " ; //			out.print("sql = " + sql + " \n"); rs = st.executeQuery(sql); if (rs.next) { records = Integer.parseInt( rs.getString("records") ); if ( records > 0 ) { duplicate = true; message += "Record with description already exists. Duplicate descriptions not allowed. Please check your entries.\n"; errors++; } 			} 			sql = "SELECT COUNT(*) AS records " + "FROM " + "weblink" + " " + // It may make sense to use a variable here at some point... "WHERE url = '" + request.getParameter( "weblink_url" ) + "' " ; //			out.print("sql = " + sql + " \n"); rs = st.executeQuery(sql); if (rs.next) { records = Integer.parseInt( rs.getString("records") ); if ( records > 0 ) { duplicate = true; message += "Record with URL already exists. Duplicate URLs not allowed. Please check your entries.\n"; errors++; } 			} 		} 		/* 		 * If no error, save data, clear fields, display form */ 		/* 		 * Save data... */ 		if ( errors == 0 ) { sql = "INSERT INTO " + "weblink" + " " + // It may make sense to use a variable for the tableName here at some point... "SELECT " + "(SELECT COALESCE(MAX(id), 0) + 1 FROM weblink) AS id, " + "'" + request.getParameter( "weblink_type" ) + "' AS type, " + "'" + request.getParameter( "weblink_url" ) + "' AS url, " + "'" + request.getParameter( "weblink_description" ) + "' AS description " ; //			out.print("sql = " + sql + " \n"); st.execute(sql); } 		/* 		 * Clear fields... */ 		weblinkType = ""; weblinkURL = ""; weblinkDescription = ""; /* 		 * If error, display form as is with the message produced by the preceding code... 		 */ } %> &lt;!DOCTYPE html> &lt;HTML> &lt;HEAD> &lt;link rel="stylesheet" type="text/css" href="&lt;%=css%>"> &lt;TITLE>&lt;%=title%>&lt;/TITLE> &lt;SCRIPT LANGUAGE="JavaScript" SRC="angular.js">&lt;/SCRIPT> &lt;SCRIPT LANGUAGE="JavaScript" SRC="jquery.js">&lt;/SCRIPT> &lt;/HEAD> &lt;BODY> &lt;H1>&lt;%=title%>&lt;/H1> &lt;br> &lt;div align="center"> &lt;nobr> &lt;div class="centered"> &lt;nobr> &lt;form action="Portal.jsp" name=f> &lt;input name=btnG type=submit value=" Return to Portal "> &lt;/form> &lt;/nobr> &lt;/div> &lt;br> &lt;p class="centered">&lt;%=message%>&lt;/p> &lt;FORM NAME="form1" METHOD="POST"> &lt;input type="hidden" name="dt" value="&lt;%=System.currentTimeMillis%>"> &lt;input type="hidden" name="posted" value="true"> &lt;input type="hidden" name="tableName" value="weblink"> &lt;table class="ivory" align="center" xwidth="80%" border="0" cellspacing="0" cellpadding="16"> &lt;TBODY> &lt;tr> &lt;td>&lt;b>Type:&lt;/B>&lt;/td>&lt;td> &lt;select name="weblink_type"> &lt;option value ="website" selected>Website&lt;/option> &lt;option value ="page">Page&lt;/option> &lt;option value ="localhost">Localhost Application&lt;/option> &lt;/select> &lt;/td> &lt;/tr> &lt;tr> &lt;td>&lt;b>URL:&lt;/B>&lt;/td>&lt;td>&lt;input type="text" name="weblink_url" size="127" value="">&lt;/td> &lt;/tr> &lt;tr> &lt;td>&lt;b>Description:&lt;/B>&lt;/td>&lt;td>&lt;input type="text" name="weblink_description" size="127" value="">&lt;/td> &lt;/tr> &lt;tr> &lt;td> &lt;/td>&lt;td> &lt;div > &lt;input type="submit" name="submit" value="Submit"> &lt;input type="reset" name="clear" value="Reset"> &lt;/div> &lt;/td> &lt;/tr> &lt;/table> &lt;br>&lt;br> &lt;/form> &lt;/nobr> &lt;/div> &lt;% } // end try catch ( SQLException ex ) { out.println ( "&lt;pre>" ); out.println ( "---" ); out.println ( "--                      FATAL ERROR IN SQL" ); out.println ( "---" ); out.println ( "-- " ); while ( ex != null ) { out.println ("-- SQLState: " + ex.getSQLState ); out.println ("-- Message: " + ex.getMessage ); out.println ("-- Vendor:  " + ex.getErrorCode ); ex = ex.getNextException ; out.println ( "-- " ); } 	out.println ( "---" ); } // end catch catch ( java.lang.Exception ex ) { /* 	 * Got some other type of exception. Dump it. */ 	out.println ( "&lt;pre>" ); out.println ( "---" ); ex.printStackTrace ; out.println("exception: " + ex.getMessage ); } // end catch finally { // No action for now } // end finally %> &lt;/BODY> &lt;/HTML>

Sometimes we just feel the need to look at a record, so I created a "View Web Link" record page. Here is a screen shot.



Here is the code.

&lt;%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.io.*" import="java.sql.*" import="java.util.*" import="java.util.Properties" errorPage="" %> &lt;%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> &lt;% /* * PortalView.jsp * * Copyright (c) 2013 Alfred Wheeler * All rights reserved. * * This software is the proprietary * information of Alfred Wheeler * */ /**  * The purpose of this application is to view a record on table WebLink * * USAGE *  PortalView.jsp * * @author      7/18/2013 created by Alfred Wheeler. * * @version     0.1 * * @see         Portal, PortalAdd, PortalEdit, PortalRemove * */ /*  * Database : demonstration, Table : weblink *  * field name                        data type * ---  ---  * id                                integer * type                             character varying(55) * url                              character varying(255) * description                      character varying(255) */ /* * Initialize some variables */ String message = ""; long dt = 0; String title = null; Connection conn = null; Statement st = null; ResultSet rs = null; String sql = ""; //String posted = "false"; //boolean populated = true; //boolean recordExists = true; //boolean duplicate = false; //boolean updated = false; //int records = 0; //int errors = 0; //String tableName = null; String weblinkId = ""; //String s = null; // miscellaneous utility variable /* * Get a date time stamp. Set a page title, if needed... */ dt = System.currentTimeMillis; title = "Portal - View Web Link"; /* * Get the database connection values, etc... from the properties file * ( This eventually will be externalized to reduce code clutter... ) */ String p = application.getRealPath("/") + "properties/Demo.properties"; InputStream propsFile; Properties appProp = new Properties; try { propsFile = new FileInputStream(p); appProp.load(propsFile); propsFile.close; } catch (IOException ioe) { out.println("&lt;PRE>"); out.println("I/O Exception."); ioe.printStackTrace; out.println("&lt;/PRE>"); } String dbDriver = "" + appProp.getProperty("dbDriver"); String dbUrl   = "" + appProp.getProperty("dbUrl"); String dbUid   = "" + appProp.getProperty("dbUid"); String dbUpw   = "" + appProp.getProperty("dbUpw"); String css     = "" + appProp.getProperty("css"); //out.println("&lt;PRE>"); //out.println(dbDriver); //out.println(dbUrl); //out.println(dbUid); //out.println(dbUpw); //out.println("&lt;/PRE>"); %> &lt;!DOCTYPE html> &lt;HTML> &lt;HEAD> &lt;link rel="stylesheet" type="text/css" href="&lt;%=css%>"> &lt;TITLE>&lt;%=title%>&lt;/TITLE> &lt;SCRIPT LANGUAGE="JavaScript" SRC="angular.js">&lt;/SCRIPT> &lt;SCRIPT LANGUAGE="JavaScript" SRC="jquery.js">&lt;/SCRIPT> &lt;/HEAD> &lt;BODY> &lt;H1>&lt;%=title%>&lt;/H1> &lt;br>&lt;br>&lt;br>&lt;br> &lt;div class="centered"> &lt;nobr> &lt;form action="Portal.jsp?&lt;%=System.currentTimeMillis%>" name=f> &lt;input name=btnG type=submit value=" Return to Portal "> &lt;/form> &lt;/nobr> &lt;/div> &lt;br> &lt;% /* * Open the database */ try { Class.forName(dbDriver); conn = DriverManager.getConnection(dbUrl, dbUid, dbUpw); st = conn.createStatement; /* 	 * Get request values */ 	weblinkId = request.getParameter( "weblink_id" ); sql = "SELECT " + "type AS weblink_type, " + "url AS weblink_url, " + "description AS weblink_description " + "FROM weblink " + "WHERE id = " + weblinkId ; //	out.print("sql = " + sql + " \n"); rs = st.executeQuery(sql); if (rs.next) { %> &lt;div class="centered"> &lt;FORM NAME="form1" METHOD="POST" ACTION="Portal.jsp"> &lt;input type="hidden" name="dt" value="&lt;%=System.currentTimeMillis%>"> &lt;table border="0" cellspacing="0"> &lt;TBODY> &lt;tr>&lt;th>Field&lt;/th>&lt;th>Value&lt;/th>&lt;/tr> &lt;tr align="left"> &lt;td>&lt;b>Type:&lt;/B>&lt;/td>&lt;td>&lt;%= rs.getString("weblink_type")%>&lt;/td> &lt;/tr> &lt;tr align="left"> &lt;td>&lt;b>URL:&lt;/B>&lt;/td>&lt;td>&lt;%= rs.getString("weblink_url")%>&lt;/td> &lt;/tr> &lt;tr align="left"> &lt;td>&lt;b>Description:&lt;/B>&lt;/td>&lt;td>&lt;%= rs.getString("weblink_description")%>&lt;/td> &lt;/tr> &lt;tr align="left"> &lt;td> &lt;/td>&lt;td> &lt;div > &lt;input type="submit" name="submit" value=" Okay "> &lt;/div> &lt;/td> &lt;/tr> &lt;/table> &lt;br>&lt;br> &lt;/form> &lt;/div> &lt;% } // end if for result set handling } // end try catch ( SQLException ex ) { %> &lt;BODY> &lt;H1>&lt;%=title%>&lt;/H1> &lt;br> &lt;div > &lt;form name="form1" method="POST" action="Portal.jsp?&lt;%=dt%>"> &lt;input type="submit" name="submit" value="Continue"> &lt;/form> &lt;/div> &lt;% out.println ( "&lt;pre>" ); out.println ( "---" ); out.println ( "--                      FATAL ERROR IN SQL" ); out.println ( "---" ); out.println ( "-- " ); while ( ex != null ) { out.println ("-- SQLState: " + ex.getSQLState ); out.println ("-- Message: " + ex.getMessage ); out.println ("-- Vendor:  " + ex.getErrorCode ); ex = ex.getNextException ; out.println ( "-- " ); } 	out.println ( "---" ); } // end catch catch ( java.lang.Exception ex ) { %> &lt;BODY> &lt;H1>&lt;%=title%>&lt;/H1> &lt;br> &lt;div > &lt;form name="form1" method="POST" action="Portal.jsp?&lt;%=dt%>"> &lt;input type="submit" name="submit" value="Continue"> &lt;/form> &lt;/div> &lt;% /* 	 * Got some other type of exception. Dump it. */ 	out.println ( "&lt;pre>" ); out.println ( "---" ); ex.printStackTrace ; out.println("exception: " + ex.getMessage ); } // end catch finally { // No action for now } // end finally %> &lt;/body> &lt;/html>

Looking at our handywork is all fine and good, but having created content, we invariably type something incorrectly and want to fix it, hence the invention of the “Edit Web Link” page! Here is a screen shot.



Here is the code.

&lt;%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.io.*" import="java.sql.*" import="java.util.*" import="java.util.Properties" errorPage="" %> &lt;%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> &lt;% /* * PortalEdit.jsp * * Copyright (c) 2013 Alfred Wheeler * All rights reserved. * * This software is the proprietary * information of Alfred Wheeler * */ /**  * The purpose of this application is to edit a record of table WebLink * * USAGE *  PortalEdit.jsp * * @author      7/18/2013 created by Alfred Wheeler. * * @version     0.1 * * @see         Portal, PortalAdd, PortalView, PortalRemove * */ /*  * Database : demonstration, Table : weblink *  * field name                        data type * ---  ---  * id                                integer * type                             character varying(55) * url                              character varying(255) * description                      character varying(255) */ /* * Initialize some variables */ String message = ""; long dt = 0; String title = null; Connection conn = null; Statement st = null; ResultSet rs = null; String sql = ""; String posted = "false"; boolean populated = true; boolean recordExists = true; boolean duplicate = false; boolean updated = false; int records = 0; int errors = 0; //String tableName = null; String weblinkId = ""; String editWebLinkType = ""; String editWebLinkURL = ""; String editWebLinkDescription = ""; String s = null; // miscellaneous utility variable /* * Get a date time stamp. Set a page title, if needed... */ dt = System.currentTimeMillis; title = "Portal - Edit Web Link"; /* * Get the database connection values, etc... from the properties file * ( This should be externalized to reduce code clutter... ) */ String p = application.getRealPath("/") + "properties/Demo.properties"; InputStream propsFile; Properties appProp = new Properties; try { propsFile = new FileInputStream(p); appProp.load(propsFile); propsFile.close; } catch (IOException ioe) { out.println("&lt;PRE>"); out.println("I/O Exception."); ioe.printStackTrace; out.println("&lt;/PRE>"); } String dbDriver = "" + appProp.getProperty("dbDriver"); String dbUrl   = "" + appProp.getProperty("dbUrl"); String dbUid   = "" + appProp.getProperty("dbUid"); String dbUpw   = "" + appProp.getProperty("dbUpw"); String css     = "" + appProp.getProperty("css"); //out.println("&lt;PRE>"); //out.println(dbDriver); //out.println(dbUrl); //out.println(dbUid); //out.println(dbUpw); //out.println("&lt;/PRE>"); /* * Open the database */ try { Class.forName(dbDriver); conn = DriverManager.getConnection(dbUrl, dbUid, dbUpw); st = conn.createStatement; /* 	 * Get request values */ 	weblinkId = request.getParameter( "weblink_id" ); editWebLinkType = request.getParameter( "editWebLinkType" ); editWebLinkURL = request.getParameter( "editWebLinkURL" ); editWebLinkDescription = request.getParameter( "editWebLinkDescription" ); if ( request.getParameter( "posted" ) == null ) { posted = "false"; } 	else { posted = request.getParameter( "posted" ); } 	if ( posted.equals("true") ) { /* 		 * 1. Check that the entries are not all blanked out (we don't want to erase any records...) * 2. Check that the entries are valid for their kind (a moot point, for this particular application...) * 3. Check that the significant (primary key) changes do not duplicate any other record... * 3. Make sure the record exists and, ideally, lock it, during the update * 4. Apply the change(s) * 5. Return to list */ 		/* 		 * Are the fields populated? */ 		populated = true; if ( editWebLinkDescription.equals( "" ) ) { if ( editWebLinkURL.equals( "" ) ) { populated = false; message += "Blank record not allowed. Please enter some values.\n"; errors++; } 		} 		if ( editWebLinkDescription.equals( "" ) ) { message += "Description is required. Please enter something.\n"; errors++; } 		if ( editWebLinkURL.equals( "" ) ) { message += "URL is required. Please enter something.\n"; errors++; } 		/* 		 * Does record "still" exist? */ 		recordExists = true; if ( populated ) { sql = "SELECT COUNT(*) AS records " + "FROM weblink " + "WHERE id = " + weblinkId ; //			out.print("sql = " + sql + " \n"); rs = st.executeQuery(sql); if (rs.next) { records = Integer.parseInt( rs.getString("records") ); if ( records &lt; 1 ) { recordExists = false; message += "Record does not exist. You may have removed by some other means or another page.\n"; errors++; } 			} 		} 		/* 		 * Would the change cause an "exact" duplicate? */ 		duplicate = false; if ( populated ) { sql = "SELECT COUNT(*) AS records " + "FROM weblink " + "WHERE description = '" + editWebLinkDescription + "' " + "AND url = '" + editWebLinkURL + "' " + "AND id != " + weblinkId ; //			out.print("sql = " + sql + " \n"); rs = st.executeQuery(sql); if (rs.next) { records = Integer.parseInt( rs.getString("records") ); if ( records > 0 ) { duplicate = true; message += "Record already exists. Duplicate records not allowed. Please check your entries.\n"; errors++; } 			} 		} 		/* 		 * If no error, save data, clear fields, display form */ 		/* 		 * Save data... */ 		if ( errors == 0 ) { // This should be a call to a stored procedure... sql = "UPDATE weblink SET " + "type = '" + editWebLinkType + "', " + "url = '" + editWebLinkURL + "', " + "description = '" + editWebLinkDescription + "' " + "WHERE id = " + weblinkId ; //			out.print("sql = " + sql + " \n"); st.execute(sql); /* 			 * Return to list.... */ %> &lt;HTML> &lt;HEAD>&lt;TITLE>&lt;/TITLE>&lt;/HEAD> &lt;BODY onload="document.form1.submit"> &lt;div> &lt;form name="form1" method="POST" action="Portal.jsp?&lt;%=System.currentTimeMillis%>"> &lt;input type="image" src="images/dots/clear.gif" alt="submit" value="Continue"> &lt;/form> &lt;/div> &lt;% updated = true; } 	} 	else { // This is the first time here, so get the values from the database table /* 		 * Note: Must work out automating getting (or just editing) only the * fields that are relevant, e.g. the timestamps are generally not that * relevant to data entry persons */ 		sql = "SELECT " + "type AS weblink_type, " + "url AS weblink_url, " + "description AS weblink_description " + "FROM weblink " + "WHERE id = " + weblinkId ; //		out.print("sql = " + sql + " \n"); rs = st.executeQuery(sql); if (rs.next) { editWebLinkType = rs.getString("weblink_type"); editWebLinkURL = rs.getString("weblink_url"); editWebLinkDescription = rs.getString("weblink_description"); } 	} 	if ( updated == false ) { %> &lt;!DOCTYPE html> &lt;HTML> &lt;HEAD> &lt;link rel="stylesheet" type="text/css" href="&lt;%=css%>"> &lt;TITLE>&lt;%=title%>&lt;/TITLE> &lt;SCRIPT LANGUAGE="JavaScript" SRC="angular.js">&lt;/SCRIPT> &lt;SCRIPT LANGUAGE="JavaScript" SRC="jquery.js">&lt;/SCRIPT> &lt;/HEAD> &lt;BODY> &lt;H1>&lt;%=title%>&lt;/H1> &lt;br>&lt;br>&lt;br>&lt;br> &lt;div class="centered"> &lt;nobr> &lt;form action="Portal.jsp" name=f> &lt;input name=btnG type=submit value=" Return to Portal "> &lt;/form> &lt;/nobr> &lt;/div> &lt;br> &lt;p class="centered">&lt;%=message%>&lt;/p> &lt;div class="centered"> &lt;FORM NAME="form1" METHOD="POST"> &lt;input type="hidden" name="dt" value="&lt;%=System.currentTimeMillis%>"> &lt;input type="hidden" name="posted" value="true"> &lt;input type="hidden" name="tableName" value="weblink"> &lt;input type="hidden" name="weblink_id" value="&lt;%=weblinkId%>"> &lt;table border="1"> &lt;TBODY> &lt;tr align="left"> &lt;td>&lt;b>Type:&lt;/B>&lt;/td>&lt;td> &lt;% s = "&lt;select name=\"editWebLinkType\">"; s += " &lt;option value=\"website\""; if (editWebLinkType.equals( "website" )) { s += " selected"; } 	s += ">Website&lt;/option>"; s += " &lt;option value =\"page\""; if (editWebLinkType.equals( "page" )) { s += " selected"; } 	s += ">Page&lt;/option>"; s += " &lt;option value =\"localhost\""; if (editWebLinkType.equals( "localhost" )) { s += " selected"; } 	s += ">Localhost Application&lt;/option>"; s += "&lt;/select>"; %> &lt;%= s%> &lt;/td> &lt;/tr> &lt;tr align="left"> &lt;td>&lt;b>URL:&lt;/B>&lt;/td>&lt;td>&lt;input type="text" name="editWebLinkURL" size="127" value="&lt;%= editWebLinkURL%>">&lt;/td> &lt;/tr> &lt;tr align="left"> &lt;td>&lt;b>Description:&lt;/B>&lt;/td>&lt;td>&lt;input type="text" name="editWebLinkDescription" size="127" value="&lt;%= editWebLinkDescription%>">&lt;/td> &lt;/tr> &lt;tr align="left"> &lt;td> &lt;/td>&lt;td> &lt;div > &lt;input type="submit" name="submit" value="Submit"> &lt;input type="reset" name="clear" value="Reset"> &lt;/div> &lt;/td> &lt;/tr> &lt;/table> &lt;br>&lt;br> &lt;/form> &lt;/div> &lt;% } // end if for result set handling } // end try catch ( SQLException ex ) { out.println ( "&lt;pre>" ); out.println ( "---" ); out.println ( "--                      FATAL ERROR IN SQL" ); out.println ( "---" ); out.println ( "-- " ); while ( ex != null ) { out.println ("-- SQLState: " + ex.getSQLState ); out.println ("-- Message: " + ex.getMessage ); out.println ("-- Vendor:  " + ex.getErrorCode ); ex = ex.getNextException ; out.println ( "-- " ); } 	out.println ( "---" ); } // end catch catch ( java.lang.Exception ex ) { /* 	 * Got some other type of exception. Dump it. */ 	out.println ( "&lt;pre>" ); out.println ( "---" ); ex.printStackTrace ; out.println("exception: " + ex.getMessage ); } // end catch finally { // No action for now } // end finally %> &lt;/body> &lt;/html>

So, we can look at our list, look at individual records, add items to the list and fix typing mistakes. Sometimes we want to get rid of a record without any confusion about recycling it in some sense. What we need to accomplish this is a Delete Record page. Conveniently, this page is very similar to the View Record page, but it is best not to browse records using a “delete record” tool… I’m sure way too many people learned it the hard way… Now, let’s see a picture and some code!



Once again, here is the code.

&lt;%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.io.*" import="java.sql.*" import="java.util.*" import="java.util.Properties" errorPage="" %> &lt;%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> &lt;% /* * PortalRemove.jsp * * Copyright (c) 2013 Alfred Wheeler * All rights reserved. * * This software is the proprietary * information of Alfred Wheeler * */ /**  * The purpose of this application is to remove a record from table WebLink * * USAGE *  PortalRemove.jsp * * @author      7/18/2013 created by Alfred Wheeler. * * @version     0.1 * * @see         Portal, PortalAdd, PortalView, PortalEdit * */ /*  * Database : demonstration, Table : weblink *  * field name                        data type * ---  ---  * id                                integer * type                             character varying(55) * url                              character varying(255) * description                      character varying(255) */ /* * Initialize some variables */ String message = ""; long dt = 0; String title = null; Connection conn = null; Statement st = null; ResultSet rs = null; String sql = ""; String posted = "false"; boolean populated = true; boolean recordExists = true; boolean duplicate = false; boolean updated = false; int records = 0; int errors = 0; //String tableName = null; String weblinkId = ""; String s = null; // miscellaneous utility variable /* * Get a date time stamp. Set a page title, if needed... */ dt = System.currentTimeMillis; title = "Portal - Remove Web Link"; /* * Get the database connection values, etc... from the properties file * ( This eventually will be externalized to reduce code clutter... ) */ String p = application.getRealPath("/") + "properties/Demo.properties"; InputStream propsFile; Properties appProp = new Properties; try { propsFile = new FileInputStream(p); appProp.load(propsFile); propsFile.close; } catch (IOException ioe) { out.println("&lt;PRE>"); out.println("I/O Exception."); ioe.printStackTrace; out.println("&lt;/PRE>"); } String dbDriver = "" + appProp.getProperty("dbDriver"); String dbUrl   = "" + appProp.getProperty("dbUrl"); String dbUid   = "" + appProp.getProperty("dbUid"); String dbUpw   = "" + appProp.getProperty("dbUpw"); String css     = "" + appProp.getProperty("css"); //out.println("&lt;PRE>"); //out.println(dbDriver); //out.println(dbUrl); //out.println(dbUid); //out.println(dbUpw); //out.println("&lt;/PRE>"); /* * Open the database */ try { Class.forName(dbDriver); conn = DriverManager.getConnection(dbUrl, dbUid, dbUpw); st = conn.createStatement; /* 	 * Get request values */ 	weblinkId = request.getParameter( "weblink_id" ); /* 	 * Get the request values */ 	if ( request.getParameter( "posted" ) == null ) { posted = "false"; } 	else { posted = request.getParameter( "posted" ); } 	if ( posted.equals("true") ) { /* 		 * 1. Make sure the record exists and, ideally, lock it, during the removal * 2. Remove the record * 3. Return to list */ 		/* 		 * Does record "still" exist? */ 		recordExists = true; if ( populated ) { sql = "SELECT COUNT(*) AS records " + "FROM weblink " + "WHERE id = " + weblinkId ; //			out.print("sql = " + sql + " \n"); rs = st.executeQuery(sql); if (rs.next) { records = Integer.parseInt( rs.getString("records") ); if ( records &lt; 1 ) { recordExists = false; message += "Record does not exist. You may have removed by some other means or another page.\n"; errors++; } 			} 		} 		/* 		 * If no error, delete the record and then return to list */ 		if ( errors == 0 ) { sql = "DELETE FROM weblink " + "WHERE id = " + weblinkId ; //			out.println( "sql = " + sql); st.execute( sql ); %> &lt;BODY onload="document.form1.submit"> &lt;div> &lt;form name="form1" method="POST" action="Portal.jsp?&lt;%=System.currentTimeMillis%>"> &lt;input type="image" src="images/dots/clear.gif" alt="submit" value="Continue"> &lt;/form> &lt;/div> &lt;% } 	} 	/* 	 * If we got this far, then either we arrived from another page or there was some error... */ %> &lt;!DOCTYPE html> &lt;HTML> &lt;HEAD> &lt;link rel="stylesheet" type="text/css" href="&lt;%=css%>"> &lt;TITLE>&lt;%=title%>&lt;/TITLE> &lt;SCRIPT LANGUAGE="JavaScript" SRC="angular.js">&lt;/SCRIPT> &lt;SCRIPT LANGUAGE="JavaScript" SRC="jquery.js">&lt;/SCRIPT> &lt;/HEAD> &lt;BODY> &lt;H1>&lt;%=title%>&lt;/H1> &lt;br>&lt;br>&lt;br>&lt;br> &lt;div class="centered"> &lt;nobr> &lt;form action="Portal.jsp?&lt;%=System.currentTimeMillis%>" name=f> &lt;input name=btnG type=submit value=" Return to Portal "> &lt;/form> &lt;/nobr> &lt;/div> &lt;br> &lt;p class="centered">&lt;%=message%>&lt;/p> &lt;% sql = "SELECT " + // consider adding an id field to possibly simplify things... "type AS weblink_type, " + "url AS weblink_url, " + "description AS weblink_description " + "FROM weblink " + "WHERE id = " + weblinkId ; //		out.print("sql = " + sql + " \n"); rs = st.executeQuery(sql); if (rs.next) { %> &lt;div class="centered"> &lt;FORM NAME="form1" METHOD="POST"> &lt;input type="hidden" name="dt" value="&lt;%=System.currentTimeMillis%>"> &lt;input type="hidden" name="posted" value="true"> &lt;input type="hidden" name="tableName" value="weblink"> &lt;input type="hidden" name="weblink_id" value="&lt;%=weblinkId%>"> &lt;table border="0" cellspacing="0"> &lt;TBODY> &lt;tr>&lt;th>Field&lt;/th>&lt;th>Value&lt;/th>&lt;/tr> &lt;tr align="left"> &lt;td>&lt;b>Type:&lt;/B>&lt;/td>&lt;td>&lt;%= rs.getString("weblink_type")%>&lt;/td> &lt;/tr> &lt;tr align="left"> &lt;td>&lt;b>URL:&lt;/B>&lt;/td>&lt;td>&lt;%= rs.getString("weblink_url")%>&lt;/td> &lt;/tr> &lt;tr align="left"> &lt;td>&lt;b>Description:&lt;/B>&lt;/td>&lt;td>&lt;%= rs.getString("weblink_description")%>&lt;/td> &lt;/tr> &lt;tr align="left"> &lt;td> &lt;/td>&lt;td> &lt;div > &lt;input type="submit" name="submit" value=" Confirm "> &lt;/div> &lt;/td> &lt;/tr> &lt;/table> &lt;br>&lt;br> &lt;/form> &lt;/div> &lt;% } // end if for result set handling } // end try catch ( SQLException ex ) { out.println ( "&lt;pre>" ); out.println ( "---" ); out.println ( "--                      FATAL ERROR IN SQL" ); out.println ( "---" ); out.println ( "-- " ); while ( ex != null ) { out.println ("-- SQLState: " + ex.getSQLState ); out.println ("-- Message: " + ex.getMessage ); out.println ("-- Vendor:  " + ex.getErrorCode ); ex = ex.getNextException ; out.println ( "-- " ); } 		out.println ( "---" ); } // end catch catch ( java.lang.Exception ex ) { /* 		 * Got some other type of exception. Dump it. */ 		out.println ( "&lt;pre>" ); out.println ( "---" ); ex.printStackTrace ; out.println("exception: " + ex.getMessage ); } // end catch finally { // No action for now } // end finally %> &lt;/body> &lt;/html>

That’s it for this example! I’ll probably tidy it up and beautify it, but it's useful as is.