Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 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.http.*;
  10. import javax.servlet.ServletException;
  11. import java.util.ArrayList;
  12. //import javax.servlet.http.HttpServlet;
  13. //import javax.servlet.http.HttpServletRequest;
  14. //import javax.servlet.http.HttpServletResponse;
  15. import java.sql.*;
  16.  
  17. /**
  18. *
  19. * @author Owner
  20. */
  21. public class Insert_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("txtUsername");
  37. String PWord = request.getParameter("txtPassword");
  38. String error = null;
  39.  
  40. if(UName != null && PWord != null)
  41. {
  42. if(UName.trim().length() > 0 && PWord.trim().length() > 0)
  43. {
  44.  
  45. if(UName.matches("^[A-za-z0-9]+$") && PWord.matches("^[A-Za-z0-9]+$"))
  46. {
  47. if(uNames !=null)
  48. {
  49. if(!uNames.contains(UName))
  50. {
  51. // write record to database
  52. String dbURL = "jdbc:odbc:dbJDBC1";
  53. String sql = "INSERT INTO tUsers (username,password) VALUES('" + UName + "','" + PWord + "')";
  54.  
  55. try
  56. {
  57. Connection conn = DriverManager.getConnection(dbURL);
  58. Statement s = conn.createStatement();
  59. s.executeUpdate(sql);
  60. s.close();
  61. conn.close();
  62. response.sendRedirect("Admin_Servlet");
  63.  
  64.  
  65. }
  66. catch(SQLException e)
  67. {
  68.  
  69. }
  70. }
  71. else
  72. {
  73. error = "Primary key violation....("+ UName + ")...Enter new username!";
  74. }
  75. }
  76. else
  77. {
  78. error ="Cookies need to be enabled!";
  79. }
  80.  
  81. }
  82. else
  83. {
  84. error = "Fields contain invalid characters!";
  85. }
  86. }
  87. else
  88. {
  89. error="Fields must be completed!";
  90. }
  91. }
  92. try {
  93.  
  94. out.println("<html>");
  95. out.println("<head>");
  96. out.println("<title>Servlet Insert_Servlet</title>");
  97. out.println("</head>");
  98. out.println("<body style = 'font-family: Verdana, Arial, Sans-Serif'>");
  99. out.println("<h2 align='center'>Insert_Servlet</h2>");
  100. if(error != null)
  101. {
  102. out.println("<p>" + error + "</p>");
  103. }
  104. out.println("<form action='Insert_Servlet' method='post'>");
  105. out.println("<table align='center'>");
  106. out.println("<tr>");
  107. out.println("<td align='right'>Username:<input type='text' name='txtUsername' /></td>");
  108. out.println("</tr>");
  109. out.println("<tr>");
  110. out.println("<td align='right'>Password:<input type='text' name='txtPassword' /></td>");
  111. out.println("</tr>");
  112. out.println("<tr>");
  113. out.println("<td align='center' colspan='2'><input type='submit' value='Write Record' /></td>");
  114. out.println("</tr>");
  115. out.println("</table>");
  116. out.println("</form>");
  117. out.println("</body>");
  118. out.println("</html>");
  119.  
  120.  
  121. } finally {
  122. out.close();
  123. }
  124. }
  125.  
  126. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  127. /**
  128. * Handles the HTTP <code>GET</code> method.
  129. * @param request servlet request
  130. * @param response servlet response
  131. * @throws ServletException if a servlet-specific error occurs
  132. * @throws IOException if an I/O error occurs
  133. */
  134. @Override
  135. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  136. throws ServletException, IOException {
  137. processRequest(request, response);
  138. }
  139.  
  140. /**
  141. * Handles the HTTP <code>POST</code> method.
  142. * @param request servlet request
  143. * @param response servlet response
  144. * @throws ServletException if a servlet-specific error occurs
  145. * @throws IOException if an I/O error occurs
  146. */
  147. @Override
  148. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  149. throws ServletException, IOException {
  150. processRequest(request, response);
  151. }
  152.  
  153. /**
  154. * Returns a short description of the servlet.
  155. * @return a String containing servlet description
  156. */
  157. @Override
  158. public String getServletInfo() {
  159. return "Short description";
  160. }// </editor-fold>
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement