-2
<%-- 
    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();
    }
  }

%>
4
Contributors
5
Replies
8
Views
6 Years
Discussion Span
Last Post by SagarSe7en
0

@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!

0

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.

Edited by Nick Evan: Fixed formatting

1

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.

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.