0

我正在尝试验证和插入数据。在这里,如果用户名存在于文件服务中,它应该发送错误消息,但在验证后发送错误消息并在文件服务中插入数据。我不能很好地在代码中失败。

我的 JSP:

<form name="create" id="myform" action="/create" method="post">
User Name:  <input type="text" name="cliname"/>
<input type ="submit" value="submit"/>
</form>

我的小服务程序:

public class CreateForm  extends HttpServlet {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
       String uname = req.getParameter("cliname");
       Query query1 = new Query("Users");
List<Entity> cli_id = datastore.prepare(query1).asList(FetchOptions.Builder.withDefaults());
for (Entity client : cli_id){
username = (String)client.getProperty("User Name");
if(username.equals(uname)) {
RequestDispatcher rd = req.getRequestDispatcher("/Create.jsp");
req.setAttribute("errormsg", "User Name Already Exists");
rd.forward(req, resp);}}
Entity userInput = new Entity("Users");
userInput.setProperty("User Name", uname);
datastore.put(userInput);
}}

请给我一个想法,

感谢您的帮助。

4

1 回答 1

1

我已经更新了你的代码。

public class CreateForm  extends HttpServlet {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
   String uname = req.getParameter("cliname");
   Query query1 = new Query("Users");
   List<Entity> cli_id = datastore.prepare(query1).asList(FetchOptions.Builder.withDefaults());
   RequestDispatcher rd = null;
for (Entity client : cli_id){
    username = (String)client.getProperty("User_Name");
    if(username.equals(uname)) {
        req.setAttribute("errormsg", "User Name Already Exists");
        rd = req.getRequestDispatcher("/error.jsp");
     }else{
        Entity userInput = new Entity("Users");
        userInput.setProperty("User_Name", uname);
        datastore.put(userInput);
        req.setAttribute("success", "User Name Added");
        rd = req.getRequestDispatcher("/Create.jsp");
     }
 }
     rd.forward(req, resp);
   }
 }
于 2013-01-04T18:06:46.797 回答