DVDLibrary
DVDLibrary
DVDLibrary
html
</body>
</html>
Add_dvd.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>DVD Library Application: Add DVD Form</title>
<!--<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
-->
</head>
<body>
<h1>Add DVD</h1>
<form action="add_dvd.do" method="POST">
Title: <input type="text" name="title"> <br/> <br/>
Year: <input type="text" name="year"> <br/> <br/>
Genre: <select name="genre">
<option value="Sci-Fi"> Sci-Fi </option>
<option value="Drama"> Drama </option>
<option value="Comedy"> Comedy </option>
</select>
or new genre: <input type="text" name="newGenre"> <br/> <br/>
<input type='submit'>
</form>
</body>
</html>
AddDVDServlet.java
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.dvd.controller;
import com.dvd.model.DVDItem;
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;
/**
*
* @author USER
*/
public class AddDVDServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code.
*/
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet AddDVDServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet AddDVDServlet at " +
request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
//processRequest(request, response);
try{
//retrieve from parameterss
String title = request.getParameter("title");
String year = request.getParameter("year");
//didi the user type in a genre?
String genre = request.getParameter("newGenre");
//if not, use the drop-down list value
if(genre == null || (genre.trim().length()==0)){
genre = request.getParameter("genre");
}
//Business logic
DVDItem item = new DVDItem(title, year, genre);
try (PrintWriter out = response.getWriter()) {
out.println("SUCCESS: added DVD titled '" + item.getTitle()+
"'");
}
}catch(RuntimeException e){
try (PrintWriter out = response.getWriter()) {
out.println("Error: " + e.getMessage());
}
}
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
DVDItem.java
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.dvd.model;
import java.io.Serializable;
/**
*
* @author USER
*/
public class DVDItem implements Serializable{
String title;
String year;
String genre;
//contructor
public DVDItem(String ti, String Years, String gen){
title = ti;
year = Years;
genre = gen;
}
public String getTitle(){
return this.title;
}
public String getYear(){
return this.year;
}
public String getGenre(){
return this.genre;
}
}
ListLibraryServlet.java
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.dvd.view;
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;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import com.dvd.model.DVDItem;
/**
*
* @author USER
*/
public class ListLibraryServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
*@param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
List dvds = new ArrayList();
dvds.add(new DVDItem("Close Encounters of the Third Kind", "1976",
"Sci-Fi"));
dvds.add(new DVDItem("Star Wars", "1977", "Sci-Fi"));
dvds.add(new DVDItem("Mission to Mars", "2000", "Sci-Fi"));
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>ListLibraryServlet</title>");
out.println("<body bgcolor='white'>");
out.println("You currently have <b>" +dvds.size() +"</b> DVDs in your
collection:<br>");
out.println("<table border='0' cellspacing-'0' cellpadding='5'>");
out.println("<tr>");
out.println(" <th>TITLE</th>");
out.println(" <th>YEAR</th>");
out.println(" <th>GENRE</th>");
out.println("</th>");
Iterator it = dvds.iterator();
while(it.hasNext()){
DVDItem item = (DVDItem) it.next();
out.println("<tr>");
out.println(" <td>" +item.getTitle() +"</td>");
out.println(" <td>" +item.getYear() +"</td>");
out.println(" <td>" +item.getGenre() +"</td>");
out.println("</tr>");
}
out.println("</table>");
out.println("End of list...");
out.println("</body>");
out.println("</html>");
out.close();
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>