Lösung zu Blatt 7

Servlet

package blatt7;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GuestbookServlet extends HttpServlet{

	Guestbook db;

	 public GuestbookServlet() throws Exception{
		super();
		db = new Guestbook();

		//db.addEntry("me", "A great Site. <br /> The best invention sice sliced bread!");
	}

	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");

		PrintWriter writer = response.getWriter();
		processForm(request);
		writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
		writer.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
		writer.println("<html>");
		writer.println("<head><title>Guestbook</title></head>");
		writer.println("<body>");
		writer.println("<h1>Guestbook</h1>");
		printForm(writer);
		printGuestbook(writer);
		writer.println("</body>");
		writer.println("</html>");
	}


	private void printGuestbook(PrintWriter writer) {
		if(!db.isEmpty()){
			for(GuestbookEntry e : db.getEntries()){

				writer.println("<p><div  style=\"font-weight:bold\">" + e.getAuthor() + "</div> wrote on "
				+ e.getDateString() + ":</p>");
				writer.println("<p>" + e.getMessage() + "</p>");

			}	
		}
	}

	private void processForm(HttpServletRequest request){
		String name = request.getParameter("name");
		String msg = request.getParameter("message");

		if(name != null && msg != null) db.addEntry(name, msg);
	}

	private void printForm(PrintWriter writer){

		writer.println("<form method=\"GET\" action=\"/blatt7/guestbook\">");
		writer.println("Name: <input type=\"text\" name=\"name\" /> <br />");
		writer.println("Message: <input type=\"text\" name=\"message\" />");
		writer.println("<input type=\"submit\" value=\"Submit\" />");
		writer.println("</form>");
	}
}

Klasse zur Verwaltung der Einträge

package blatt7;

import java.sql.SQLException;
import java.util.List;

public class Guestbook {
	List<GuestbookEntry> entryList;
	GuestbookDatabase db;

	public Guestbook() throws Exception{
		db = new GuestbookDatabase();
		entryList = db.getEntries();
	}

	public void addEntry(String anAuthor, String aMessage){
		GuestbookEntry entry = new GuestbookEntry(anAuthor, aMessage);
	        entryList.add(entry);

		try {
			db.saveEntry(entry);
		} catch (SQLException e) {
			e.printStackTrace();
		}	
	}

	public List<GuestbookEntry> getEntries(){
		return entryList;
	}

	public boolean isEmpty(){
		return entryList.isEmpty();
	}
}

Klasse für einen Gästebucheintrag


package blatt7;

import java.sql.Date;
import java.text.SimpleDateFormat;

public class GuestbookEntry {
	private String author;
	private String message;
        private Date datum;

	public GuestbookEntry(String anAuthor, String aMessage){
		author = anAuthor;
		message = aMessage;
                datum = new Date(System.currentTimeMillis());
	}

        public GuestbookEntry(String anAuthor, String aMessage, Date aDate){
		author = anAuthor;
		message = aMessage;
		datum = aDate;
	}


	public String getAuthor() {
		return author;
	}

	public String getMessage() {
		return message;
	}

        public String getDateString(){
		SimpleDateFormat s = new SimpleDateFormat();
		return s.format(datum); 
	}
	
	public Date getDate(){
		return datum;
	}
}

Datenbank-Zugriff

package blatt7;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.util.LinkedList;
import java.util.List;

public class GuestbookDatabase {

	private static String driver = "com.mysql.jdbc.Driver";
	private static String protocol = "jdbc:mysql:";
	private String dbName = "db";  
	private String port = "11190"; 
	private String user = "sep";
	private String pwd = "geheim"; 

	private Connection conn = null;
	private Statement s;

	public GuestbookDatabase() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
		Class.forName(driver).newInstance();
		String url = protocol + "//turing:"+port + "/"+ dbName +"?user="+user+"&password="+pwd;
		
		conn = DriverManager.getConnection(url);
		s = conn.createStatement();
	}

	

	public void saveEntry(GuestbookEntry entry) throws SQLException{
		String query = "INSERT INTO guestbook VALUES('" + entry.getAuthor() + "', '"+ entry.getMessage()+ "', '" + entry.getDate().toString() + "')";

		s.execute(query);
	}

	

	public List<GuestbookEntry> getEntries() throws SQLException{
		List<GuestbookEntry> entries = new LinkedList<GuestbookEntry>();
		ResultSet rs = s.executeQuery("SELECT * FROM guestbook");

		while(rs.next()){
			entries.add(new GuestbookEntry(rs.getString(1), rs.getString(2), rs.getDate(3)));
		}
		return entries;
	}

	public void stop() throws SQLException{
		s.close();
        	conn.close();
	}
}