我正在尝试验证和插入数据。在这里,如果用户名存在于文件服务中,它应该发送错误消息,但在验证后发送错误消息并在文件服务中插入数据。我不能很好地在代码中失败。
我的 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);
}}
请给我一个想法,
感谢您的帮助。