Guest User

db_servlet

a guest
Mar 17th, 2016
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.72 KB | None | 0 0
  1. index.html -
  2.  
  3.  
  4. <!DOCTYPE html>
  5. <!--
  6. To change this license header, choose License Headers in Project Properties.
  7. To change this template file, choose Tools | Templates
  8. and open the template in the editor.
  9. -->
  10. <html>
  11. <head>
  12. <title>TODO supply a title</title>
  13. <meta charset="UTF-8">
  14. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  15. </head>
  16. <body>
  17. <div>Login</div>
  18. <form action="login" method="POST">
  19. username:-<input type="text" name="uname"><br><br>
  20. password:-<input type="text" name="pass"><br>
  21. <input type="submit">
  22. </form>
  23. <br><br>
  24. <a href="registerhtml.html">Sign Up</a>
  25. </body>
  26. </html>
  27. ----------------------
  28.  
  29. registerhtml.html -
  30.  
  31. <!DOCTYPE html>
  32. <!--
  33. To change this license header, choose License Headers in Project Properties.
  34. To change this template file, choose Tools | Templates
  35. and open the template in the editor.
  36. -->
  37. <html>
  38. <head>
  39. <title>TODO supply a title</title>
  40. <meta charset="UTF-8">
  41. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  42. </head>
  43. <body>
  44. <div>Register</div>
  45. <form action="register" method="POST">
  46. username:-<input type="text" name="username"><br><br>
  47. password:-<input type="text" name="password"><br><br>
  48. gender:- <input type="text" name="gender"><br><br>
  49. branch:-<input type="text" name="branch"><br><br>
  50. <input type="submit">
  51.  
  52. </form>
  53.  
  54. </body>
  55. </html>
  56.  
  57. ------------------
  58.  
  59. dbconnection.java -
  60.  
  61.  
  62. /*
  63. * To change this license header, choose License Headers in Project Properties.
  64. * To change this template file, choose Tools | Templates
  65. * and open the template in the editor.
  66. */
  67. package db;
  68.  
  69.  
  70. import java.sql.Connection;
  71. import java.sql.DriverManager;
  72. import java.sql.ResultSet;
  73. import java.sql.SQLException;
  74. import java.sql.Statement;
  75. import java.util.logging.Level;
  76. import java.util.logging.Logger;
  77.  
  78. /**
  79. *
  80. * @author JNEC-03
  81. */
  82. public class dbconnection {
  83. Connection conn;
  84. Statement stmt;
  85. ResultSet rs;
  86.  
  87. public dbconnection(){
  88. String dburl="jdbc:derby://localhost:1527/warlock";
  89. try {
  90. Class.forName("com.mysql.jdbc.Driver");
  91. } catch (ClassNotFoundException ex) {
  92. Logger.getLogger(dbconnection.class.getName()).log(Level.SEVERE, null, ex);
  93. }
  94. try {
  95. conn = DriverManager.getConnection(dburl,"root","toor");
  96. stmt =conn.createStatement();
  97. } catch (SQLException ex) {
  98. Logger.getLogger(dbconnection.class.getName()).log(Level.SEVERE, null, ex);
  99. }
  100.  
  101. }
  102.  
  103.  
  104. public boolean check(String un,String pwd)
  105. {
  106.  
  107. try {
  108. String query="select * from ROOT.STUDENT where username='"+un+"' and password='"+pwd+"'";
  109. rs=stmt.executeQuery(query);
  110. if(rs.next())
  111. {
  112. return true;
  113. }
  114. else{
  115. return false;
  116. }
  117. } catch (SQLException ex) {
  118. Logger.getLogger(dbconnection.class.getName()).log(Level.SEVERE, null, ex);
  119. }
  120. return false;
  121.  
  122. }
  123.  
  124. public int insert(String u,String p,String g,String b){
  125. try{
  126. String query="insert into ROOT.STUDENT values ('"+u+"', '"+p+"', '"+g+"', '"+b+"')";
  127. System.out.print(query);
  128. int count = stmt.executeUpdate(query);
  129. return count;
  130. }
  131. catch(Exception ex){
  132.  
  133. }
  134. return 0;
  135.  
  136. }
  137. }
  138.  
  139. ------------------------------------
  140.  
  141. login.java -
  142.  
  143. /*
  144. * To change this license header, choose License Headers in Project Properties.
  145. * To change this template file, choose Tools | Templates
  146. * and open the template in the editor.
  147. */
  148. package db;
  149.  
  150. import java.io.IOException;
  151. import java.io.PrintWriter;
  152. import javax.servlet.ServletException;
  153. import javax.servlet.annotation.WebServlet;
  154. import javax.servlet.http.HttpServlet;
  155. import javax.servlet.http.HttpServletRequest;
  156. import javax.servlet.http.HttpServletResponse;
  157.  
  158. /**
  159. *
  160. * @author JNEC-03
  161. */
  162. @WebServlet(name = "login", urlPatterns = {"/login"})
  163. public class login extends HttpServlet {
  164.  
  165. /**
  166. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  167. * methods.
  168. *
  169. * @param request servlet request
  170. * @param response servlet response
  171. * @throws ServletException if a servlet-specific error occurs
  172. * @throws IOException if an I/O error occurs
  173. */
  174. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  175. throws ServletException, IOException {
  176. response.setContentType("text/html;charset=UTF-8");
  177. String username = request.getParameter("uname");
  178. String password = request.getParameter("pass");
  179. try (PrintWriter out = response.getWriter()) {
  180. /* TODO output your page here. You may use following sample code. */
  181. out.println("<!DOCTYPE html>");
  182. out.println("<html>");
  183. out.println("<head>");
  184. out.println("<title>Servlet login</title>");
  185. out.println("</head>");
  186. out.println("<body>");
  187. out.println("<h1>Servlet login at " + request.getContextPath() + "</h1>");
  188. dbconnection db= new dbconnection();
  189. boolean flag = db.check(username, password);
  190. if(flag)
  191. {
  192. out.println("login successful");
  193. }
  194. else{
  195. out.println("login unsuccessful");
  196. }
  197. out.println("</body>");
  198. out.println("</html>");
  199. }
  200. }
  201.  
  202. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  203. /**
  204. * Handles the HTTP <code>GET</code> method.
  205. *
  206. * @param request servlet request
  207. * @param response servlet response
  208. * @throws ServletException if a servlet-specific error occurs
  209. * @throws IOException if an I/O error occurs
  210. */
  211. @Override
  212. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  213. throws ServletException, IOException {
  214. processRequest(request, response);
  215. }
  216.  
  217. /**
  218. * Handles the HTTP <code>POST</code> method.
  219. *
  220. * @param request servlet request
  221. * @param response servlet response
  222. * @throws ServletException if a servlet-specific error occurs
  223. * @throws IOException if an I/O error occurs
  224. */
  225. @Override
  226. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  227. throws ServletException, IOException {
  228. processRequest(request, response);
  229. }
  230.  
  231. /**
  232. * Returns a short description of the servlet.
  233. *
  234. * @return a String containing servlet description
  235. */
  236. @Override
  237. public String getServletInfo() {
  238. return "Short description";
  239. }// </editor-fold>
  240.  
  241. }
  242.  
  243.  
  244. -----------------------
  245.  
  246.  
  247. register.java -
  248.  
  249.  
  250. /*
  251. * To change this license header, choose License Headers in Project Properties.
  252. * To change this template file, choose Tools | Templates
  253. * and open the template in the editor.
  254. */
  255. package db;
  256.  
  257. import java.io.IOException;
  258. import java.io.PrintWriter;
  259. import javax.servlet.ServletException;
  260. import javax.servlet.annotation.WebServlet;
  261. import javax.servlet.http.HttpServlet;
  262. import javax.servlet.http.HttpServletRequest;
  263. import javax.servlet.http.HttpServletResponse;
  264.  
  265. /**
  266. *
  267. * @author JNEC-03
  268. */
  269. @WebServlet(name = "register", urlPatterns = {"/register"})
  270. public class register extends HttpServlet {
  271.  
  272. /**
  273. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  274. * methods.
  275. *
  276. * @param request servlet request
  277. * @param response servlet response
  278. * @throws ServletException if a servlet-specific error occurs
  279. * @throws IOException if an I/O error occurs
  280. */
  281. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  282. throws ServletException, IOException {
  283. response.setContentType("text/html;charset=UTF-8");
  284. String u = request.getParameter("username");
  285. String p = request.getParameter("password");
  286. String g = request.getParameter("gender");
  287. String b = request.getParameter("branch");
  288. try (PrintWriter out = response.getWriter()) {
  289. /* TODO output your page here. You may use following sample code. */
  290. out.println("<!DOCTYPE html>");
  291. out.println("<html>");
  292. out.println("<head>");
  293. out.println("<title>Servlet register</title>");
  294. out.println("</head>");
  295. out.println("<body>");
  296. out.println("<h1>Servlet register at " + request.getContextPath() + "</h1>");
  297.  
  298. dbconnection db = new dbconnection();
  299. int count = db.insert(u, p, g, b);
  300. if(count>0)
  301. {
  302. out.println("record inserted");
  303. }
  304. else
  305. {
  306. out.println("record not inserted");
  307. }
  308.  
  309. out.println("</body>");
  310. out.println("</html>");
  311. }
  312. }
  313.  
  314. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  315. /**
  316. * Handles the HTTP <code>GET</code> method.
  317. *
  318. * @param request servlet request
  319. * @param response servlet response
  320. * @throws ServletException if a servlet-specific error occurs
  321. * @throws IOException if an I/O error occurs
  322. */
  323. @Override
  324. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  325. throws ServletException, IOException {
  326. processRequest(request, response);
  327. }
  328.  
  329. /**
  330. * Handles the HTTP <code>POST</code> method.
  331. *
  332. * @param request servlet request
  333. * @param response servlet response
  334. * @throws ServletException if a servlet-specific error occurs
  335. * @throws IOException if an I/O error occurs
  336. */
  337. @Override
  338. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  339. throws ServletException, IOException {
  340. processRequest(request, response);
  341. }
  342.  
  343. /**
  344. * Returns a short description of the servlet.
  345. *
  346. * @return a String containing servlet description
  347. */
  348. @Override
  349. public String getServletInfo() {
  350. return "Short description";
  351. }// </editor-fold>
  352.  
  353. }
  354. -----------------
Add Comment
Please, Sign In to add comment