Choose Your Language

Monday 27 May 2013

passing multiple parameter to jsp page using ajax example code

<%--
    Document   : userregistration
    Created on : 17 May, 2013, 11:22:42 AM
    Author     : Aravind Sankaran
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>

<%!static Connection con;%>
<%!  static String url="jdbc:mysql://localhost:3306/dbname?";%>
<%! static String userName = "dbUserName"; %>
<%! static String password = "dbPassword";%>

 <%
         Class.forName("com.mysql.jdbc.Driver");
         con=(Connection)DriverManager.getConnection(url, userName, password);
         Statement s = (Statement)con.createStatement();
          ResultSet rs=s.executeQuery("SELECT * FROM usertype");
%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;

var usertype = document.getElementById("utype");
var userType=
usertype .options[usertype .selectedIndex].value;
var uname=document.getElementById("username").value;
var urls="checkusername.jsp?un="+
uname+"&ut="+userType;

if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4)
    {
        document.getElementById("err").innerHTML=xmlhttp.responseText;
     }
  }
xmlhttp.open("GET",urls,true);
xmlhttp.send();
}
</script>

    </head>
    <body>
<%

%>
        User Type<select name="utype" id="utype">
<%while(rs,next()){
 String userType=rs.getString("userTypeColumnName");
%>
                              <option  value="<%=userType%>"><%=userType%></option>
<%}%>
                        </select>
        User Name: <input type="text" name="username" id="username" onkeyup="loadXMLDoc()">
        <span id="err"> </span>

    </body>
</html>


checkusername.jsp


<%--
    Document   : checkusername
    Created on : 9 May, 2013, 12:42:25 PM
    Author     : Aravind Sankaran
--%>

<%@ page import="java.io.*,java.sql.*" %>
<%@ page contentType="text/html" pageEncoding="UTF-8"%>

<% 
                    String
un=request.getParameter("un");                    String ut=request.getParameter("ut");
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename","databaseusername","databasepassword");
                    Statement st=con.createStatement();
                    ResultSet rs = st.executeQuery("select * from table_name where username='"+un+"' AND usertype='"+ut+"' ");  // this is for name
                    if(rs.next())
                    {   
                        out.println("<font color=red>");
                        out.println("Name already taken");
                        out.println("</font>");

                    }else {
                        out.println("<font color=green>");
                        out.println("Available");
                        out.println("</font>");

                    }
rs.close();
st.close();
con.close();
%>

No comments:

Post a Comment