how to get popup fro servelt to jsp by using ajax.
I went popup in jsp page when my form submit successfully. through servlet can any buddy give r explian how to write the code
Sumit kumar jain pithani
this simple dude it is like popup after submiting the form the value of field will sub,mit r insert into data base server the is simple code for it 1.jsp page 2.java script page 3.servlet page here is jsp page :Salu2.jsp file ---------------------------------------------------------------------------------------------------------------------------- <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="Controller.ConnectionString"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <link rel="stylesheet" type="text/css" href="Main.css"> <script language="javascript" type="text/javascript" src="saluValidate.js"></script> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <div id="coltwo1"> <div id="salu" > <div id="tittle"> SALUTATION </div> <div id="saluin"> <form name="Salutation" method="post" action="Salutation"> <fieldset style="width:350px;height: 100px; "> <div id="smart" style="margin-top: 10px;" > <label style="font-size: medium;" title="Fulltitle">Full Name </label> <input type="text" name="Fulltitle" id="title1" style="width: 180px" title="select Fulltitle "> </div> <div id="smart" style="padding-top: 5px;"> <label style="font-size: medium;" title="Shorttitle">Short Name </label> <input type="text" name="Shorttitle" id="title2" title="select Shorttitle "> </div> <input type="hidden" name="id" id="title3" value="xx"> </fieldset> <div style=" clear: both;"> <input type="submit" name ="s1" value="SUBMIT" onclick="return validateSalu(this.value);" class="button" title="SUBMIT"> <input type="reset" name ="s2" value="RESET" class="button" title="RESET"> <input type="button" name ="s3" value="UPDATE" class="button" onclick="return validateSalu(this.value);" title="UPDATE"> <input type="button" name ="s4" value="DELETE" class="button" onclick="return validateSalu(this.value);" title="DELETE"> </div> </form> </div> <div id="salu1" > <select name="drop1" size="6" multiple="multiple" style="width: 150px;" id="listB1" onclick="return func(this.value);" title="drop1"> <optgroup label=" SALUTAION"></optgroup> <% ConnectionString cs=new ConnectionString(); cs.getConn(); PreparedStatement p=cs.conn.prepareStatement("select * from Salutation "); ResultSet rs=p.executeQuery(); while(rs.next()) { String sid=rs.getString(1); String stitle=rs.getString(2); %> <option value="<%=sid%>"> <% out.println(sid +" "+stitle); %> </option> <%} rs.close(); p.close(); %> </select> </div> </div> </div> </body> </html> ------------------------------------------------------------------------------------------------------------------------ this my java script and ajax file same..:saluValidate.js ---------------------------------------------------------- function validateSalu(butt){ var fn=document.Salutation.Fulltitle.value; var sn=document.Salutation.Shorttitle.value; var salu_id=document.Salutation.id.value; var i=0; var j=0; alert("sumit "+(++j)); if(fn=="") { alert("Please Enter Full Name"); fn=""; document.Salutation.Fulltitle.focus(); return false; }else if(!fn.match(/^[a-zA-Z]+$/)){ alert("Please Enter Correct Full Name"); fn=""; document.Salutation.Fulltitle.focus(); return false; } if(sn=="") { alert("Please Enter Short Name"); sn=""; document.Salutation.Shorttitle.focus(); return false; }else if(!sn.match(/^[a-zA-Z]+$/)){ alert("Please Enter Correct Short Name"); sn=""; document.Salutation.Shorttitle.focus(); return false; } var B=true; if((/SUBMIT/g).test(butt)){ B=confirm("DO YOU WANT SUBMIT --- SALUTATION !!! PRESS OK ELSE CANCEL"); i=1; //alert(""+butt+""+i); }else if((/UPDATE/g).test(butt)){ B=confirm("DO YOU WANT UPDATE --- SALUTATION !!! PRESS OK ELSE CANCEL"); i=2; //alert(""+butt+""+i); }else if((/DELETE/g).test(butt)){ B=confirm("DO YOU WANT DELETE --- SALUTATION !!! PRESS OK ELSE CANCEL"); i=3; //alert(""+butt+""+i); } if(B==true){ AjexFun(fn,sn,i,salu_id); return true; }else{ return false; } } var xmlhttp; function AjexFun(f,s,b,i){ alert("sumit1"); url="Salutation?full="+f+"&short="+s+"&button1="+b+"&id="+i; if(window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest(); } else{ xmlhttp=new ActiveXobjective("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=fun; alert("sumit2"); xmlhttp.open("post",url, true); alert("sumit3"); xmlhttp.send(); } function fun(){ if(xmlhttp.readyState==4){ var x=xmlhttp.responseText; alert(x); document.getElementById("salu1").innerHTML=x; document.getElementById("title3").value=""; document.getElementById("title1").value=""; document.getElementById("title2").value=""; return false; } } function func(id){ //alert("sumit1"); url="Salutation?id1="+id; if(window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest(); }else{ xmlhttp=new ActiveXobjective("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=fun1; //alert("sumit2"); xmlhttp.open("get",url, true); //alert("sumit3"); xmlhttp.send(); } function fun1(){ if(xmlhttp.readyState==4){ var x=xmlhttp.responseText; alert("sumit1"+x); var y=x.split(":"); document.getElementById("title3").value=y[0]; document.getElementById("title1").value=y[1]; document.getElementById("title2").value=y[2]; return true; } } ------------------------------------------------------------ ------------------------------------------------------------ this my java servlet page ............:Salutation.java --------------------------------------------------------- package Controller; import java.io.IOException; import java.io.PrintWriter; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class Salutation */ @WebServlet("/Salutation") public class Salutation extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Salutation() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //PrintWriter out=response.getWriter(); System.out.println("get1"); ConnectionString cs=new ConnectionString(); cs.getConn(); String salid=" "; String result=""; salid=(request.getParameter("id1")).toUpperCase(); try{ PreparedStatement p=cs.conn.prepareStatement("select * from Salutation where salu_id ='"+salid+"'" ); ResultSet rs=p.executeQuery(); System.out.println("get3"); while(rs.next()) { result+=rs.getString("salu_id"); result+=":"+rs.getString("salu_name"); result+=":"+rs.getString("salu_title"); } response.getWriter().print(result); }catch(Exception e){ System.out.print(e.getMessage()); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub try{ PrintWriter out=response.getWriter(); System.out.println("servlet1"); ConnectionString cs=new ConnectionString(); cs.getConn(); String fullname=" "; String shortname=" "; String sid=" "; int count=0; System.out.println("servlet2"); fullname=(request.getParameter("full")).toUpperCase(); shortname=(request.getParameter("short")).toUpperCase(); sid=(request.getParameter("id")).toUpperCase(); String button=request.getParameter("button1"); System.out.println(button); Integer i= Integer.valueOf(button); System.out.println(i+""+sid); switch(i){ case 1: try{ PreparedStatement p=cs.conn.prepareStatement("select * from Salutation where salu_name ='"+fullname+"'"+"and salu_title='"+shortname+"'" ); ResultSet rs=p.executeQuery(); System.out.println("servlet3"); while(rs.next()) { count++; } if(count>=1) { System.out.println("servlet4"); out.println("already exist !!!"); } else { System.out.println("servlet5"); String url="{call salut (?,?)}"; CallableStatement p1=cs.conn.prepareCall(url); p1.setString(1, fullname); p1.setString(2, shortname); p1.executeUpdate(); out.println("Submit successfull !!!"); } }catch(Exception e){ System.out.print(e.getMessage()); } break; case 2: try{ String url="{call update_salut (?,?,?)}"; CallableStatement p1=cs.conn.prepareCall(url); p1.setString(1,sid); p1.setString(2,fullname); p1.setString(3,shortname); p1.executeUpdate(); PreparedStatement p2=cs.conn.prepareStatement("select * from Salutation where status<>"+2); p2.executeQuery(); response.getWriter().print("Update successfull !!!"); }catch(Exception e){ System.out.print(e.getMessage()); } break; case 3: try{ System.out.print("jain Delete 1"+sid); String url="{call del_salu1 (?)}"; System.out.print("jain Delete 1"); CallableStatement p1=cs.conn.prepareCall(url); p1.setString(1,sid); p1.executeUpdate(); PreparedStatement p2=cs.conn.prepareStatement("select * from Salutation where status<>"+2); p2.executeQuery(); response.getWriter().print("Delete successfull !!!"); }catch(Exception e){ System.out.print(e.getMessage()); } break; } }catch(Exception e){ response.sendRedirect("Salu2.jsp"); System.out.println(e.getMessage()); } } } ------------------------------------------------------------------------------------------------------------------------ connection between the database and servelet using java script:ConnectionString.java ----------------------------------------------- package Controller; import java.sql.*; public class ConnectionString { public Connection conn=null; public void getConn() { try { String url="jdbc:sqlserver://"*******ip--address***********";databaseName=komal"; Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); conn=DriverManager.getConnection(url, "username", "password"); } catch(Exception e) { e.getMessage(); } } }
Ads