Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp: Connectivity issue using jsp and mysql in netbeans

 
Deepika Choudhari
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey friends,

Anybody can help me to sort out my problem in getting connected to mysql database using jsp in netbeans. I have a login page and i want to login using uid and pwd, when i enter it displays invalid username and password although i have inserted uid and pwd in table called 'login' in mysql!!! Here I have the following code---


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.lang.*" %>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>login Page</title>
</head>
<body>
<%!
Connection con;
Statement st;
ResultSet rs;
String uid,pwd;

%>
<%
try
{
uid=request.getParameter("uid"); // uid is text field name of html page
pwd=request.getParameter("textfield"); // textfield is text field name of html page
uid=uid.trim();
pwd=pwd.trim();

Class.forName("com.mysql.jdbc.Driver");


con=DriverManager.getConnection("jdbc:mysql://localhost:3306/contact","root",""); // 'contact' database name

if(con==null)
{
out.println("Connection not opened");
}
else
{
out.println("Connection opened");
out.println("DB is connected");


st=con.createStatement();
String q="select * from login where username='"+uid+"' and password='"+pwd+"'";


rs=st.executeQuery(q);


while(rs.next())
{

%>

<jsp:forward page="Developers login page.html"></jsp:forward>

<%
}


out.println("Sorry...Invalid username and password.Please try again later");
}
}
catch(Exception e)
{
out.println(e.toString());
}

%>
</body>
</html>


instead of going to "Developers login page.html" its displaying error message as "Sorry...Invalid username and password.Please try again later" !!!


please help me in fixing out this issue! It will be very helpfull...
regards :)
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64973
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Step 1: move all that code out of a JSP and into a Java class where it belongs! JSP is for view generation, not control.

If this is part of an authentication system, first you should find previous discussions where Tim will advise you not to roll your own. But it you insist, this sort of check should be done in filters.
 
Deepika Choudhari
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey thanks for the suggestion,but previously I have used jsp code for bussiness logic and it run very well but there I have used Oracle for Database,this time I m using mysql but its showing error. I am developing a live website for the first time,so I am not understanding how to connect to Database.
Thanks for the reply!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64973
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me know when you have moved the code into a Java class and we can look at the it in its new home.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic