admin管理员组

文章数量:1289867

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>

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>
Share Improve this question edited Jul 10, 2014 at 18:53 Mike Stockdale 5,2663 gold badges31 silver badges34 bronze badges asked Oct 10, 2013 at 8:38 VasuVasu 3651 gold badge10 silver badges18 bronze badges 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. – Vinoth Krishnan Commented Oct 10, 2013 at 8:41
  • can you give some simple example here ? – Vasu 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. – Srinath Thota Commented Oct 10, 2013 at 9:29
Add a ment  | 

2 Answers 2

Reset to default 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.');
            }
            });
    }
}

@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);
}
}

本文标签: htmlHow to write a database connection in JavascriptStack Overflow