<%-- 
    Document   : Login1
    Created on : Jul 1, 2011, 1:16:12 AM
    Author     : Dharmendra
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%@page import="com.p1.*" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body><center>
            <form action="">
             <br>UserID   :   <input type="text" name="name" value="" size="20" />
             <br>Password :   <input type="password" name="pass" value="" size="20" />
             <br><input type="submit" value="Login" />
            </form>
        </center>
        
    </body>
</html>
<%
int x=0;
String s1=request.getParameter("name");
String s2=request.getParameter("pass");
out.print(s1+s2);
Connection con=null;
ResultSet rs=null;
PreparedStatement ps=null;
try{
    Class.forName("com.mysql.jdbc.Driver");
    con=DriverManager.getConnection("jdbc:mysql://localhost/dk","root","admin");
    
    String sql="select *from login where userid=? and pass=?";
    ps=con.prepareStatement(sql);
    ps.setString(1, s1);
    ps.setString(2, s2);
    rs=ps.executeQuery();

     if(rs.next()){
        x=1;
        }
    RequestDispatcher rd=null;
    if(x==1){
        out.print("ok");
        rd=request.getRequestDispatcher("home.jsp");
      }
    else{
        out.print("Not ok");
        rd=request.getRequestDispatcher("Login1.jsp");
        }
    rd.forward(request, response);

    }catch(Exception e){
        e.printStackTrace();
        }
finally{
    try{
    rs.close();
    ps.close();
    con.close();
    }catch(Exception ee)
    {
        ee.printStackTrace();
    }
  }

%>

Recommended Answers

All 5 Replies

@dharma117
This Method of Login is absolutely wrong! You Need To Make of Use of Servlets and classes! Once done then be able to call methods to servlets from classes. Using Database Connectivity in JSP is likely to violate design pattern and you are rather going from a good solution to even a worse solution! Use Servlets and Classes. DON'T PUT DATABASE CODE IN JSP!

Can you explain the code? Because it is very wrong! So maybe you didn't meant to write that but it is just some sort of pseudo code.
If it is real code then it is has some errors.

No, I doubt he can because he simply copied and pasted it here from another post he saw elsewhere - which is why I have deleted it.

The Link Posted by @javaAddict is a very excellent tutorial on how to create a login system and has implementation of the MVC Framework!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.