Create JSP pages and a servlet that will allow students to save course selection
ID: 3931346 • Letter: C
Question
Create JSP pages and a servlet that will allow students to save course selections during their session. All output data values may be hard-coded (no database or files required).
I have the following code but it is incorrect.
public class NewServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = response.getSession();
String url = "";
Boolean check1=false, check2=false, check3=false;
String action = response.getParameter("action");
if (action.equals("new")) {
url = "/courseO.jsp";
}
else if (action.equals("previous")) {
url = "/courseS.jsp";
if (response.getParameter("check1") != null)
{
check1 = true;
session.setAttribute("check1", check1);
}
if (response.getParameter("check2") != null)
{
check2 = true;
session.setAttribute("check2", check2);
}
if (response.getParameter("check3") != null)
{
check3 = true;
session.setAttribute("check3", check3);
}
}
// perform action and set URL to appropriate page
if (action.equals("previous")) {
url = "/courseS.jsp"; // the "index" page
}
else if (action.equals("new")) {
url = "/courseO.jsp";
}
// forward to the view
getServletContext()
.getRequestDispatcher(url)
.forward(request, response);
}
}
Explanation / Answer
********doPost Method*********************
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// gets values of text fields
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String cource = request.getParameter("cource");
String gender = request.getParameter("gender");
InputStream inputStream = null; // input stream of the upload file
// obtains the upload file part in this multipart request
if (filePart != null) {
// prints out some information for debugging
System.out.println(filePart.getName());
System.out.println(filePart.getSize());
System.out.println(filePart.getContentType());
// obtains input stream of the upload file
inputStream = filePart.getInputStream();
}
Connection conn = null; // connection to the database
String message = null; // message will be sent back to client
try {
// connects to the database
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
// constructs SQL statement
String sql = "INSERT INTO contacts (first_name, last_name,cource, gender) values (?, ?, ?, ?)";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, firstName);
statement.setString(2, lastName);
statement.setString(3, cource);
statement.setString(4, gender);
if (inputStream != null) {
// fetches input stream of the upload file for the blob column
statement.setBlob(3, inputStream);
}
// sends the statement to the database server
int row = statement.executeUpdate();
if (row > 0) {
message = "Information saved into database";
}
} catch (SQLException ex) {
message = "ERROR: " + ex.getMessage();
ex.printStackTrace();
} finally {
if (conn != null) {
// closes the database connection
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
// sets the message in request scope
request.setAttribute("Message", message);
// forwards to the message page
getServletContext().getRequestDispatcher("/Message.jsp").forward(request, response);
}
}
}