3

I want to connect to a database depending on the data enter in a .jsp form. I do not know how to connect to database in a Javascript.

My code as follows:

<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<html>
<head>
<script>
function validateForm()
{
   if(document.frm.username.value=="srinu")
{
  // conect database here
}
else 
{
  alert("wrong input");
  document.frm.pwd.focus();
  return false;
}
}

Body here, i want to connect database based on the details entered in the body.

 <body>
<form name="frm" method="get" action="validateInput.jsp" onSubmit="return validateForm()">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
   <tr>
   <td width="22%">&nbsp;</td>
   <td width="78%">&nbsp;</td>
  </tr>
  <tr>
   <td>UserName </td>
  <td><input type="text" name="username" /></td>
 </tr>

<tr>
<td>&nbsp;</td>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</form>
</body>
</html>
3
  • 1
    You should not do this validation inside your javascript. It is not a good practice. You pass this values to servlet and do validate there. Commented Oct 10, 2013 at 8:41
  • can you give some simple example here ? Commented Oct 10, 2013 at 8:42
  • You can create a database connection in JSP as same as Java. But its not a good practice you need to create connection once and carry that connection through out the application with help of JSP session. Commented Oct 10, 2013 at 9:29

2 Answers 2

4

You can not connect to database from client side. JavaScript run on a browser which has a very strict security restrictions. And keeping any connection string on JavaScript is not a good practice at all . Don't keep any sensitive things on client side. Make a call to server side validate and work on that.

function validateForm()
{
    if(document.frm.username.value=="srinu")
    {
        $.ajax({
            type: 'GET',
            url: 'validateuser?username=document.frm.username.value' + '&Pwd=' + document.frm.userpassword.value,
            success: function (data) {                   
                if (data=='Y')
                    alert('Valid User');
                else
                    alert('Invalid User');
            }, cache: false,
            error: function (xhr, ajaxOptions, thrownError) {
                alert('failed.');
            }
            });
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

@user2773010 Glad to help, not for entire project. You need to think about how to implement the following snippit as per your requirements.

String userName = request.getParameter("user");
String password = request.getParameter("pass");

try
{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")   ;
    con = DriverManager.getConnection("jdbc:odbc:library", "administrator", "");
    stat = con.createStatement();
    System.out.println("user Connected");
    rs = stat.executeQuery("Select * FROM User_Master");
    while(rs.next())
    {

    String un = rs.getString(5);
    String Pwd = rs.getString(6);

    if(un.equals(userName) && Pwd.equals(password))
    {
        session.setAttribute("username",un);
        String des = "http://localhost:8080/Final_Review/review/homeuser.jsp";
        response.sendRedirect(response.encodeRedirectURL(des));
        break;
    }
    /* else
    {
        response.sendRedirect(response.encodeRedirectURL("http://localhost:8080/Final_Review/review/ErrorPage.jsp"));
    }*/
    }
    stat.close();
    con.close();
}
catch (ClassNotFoundException e)
{
    e.printStackTrace();
}
catch(SQLException se)
{
    System.out.println(se);
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.