As an extension to my previous post (unable to get the passed data from servlet), here is my original question.
In regular JSP/Servlet MVC communication, we use requestdispatcher to send data from a servlet response to next jsp. Below is my current code.
Index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form name="form1" id="name1" method="post" action="GetData">
<input type="text" id="name" name="name" /> <input type="text"
id="number" name="number" /> <input type="submit" />
</form>
</body>
</html>
Index1.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<input id="Name" name="Name" type="text"
value="${userBeans[0].getName()}" />
<input id="Number" name="Number" type="text"
value="${userBeans[0].getNumber()}" />
</body>
</html>
UserBean
public class UserBean {
private String name;
private int number;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
GetData servlet
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
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 GetData
*/
@WebServlet("/GetData")
public class GetData extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public GetData() {
super();
// TODO Auto-generated constructor stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
GetDataDAO getdatadao = new GetDataDAO();
List<UserBean> userBeans = null;
try {
userBeans = getdatadao.list();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.setAttribute("userBeans", userBeans);
request.getRequestDispatcher("index1.jsp").forward(request, response);
}
}
GetDATADAO
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class GetDataDAO {
public List<UserBean> list() throws ClassNotFoundException, SQLException {
ArrayList<UserBean> list = new ArrayList<UserBean>();
Connection conn = null;
Statement stmt = null;
ResultSet rs;
String queryString = null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String userName = "sa";
String password = "T!ger123";
String url = "jdbc:sqlserver://home\\SQLEXPRESS;DatabaseName=TEST";
conn = DriverManager.getConnection(url, userName, password);
System.out.println("Connecting to database…");
System.out.println("Oracle JDBC Driver Registered!");
if (conn != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
stmt = conn.createStatement();
queryString = "SELECT * from UserTable";
rs = stmt.executeQuery(queryString);
while (rs.next()) {
UserBean userBean = new UserBean();
userBean.setName(rs.getString("name"));
userBean.setNumber(rs.getInt("number"));
list.add(userBean);
}
return list;
}
}
Basically in my program there is are 2 textboxes, once I click on the submit button, the data from database should be pulled and displayed in the textboxes in the second page(index1.jsp).
When i do my program in this manner everything is working fine. But when i use Jquery/AJAX, it is not passing the values to next page.
I've removed the action and method and changed the input type to button and assigned a ID. And below is my updated file(to use with Jquery/AJAX).
<form name="form1" id="name1">
<input type="text" id="name" name="name" /> <input type="text"
id="number" name="number" /> <input type="button" id="getCase" name="getCase"/>
<script type="text/javascript" src="indexJS.js"></script>
</form>
And I've created a JS file as below and in my index page I've added the required jquery and ajax files (the web links).
$('#getCase').click(function() {
$.ajax({
type : 'post',
url : 'GetData',
success : function(data) {
alert(data);
console.log(data);
},
});
});
And when I run this program nothing is happening, but the data is printed in the console,the entire HTML file with the values in it.
Can someone please let me know how can i display the values in the next page text boxes?
Thanks