Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5.  
  6.  
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import javax.servlet.ServletException;
  10. import javax.servlet.http.*;
  11. //import javax.servlet.http.HttpServletRequest;
  12. //import javax.servlet.http.HttpServletResponse;
  13. import java.sql.*;
  14. import java.util.ArrayList;
  15.  
  16.  
  17. /**
  18. *
  19. * @author Owner
  20. */
  21. public class Update_Servlet extends HttpServlet {
  22.  
  23. /**
  24. * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
  25. * @param request servlet request
  26. * @param response servlet response
  27. * @throws ServletException if a servlet-specific error occurs
  28. * @throws IOException if an I/O error occurs
  29. */
  30. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  31. throws ServletException, IOException {
  32. response.setContentType("text/html;charset=UTF-8");
  33. PrintWriter out = response.getWriter();
  34. HttpSession mySession = request.getSession();
  35. ArrayList uNames = (ArrayList)mySession.getAttribute("uNames");
  36. String UName = request.getParameter("username");
  37. String PWord = request.getParameter("password");
  38. String submit = request.getParameter("cmdSubmit");
  39. String origUName="";
  40. String error = null;
  41. boolean update = false;
  42. if(submit != null) //user clicked submit button
  43. {
  44. //retrieve session variable
  45. origUName = (String) mySession.getAttribute("origUName");
  46. if(UName.trim().length() > 0 && PWord.trim().length() > 0)
  47. {
  48. if(UName.matches("^[A-za-z0-9]+$") && PWord.matches("^[A-Za-z0-9]+$"))
  49. {
  50.  
  51. if(uNames != null)
  52. {
  53. if(!UName.equals(origUName))
  54. {
  55. if(!uNames.contains(UName))
  56. {
  57. update = true;
  58. }
  59. else
  60. {
  61. error = "Primary key violation...("+ UName + ")...Enter new username!";
  62. }
  63. }
  64. else
  65. {
  66. update = true;
  67. }
  68. }
  69. else
  70. {
  71. error = "Cookes must be enabled";
  72. }
  73. }
  74.  
  75. else
  76. {
  77. //display error message
  78. error = "Fields contain invalid characters!.. re enter data!";
  79. }
  80. }
  81. else
  82. {
  83. //display error message
  84. error = "Fields must be completed!";
  85. }
  86. }
  87. else // if else is true then the page is loaded for the first time...capture original primary key value
  88. {
  89. // create session variable
  90. mySession.setAttribute("origUName", UName);
  91. }
  92. if(update)
  93. {
  94. String dbURL = "jdbc:odbc:dbJDBC1";
  95. String sql = "UPDATE tUsers SET username = '" + UName + "',password ='" + PWord + "' WHERE username = '" + origUName + "'";
  96. try
  97. {
  98. Connection conn = DriverManager.getConnection(dbURL);
  99. Statement s = conn.createStatement();
  100. s.executeUpdate(sql);
  101. s.close();
  102. conn.close();
  103. response.sendRedirect("Admin_Servlet");
  104. }
  105. catch(SQLException ex)
  106. {
  107. error = ex.getMessage();
  108. }
  109.  
  110.  
  111. }
  112. try {
  113.  
  114. out.println("<html>");
  115. out.println("<head>");
  116. out.println("<title>Servlet Update_Servlet</title>");
  117. out.println("</head>");
  118. out.println("<body style = 'font-family: Verdana, Arial, Sans-Serif'>");
  119. out.println("<h2 align='center'>Update_Servlet</h2>");
  120. if(error != null)
  121. {
  122. out.println("<p align='center' style='color:red'>" + error + "</p>");
  123. }
  124. out.println("<form action='Update_Servlet' method='post'>");
  125. out.println("<table align='center'>");
  126. out.println("<tr>");
  127. out.println("<td align='right'>Username:<input type='text' name='username' value='" + UName +"'/><br /></td>");
  128. out.println("</tr>");
  129. out.println("<tr>");
  130. out.println("<td align='right'>Password:<input type='text' name='password' value='" + PWord +"'/><br /></td>");
  131. out.println("</tr>");
  132. out.println ("<tr>");
  133. out.println("<td align='center' colspan='2'><input type='submit' value='Update Record' name='cmdSubmit' /></td>");
  134. out.println("</tr>");
  135. out.println("</table>");
  136. out.println("</form>");
  137. out.println("</body>");
  138. out.println("</html>");
  139.  
  140. } finally {
  141. out.close();
  142. }
  143. }
  144.  
  145. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  146. /**
  147. * Handles the HTTP <code>GET</code> method.
  148. * @param request servlet request
  149. * @param response servlet response
  150. * @throws ServletException if a servlet-specific error occurs
  151. * @throws IOException if an I/O error occurs
  152. */
  153. @Override
  154. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  155. throws ServletException, IOException {
  156. processRequest(request, response);
  157. }
  158.  
  159. /**
  160. * Handles the HTTP <code>POST</code> method.
  161. * @param request servlet request
  162. * @param response servlet response
  163. * @throws ServletException if a servlet-specific error occurs
  164. * @throws IOException if an I/O error occurs
  165. */
  166. @Override
  167. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  168. throws ServletException, IOException {
  169. processRequest(request, response);
  170. }
  171.  
  172. /**
  173. * Returns a short description of the servlet.
  174. * @return a String containing servlet description
  175. */
  176. @Override
  177. public String getServletInfo() {
  178. return "Short description";
  179. }// </editor-fold>
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement