Ajax JSON and jsp -
Ajax JSON and jsp -
i have created form info page using jsp , apply ajax phone call , want store info in ms access found difficulty form info not store , console doesn't show error.. please guide me..
my login.jsp
<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <!doctype html> <html> <head> <title>login</title> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script type="text/javascript"> function senddata() { alert("i m in senddata function"); var email=$('#email').val(); var name=$('#name').val(); var password=$('#password').val(); alert("i in senddata mode"); var jsondataobject=new object(); alert(" json call"); jsondataobject.name=name; jsondataobject.email=email; jsondataobject.password=password; var jsonstring= json.stringify(jsondataobject); alert("before ajax call"); $.ajax({ type:"post", url:"localhost:8080/restdemo/userinfoservice", datatype:"json", data:jsonstring }); alert("after ajax phone call send data"); } </script> </head> <body bgcolor=#d8d8d8> <form name="form" method="post" onsubmit="senddata()"> <div style="margin-left:300px;margin-top:110px;background-color:#5fb404;padding:20px;width:650px;height:450px" > <h1><center> login registration form</center></h1> <pre> <p style="font-size:20px"> name :<input type="text" name="name" id="name" ><br> email :<input type="text" name="email" id="email" ><br> password :<input type="password" name="password" id="password"> <center><input type="submit" name="submit" value="submit" style="padding:7px"></center> </p></pre> </div> </form> </body> </html>
2.web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="webapp_id" version="3.0"> <display-name>restdemo</display-name> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>jersey rest service</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.servletcontainer</servlet-class> <!-- register resources , providers under com.vogella.jersey.first package. --> <init-param> <param-name>jersey.config.property.packages</param-name> <param-value>service</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey rest service</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app>
3.userinfo.java
package service; import javax.ws.rs.consumes; import javax.ws.rs.formparam; import javax.ws.rs.post; import javax.ws.rs.path; import javax.ws.rs.produces; import javax.ws.rs.core.mediatype; @consumes(mediatype.application_json) @path("/userinfoservice") public class userinfo { @post @consumes(mediatype.application_form_urlencoded) //@produces(mediatype.application_xml) public void setdetails(@formparam("name") string name, @formparam("email") string email, @formparam("password") string password) { userdetailsdao userdetailsdao=new userdetailsdaoimpl(); userdetailsdao.getuserdetails(name, email, password); } }
4.userdetaildao.java
package service; public interface userdetailsdao { public void getuserdetails(string name,string email,string password); }
5.userdetaildaoimpl.java
package service; import java.sql.*; public class userdetailsdaoimpl implements userdetailsdao { @override public void getuserdetails(string name, string email, string password) { system.out.println("test"); seek { connection conn; statement stmt; resultset rs; class.forname("sun.jdbc.odbc.jdbcodbcdriver"); conn=drivermanager.getconnection("jdbc:odbc:datainsert","",""); stmt=conn.createstatement(); int nrow=stmt.executeupdate("insert usertable values('"+name+"','"+email+"','"+password+"')"); if(nrow>0) { system.out.println("record inserted"); } rs=stmt.executequery("select * usertable"); while(rs.next()) { string getname = rs.getstring(1); string getemail = rs.getstring(2); string getpassword = rs.getstring(3); system.out.println(getname+" "+getemail+" "+getpassword); } rs.close(); stmt.close(); conn.close(); } //end of seek block catch(classnotfoundexception e) { e.printstacktrace(); } catch(sqlexception w) { w.printstacktrace(); } } }
you should alter url url:"localhost:8080/restdemo/userinfoservice"
in ajax post, should url pattern mapped servlet file in web.xml
$.ajax({ type:"post", url:"/userinfoservice", datatype:"json", data:jsonstring });
try /rest/userinfoservice
if above 1 doesnt work.
i expect calling userinfo
jsp file
ajax json jsp
Comments
Post a Comment