我已经使用JSP创建了一个Form数据页并应用了ajax调用,并希望将数据存储在MS访问中,但我发现存在一些困难,因为表单数据不存储,甚至控制台也没有显示任何错误。 请指导我..
我的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 am 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 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 and 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");
try
{
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 into UserTable values('"+name+"','"+email+"','"+password+"')");
if(nrow>0)
{
System.out.println("Record Inserted");
}
rs=stmt.executeQuery("SELECT * FROM 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 try block
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException w)
{
w.printStackTrace();
}
}
}
答案 0 :(得分:0)
你应该在ajax帖子中更改此URL url:"localhost:8080/RestDemo/UserInfoService"
,应该是Web.xml
$.ajax({
type:"POST",
url:"/UserInfoService",
dataType:"json",
data:jsonString
});
如果上述内容无效,请尝试/rest/UserInfoService
。
我希望你从你的jsp文件中调用UserInfo
答案 1 :(得分:0)
New login.jsp page
<%@ 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 am in SendData mode");
var arr = {"name": name,
"email": email,
"password": password
};
alert("Before Ajax call");
$.ajax({
type:"POST",
url:"rest/UserInfoService",
dataType:"json",
data:arr
});
alert("After Ajax 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>