This is my first servlet...
public class ServletCheckServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
PrintWriter pw=resp.getWriter();
String s1=req.getParameter("username");
String s2=req.getParameter("password");
DatastoreService datastore=DatastoreServiceFactory.getDatastoreService();
Entity employee=new Entity("Employee");
employee.setProperty("firstname", "Ram");
employee.setProperty("lastname", "prasad");
Date hiredate=new Date();
employee.setProperty("Hiredate", hiredate);
employee.setProperty("attendedHRtraining",true);
datastore.put(employee);
Key employeekey=KeyFactory.createKey("employees", "employee1");
}
}
My second servlet is below....
public class ValidUserServlet extends HttpServlet{
protected void doGet(HttpServletRequest req,HttpServletResponse resp){
PrintWriter pw;
try {
pw = resp.getWriter();
pw.println("Welcome to the second servlet");
Key employeekey=KeyFactory.createKey("employees", "employee1");
DatastoreService datastore=DatastoreServiceFactory.getDatastoreService();
try {
Entity employee=datastore.get(employeekey);
String fn=(String) employee.getProperty("firstname");
pw.println(fn);
} catch (EntityNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-genera开发者_如何学运维ted catch block
e.printStackTrace();
}
}
}
But when i run this Application.....the servlet displys...but the values from Appengine datastore is not displayed...
What is the problem in my programs...how to clear this...is it correct......
Please help me.......
Instead of using Key employeekey=KeyFactory.createKey("employees", "employee1");
You can use
Entity employeeEntity = datastore.get(KeyFactory.stringToKey(employee.getId()));
Since you are using a plain old servlet you can get employeeId
from request
String employeeId = request.getParameter("employeeId");
Entity employeeEntity = datastore.get(KeyFactory.stringToKey(employeeId);
You can reference this source code to further clarify.
精彩评论