-
Javadatabase login with Tomcat
Hi,
I'm trying to create a website where users can upload partypics and such. Pretty lame idea but thats not the problem. I am trying to create a login page which checks in my database if the user name and the password is correct. However the problem is that everything works great with just one user name and password in my database, but as soon as another one registers no one can login.
Here is my code.
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.Statement;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoggaIN extends HttpServlet {
Statement stmt;
ResultSet rs;
Connection con;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String namn = request.getParameter("user");
String losen = request.getParameter("password");
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = (Connection) DriverManager.getConnection("jdbc dbc:databasen","","");
stmt = con.createStatement();
rs = (ResultSet) stmt.executeQuery("select UserName, Password from users");
while(rs.next()) {
if (!(namn.equals("null")) && (namn.equals(rs.getString("UserName"))) && (!(losen.equals("null")) && losen.equals(rs.getString("Password"))))
{
response.sendRedirect("inloggad.html");
}
else
{
response.sendRedirect("fel.html");
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
-
It looks like you are redirecting the user if their name and password don't match the first one in the database. Move this line
Code:
response.sendRedirect("fel.html");
outside of the while loop and get rid of the else clause. That should take care of it.
-
It worked!!
Yep, that did the trick! Thank you very much
Similar Threads
-
By dev_user in forum Java
Replies: 3
Last Post: 01-26-2012, 01:16 AM
-
Replies: 3
Last Post: 08-01-2007, 08:46 PM
-
By dbrook007 in forum ASP.NET
Replies: 0
Last Post: 11-06-2006, 04:54 AM
-
By sujiganne in forum Java
Replies: 2
Last Post: 04-07-2006, 06:39 AM
-
By natoskiii in forum Java
Replies: 1
Last Post: 02-27-2006, 03:30 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|