Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- index.html -
- <!DOCTYPE html>
- <!--
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
- -->
- <html>
- <head>
- <title>TODO supply a title</title>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- </head>
- <body>
- <div>Login</div>
- <form action="login" method="POST">
- username:-<input type="text" name="uname"><br><br>
- password:-<input type="text" name="pass"><br>
- <input type="submit">
- </form>
- <br><br>
- <a href="registerhtml.html">Sign Up</a>
- </body>
- </html>
- ----------------------
- registerhtml.html -
- <!DOCTYPE html>
- <!--
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
- -->
- <html>
- <head>
- <title>TODO supply a title</title>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- </head>
- <body>
- <div>Register</div>
- <form action="register" method="POST">
- username:-<input type="text" name="username"><br><br>
- password:-<input type="text" name="password"><br><br>
- gender:- <input type="text" name="gender"><br><br>
- branch:-<input type="text" name="branch"><br><br>
- <input type="submit">
- </form>
- </body>
- </html>
- ------------------
- dbconnection.java -
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package db;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author JNEC-03
- */
- public class dbconnection {
- Connection conn;
- Statement stmt;
- ResultSet rs;
- public dbconnection(){
- String dburl="jdbc:derby://localhost:1527/warlock";
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(dbconnection.class.getName()).log(Level.SEVERE, null, ex);
- }
- try {
- conn = DriverManager.getConnection(dburl,"root","toor");
- stmt =conn.createStatement();
- } catch (SQLException ex) {
- Logger.getLogger(dbconnection.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public boolean check(String un,String pwd)
- {
- try {
- String query="select * from ROOT.STUDENT where username='"+un+"' and password='"+pwd+"'";
- rs=stmt.executeQuery(query);
- if(rs.next())
- {
- return true;
- }
- else{
- return false;
- }
- } catch (SQLException ex) {
- Logger.getLogger(dbconnection.class.getName()).log(Level.SEVERE, null, ex);
- }
- return false;
- }
- public int insert(String u,String p,String g,String b){
- try{
- String query="insert into ROOT.STUDENT values ('"+u+"', '"+p+"', '"+g+"', '"+b+"')";
- System.out.print(query);
- int count = stmt.executeUpdate(query);
- return count;
- }
- catch(Exception ex){
- }
- return 0;
- }
- }
- ------------------------------------
- login.java -
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package db;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- *
- * @author JNEC-03
- */
- @WebServlet(name = "login", urlPatterns = {"/login"})
- public class login extends HttpServlet {
- /**
- * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
- * methods.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- String username = request.getParameter("uname");
- String password = request.getParameter("pass");
- try (PrintWriter out = response.getWriter()) {
- /* TODO output your page here. You may use following sample code. */
- out.println("<!DOCTYPE html>");
- out.println("<html>");
- out.println("<head>");
- out.println("<title>Servlet login</title>");
- out.println("</head>");
- out.println("<body>");
- out.println("<h1>Servlet login at " + request.getContextPath() + "</h1>");
- dbconnection db= new dbconnection();
- boolean flag = db.check(username, password);
- if(flag)
- {
- out.println("login successful");
- }
- else{
- out.println("login unsuccessful");
- }
- out.println("</body>");
- out.println("</html>");
- }
- }
- // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
- /**
- * Handles the HTTP <code>GET</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Handles the HTTP <code>POST</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Returns a short description of the servlet.
- *
- * @return a String containing servlet description
- */
- @Override
- public String getServletInfo() {
- return "Short description";
- }// </editor-fold>
- }
- -----------------------
- register.java -
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package db;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- *
- * @author JNEC-03
- */
- @WebServlet(name = "register", urlPatterns = {"/register"})
- public class register extends HttpServlet {
- /**
- * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
- * methods.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- String u = request.getParameter("username");
- String p = request.getParameter("password");
- String g = request.getParameter("gender");
- String b = request.getParameter("branch");
- try (PrintWriter out = response.getWriter()) {
- /* TODO output your page here. You may use following sample code. */
- out.println("<!DOCTYPE html>");
- out.println("<html>");
- out.println("<head>");
- out.println("<title>Servlet register</title>");
- out.println("</head>");
- out.println("<body>");
- out.println("<h1>Servlet register at " + request.getContextPath() + "</h1>");
- dbconnection db = new dbconnection();
- int count = db.insert(u, p, g, b);
- if(count>0)
- {
- out.println("record inserted");
- }
- else
- {
- out.println("record not inserted");
- }
- out.println("</body>");
- out.println("</html>");
- }
- }
- // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
- /**
- * Handles the HTTP <code>GET</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Handles the HTTP <code>POST</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Returns a short description of the servlet.
- *
- * @return a String containing servlet description
- */
- @Override
- public String getServletInfo() {
- return "Short description";
- }// </editor-fold>
- }
- -----------------
Add Comment
Please, Sign In to add comment