Created
November 1, 2016 14:35
-
-
Save darbyluv2code/282fe4e82117fb813916a2a34398dcb5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Add Student</title> | |
<link type="text/css" rel="stylesheet" href="css/style.css"> | |
<link type="text/css" rel="stylesheet" href="css/add-student-style.css"> | |
</head> | |
<body> | |
<div id="wrapper"> | |
<div id="header"> | |
<h2>FooBar University</h2> | |
</div> | |
</div> | |
<div id="container"> | |
<h3>Add Student</h3> | |
<form action="StudentControllerServlet" method="POST"> | |
<input type="hidden" name="command" value="ADD" /> | |
<table> | |
<tbody> | |
<tr> | |
<td><label>First name:</label></td> | |
<td><input type="text" name="firstName" /></td> | |
</tr> | |
<tr> | |
<td><label>Last name:</label></td> | |
<td><input type="text" name="lastName" /></td> | |
</tr> | |
<tr> | |
<td><label>Email:</label></td> | |
<td><input type="text" name="email" /></td> | |
</tr> | |
<tr> | |
<td><label></label></td> | |
<td><input type="submit" value="Save" class="save" /></td> | |
</tr> | |
</tbody> | |
</table> | |
</form> | |
<div style="clear: both;"></div> | |
<p> | |
<a href="StudentControllerServlet">Back to List</a> | |
</p> | |
</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.luv2code.web.jdbc; | |
import java.io.IOException; | |
import java.util.List; | |
import javax.annotation.Resource; | |
import javax.servlet.RequestDispatcher; | |
import javax.servlet.ServletException; | |
import javax.servlet.annotation.WebServlet; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import javax.sql.DataSource; | |
/** | |
* Servlet implementation class StudentControllerServlet | |
*/ | |
@WebServlet("/StudentControllerServlet") | |
public class StudentControllerServlet extends HttpServlet { | |
private static final long serialVersionUID = 1L; | |
private StudentDbUtil studentDbUtil; | |
@Resource(name="jdbc/web_student_tracker") | |
private DataSource dataSource; | |
@Override | |
public void init() throws ServletException { | |
super.init(); | |
// create our student db util ... and pass in the conn pool / datasource | |
try { | |
studentDbUtil = new StudentDbUtil(dataSource); | |
} | |
catch (Exception exc) { | |
throw new ServletException(exc); | |
} | |
} | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
try { | |
// read the "command" parameter | |
String theCommand = request.getParameter("command"); | |
// if the command is missing, then default to listing students | |
if (theCommand == null) { | |
theCommand = "LIST"; | |
} | |
// route to the appropriate method | |
switch (theCommand) { | |
case "LIST": | |
listStudents(request, response); | |
break; | |
case "LOAD": | |
loadStudent(request, response); | |
break; | |
case "UPDATE": | |
updateStudent(request, response); | |
break; | |
case "DELETE": | |
deleteStudent(request, response); | |
break; | |
default: | |
listStudents(request, response); | |
} | |
} | |
catch (Exception exc) { | |
throw new ServletException(exc); | |
} | |
} | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
try { | |
// read the "command" parameter | |
String theCommand = request.getParameter("command"); | |
// route to the appropriate method | |
switch (theCommand) { | |
case "ADD": | |
addStudent(request, response); | |
break; | |
default: | |
listStudents(request, response); | |
} | |
} | |
catch (Exception exc) { | |
throw new ServletException(exc); | |
} | |
} | |
private void deleteStudent(HttpServletRequest request, HttpServletResponse response) | |
throws Exception { | |
// read student id from form data | |
String theStudentId = request.getParameter("studentId"); | |
// delete student from database | |
studentDbUtil.deleteStudent(theStudentId); | |
// send them back to "list students" page | |
listStudents(request, response); | |
} | |
private void updateStudent(HttpServletRequest request, HttpServletResponse response) | |
throws Exception { | |
// read student info from form data | |
int id = Integer.parseInt(request.getParameter("studentId")); | |
String firstName = request.getParameter("firstName"); | |
String lastName = request.getParameter("lastName"); | |
String email = request.getParameter("email"); | |
// create a new student object | |
Student theStudent = new Student(id, firstName, lastName, email); | |
// perform update on database | |
studentDbUtil.updateStudent(theStudent); | |
// send them back to the "list students" page | |
listStudents(request, response); | |
} | |
private void loadStudent(HttpServletRequest request, HttpServletResponse response) | |
throws Exception { | |
// read student id from form data | |
String theStudentId = request.getParameter("studentId"); | |
// get student from database (db util) | |
Student theStudent = studentDbUtil.getStudent(theStudentId); | |
// place student in the request attribute | |
request.setAttribute("THE_STUDENT", theStudent); | |
// send to jsp page: update-student-form.jsp | |
RequestDispatcher dispatcher = | |
request.getRequestDispatcher("/update-student-form.jsp"); | |
dispatcher.forward(request, response); | |
} | |
private void addStudent(HttpServletRequest request, HttpServletResponse response) throws Exception { | |
// read student info from form data | |
String firstName = request.getParameter("firstName"); | |
String lastName = request.getParameter("lastName"); | |
String email = request.getParameter("email"); | |
// create a new student object | |
Student theStudent = new Student(firstName, lastName, email); | |
// add the student to the database | |
studentDbUtil.addStudent(theStudent); | |
// send back to main page (the student list) | |
// SEND AS REDIRECT to avoid multiple-browser reload issue | |
response.sendRedirect(request.getContextPath() + "/StudentControllerServlet?command=LIST"); | |
} | |
private void listStudents(HttpServletRequest request, HttpServletResponse response) | |
throws Exception { | |
// get students from db util | |
List<Student> students = studentDbUtil.getStudents(); | |
// add students to the request | |
request.setAttribute("STUDENT_LIST", students); | |
// send to JSP page (view) | |
RequestDispatcher dispatcher = request.getRequestDispatcher("/list-students.jsp"); | |
dispatcher.forward(request, response); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment