package com.example.datastoreWrite;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class DataStoreWriteServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
Key v1 = KeyFactory.createKey("Person", "Raghav");
Key r1 = KeyFactory.createKey("Person", "vinay");
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity e1, e2;
try {
e1 = datastore.get(r1);
e2 = datastore.get(v1);
Long vAge = (Long) e1.getProperty("age");
Long rAge = (Long) e2.getProperty("age");
System.out.println("vinay age"+vAge);
System.out.println(" age"+rAge);
} catch (EntityNotFoundException e) {
// Alice or Bob doesn't exist!
}
String message = "Simple";
req.setAttribute("message", message); // This will be available as ${message}
try {
req.getRequestDispatcher("/Show.jsp").forward(req, resp);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}}
这是我的JSP文件
<%@ page language="java"
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Simple DataStore Display</title></head>
<body><p>Message: ${message}</p></body>
</html>
MY WEB.xml
的xmlns = “http://java.sun.com/xml/ns/javaee”
的xmlns:网页= “http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”
的xsi:的schemaLocation =“http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd“version =”2.5“&gt;
<servlet>
<servlet-name>SimpleDataStoreApplicationTest</servlet-name>
<servlet-class>com.datasote.test.SimpleDataStoreApplicationTestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SimpleDataStoreApplicationTest</servlet-name>
<url-pattern>/simpledatastoreapplicationtest</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list></web-app>
答案 0 :(得分:0)
我怀疑您忘记在Show.jsp文件中正确关闭页面指令。
请使用:<%@ page language="java"%>
。
除上述内容外,您的代码看起来效果很好,如果数据不存在,您会在Message: Simple
页面中看到Show.jsp
输出。