Saturday 5 September 2015

JSP Servlet Databse LoginPage

 Watch this video
newjsp.jsp

<%--
    Document   : newjsp
    Created on : 2 Sep, 2015, 10:25:39 PM
    Author     : harsh
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>Login page</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>
            <form action="log" method="post">
                Name :<input type="text" name="name"><br>
                PASS :<input type="password" name="pass"><br>
                <input type="submit" name="submit" value="submit">
            </form>
        </div>
    </body>
</html>

NewServlet.java

// a pure processing servlet to authenticate the user
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class NewServlet extends HttpServlet
{
   /// private static final String url = "jdbc:mysql://localhost/vusers";

    //    private static final String user = "root";

      //  private static final String pw = "mysql";
           
    public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException
    {
        try
        {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/users","hall","bed");
            PreparedStatement ps = conn.prepareStatement("SELECT * FROM USERNAME WHERE username=? AND password=?");
            // get data of form
                       
            String username = req.getParameter("name");
            String password = req.getParameter("pass");
            // set the values for parameters
            ps.setString(1,username);
            ps.setString(2,password);
            ResultSet rs = ps.executeQuery();
            if (rs.next())
            {
                HttpSession session = req.getSession(true);
                                session.setAttribute("Welcome",username);
                res.sendRedirect("index.html");  // Wel is
               
            }
            else
                            res.sendRedirect("newjsp1.jsp");
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}
       


       

No comments:

Post a Comment