Index.
html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!--
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></title>
<meta http-equiv="Content-Type" content="text/html; charset-UTF-8">
</head>
<body>
<h1> DVD Library Application</h1>
<ul>
<li> <a href='list_library.view'> Display my DVD library</a></li>
<li> <a href='add_dvd.html'> Add a DVD to my collection</a></li>
</ul>
</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>
[Link]
/*
* 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 [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
/**
*
* @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 {
[Link]("text/html;charset=UTF-8");
try (PrintWriter out = [Link]()) {
/* TODO output your page here. You may use following sample code.
*/
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet AddDVDServlet</title>");
[Link]("</head>");
[Link]("<body>");
[Link]("<h1>Servlet AddDVDServlet at " +
[Link]() + "</h1>");
[Link]("</body>");
[Link]("</html>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click
on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</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 doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* 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 = [Link]("title");
String year = [Link]("year");
//didi the user type in a genre?
String genre = [Link]("newGenre");
//if not, use the drop-down list value
if(genre == null || ([Link]().length()==0)){
genre = [Link]("genre");
}
//Business logic
DVDItem item = new DVDItem(title, year, genre);
try (PrintWriter out = [Link]()) {
[Link]("SUCCESS: added DVD titled '" + [Link]()+
"'");
}
}catch(RuntimeException e){
try (PrintWriter out = [Link]()) {
[Link]("Error: " + [Link]());
}
}
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
[Link]
/*
* 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 [Link];
import [Link];
/**
*
* @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 [Link];
}
public String getYear(){
return [Link];
}
public String getGenre(){
return [Link];
}
}
[Link]
/*
* 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 [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
/**
*
* @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();
[Link](new DVDItem("Close Encounters of the Third Kind", "1976",
"Sci-Fi"));
[Link](new DVDItem("Star Wars", "1977", "Sci-Fi"));
[Link](new DVDItem("Mission to Mars", "2000", "Sci-Fi"));
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<html>");
[Link]("<head>");
[Link]("<title>ListLibraryServlet</title>");
[Link]("<body bgcolor='white'>");
[Link]("You currently have <b>" +[Link]() +"</b> DVDs in your
collection:<br>");
[Link]("<table border='0' cellspacing-'0' cellpadding='5'>");
[Link]("<tr>");
[Link](" <th>TITLE</th>");
[Link](" <th>YEAR</th>");
[Link](" <th>GENRE</th>");
[Link]("</th>");
Iterator it = [Link]();
while([Link]()){
DVDItem item = (DVDItem) [Link]();
[Link]("<tr>");
[Link](" <td>" +[Link]() +"</td>");
[Link](" <td>" +[Link]() +"</td>");
[Link](" <td>" +[Link]() +"</td>");
[Link]("</tr>");
}
[Link]("</table>");
[Link]("End of list...");
[Link]("</body>");
[Link]("</html>");
[Link]();
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click
on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</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 doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* 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>