Skip to content

Instantly share code, notes, and snippets.

@darbyluv2code
Created June 20, 2017 12:01
Show Gist options
  • Select an option

  • Save darbyluv2code/a257feb34d20ded1a5a41d3771f7d5a1 to your computer and use it in GitHub Desktop.

Select an option

Save darbyluv2code/a257feb34d20ded1a5a41d3771f7d5a1 to your computer and use it in GitHub Desktop.
student-tracker app bug fixes
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 {
// TODO Auto-generated method stub
super.init();
try{
studentDbUtil = new StudentDbUtil(dataSource);
}
catch(Exception ex){
throw new ServletException(ex);
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String theCommand = request.getParameter("command");
// if the command is missing, then default to listing students
if (theCommand == null) {
theCommand = "LIST";
}
switch (theCommand) {
case "LIST":
listStudents(request,response);
break;
case "ADD":
try {
addStudents(request,response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case "LOAD":
try {
loadStudents(request,response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case "UPDATE":
try {
updateStudents(request,response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
default:
listStudents(request,response);
break;
}
}
private void updateStudents(HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
int id = Integer.parseInt(request.getParameter("studentId"));
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String email = request.getParameter("email");
Student theStudent = new Student(id, firstName, lastName, email);
studentDbUtil.updateStudent(theStudent);
listStudents(request, response);
}
private void loadStudents(HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
String theStudentId = request.getParameter("studentId");
Student theStudent = studentDbUtil.getStudent(theStudentId);
request.setAttribute("THE_STUDENT", theStudent);
RequestDispatcher theDispatcher = request.getRequestDispatcher("/update-student-form.jsp");
theDispatcher.forward(request, response);
}
private void addStudents(HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String email = request.getParameter("email");
Student theStudent = new Student(firstName, lastName, email);
studentDbUtil.addStudent(theStudent);
listStudents(request, response);
}
private void listStudents(HttpServletRequest request, HttpServletResponse response) {
try {
List<Student> students = studentDbUtil.getStudents();
System.out.println("students:" + students);
request.setAttribute("STUDENT_LIST", students);
RequestDispatcher dispatcher = request.getRequestDispatcher("/list-students.jsp");
dispatcher.forward(request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>web-student-tracker</display-name>
<welcome-file-list>
<welcome-file>StudentControllerServlet</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment