Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package Moduler;
  7.  
  8. import DB.DataBase;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.PrintWriter;
  12. import java.sql.Connection;
  13. import java.sql.PreparedStatement;
  14. import java.sql.SQLException;
  15. import javax.servlet.ServletException;
  16. import javax.servlet.annotation.MultipartConfig;
  17. import javax.servlet.annotation.WebServlet;
  18. import javax.servlet.http.HttpServlet;
  19. import javax.servlet.http.HttpServletRequest;
  20. import javax.servlet.http.HttpServletResponse;
  21. import javax.servlet.http.Part;
  22.  
  23. /**
  24. *
  25. * @author nilsf
  26. */
  27. @WebServlet("/DeliverModuleStudent")
  28. @MultipartConfig(maxFileSize = 16177215) // upload file's size up to 16MB
  29. public class DeliverModuleStudent extends HttpServlet {
  30.  
  31. /**
  32. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  33. * methods.
  34. *
  35. * @param request servlet request
  36. * @param response servlet response
  37. * @throws ServletException if a servlet-specific error occurs
  38. * @throws IOException if an I/O error occurs
  39. */
  40.  
  41. DataBase db = new DataBase();
  42. Connection con = null;
  43. PreparedStatement pst = null;
  44. String message = null; // message will be sent back to client
  45.  
  46. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  47. throws ServletException, IOException {
  48. response.setContentType("text/html;charset=UTF-8");
  49. String email = request.getParameter("email");
  50. String modulID = request.getParameter("modulID");
  51.  
  52. InputStream inputStream = null; // input stream of the upload file
  53.  
  54. // obtains the upload file part in this multipart request
  55. Part filePart = request.getPart("file");
  56. if (filePart != null) {
  57.  
  58.  
  59. // obtains input stream of the upload file
  60. inputStream = filePart.getInputStream();
  61. }
  62.  
  63. try (PrintWriter out = response.getWriter()) {
  64.  
  65. con = db.getCon();
  66.  
  67. // constructs SQL statement
  68. String sql = "INSERT INTO Innlevering (email, modulID, innleveringFil) values (?, ?, ?)";
  69. pst = con.prepareStatement(sql);
  70. pst.setString(1, email);
  71. pst.setString(2, modulID);
  72.  
  73.  
  74. if (inputStream != null) {
  75. // fetches input stream of the upload file for the blob column
  76. pst.setBlob(3, inputStream);
  77. }
  78.  
  79.  
  80. // sends the statement to the database server
  81. int row = pst.executeUpdate();
  82. if (row > 0) {
  83. message = "File uploaded and saved into database";
  84. out.println(message);
  85. }
  86.  
  87.  
  88. } catch (SQLException ex) {
  89. message = "ERROR: " + ex.getMessage();
  90. ex.printStackTrace();
  91. } finally {
  92. if (con != null) {
  93. // closes the database connection
  94. try {
  95. con.close();
  96. } catch (SQLException ex) {
  97. ex.printStackTrace();
  98. }
  99. }
  100. // sets the message in request scope
  101. request.setAttribute("Message", message);
  102.  
  103. }
  104. }
  105.  
  106.  
  107.  
  108. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  109. /**
  110. * Handles the HTTP <code>GET</code> method.
  111. *
  112. * @param request servlet request
  113. * @param response servlet response
  114. * @throws ServletException if a servlet-specific error occurs
  115. * @throws IOException if an I/O error occurs
  116. */
  117. @Override
  118. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  119.  
  120. throws ServletException, IOException {
  121. processRequest(request, response);
  122. }
  123.  
  124. /**
  125. * Handles the HTTP <code>POST</code> method.
  126. *
  127. * @param request servlet request
  128. * @param response servlet response
  129. * @throws ServletException if a servlet-specific error occurs
  130. * @throws IOException if an I/O error occurs
  131. */
  132. @Override
  133. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  134. throws ServletException, IOException {
  135. processRequest(request, response);
  136. }
  137.  
  138. /**
  139. * Returns a short description of the servlet.
  140. *
  141. * @return a String containing servlet description
  142. */
  143. @Override
  144. public String getServletInfo() {
  145. return "Short description";
  146. }// </editor-fold>
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement