我正在尝试将post方法发送到servlet。它是如何工作的,它将检查操作字符串,然后触发命令DeleteCommand
并单击表中的对象然后它获取Reg对象的Id并将其从数据库中删除..
所以这是我的servlet:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
System.out.println(action);
if (action != null) {
Command command = new CommandFactory().createCommand(action);
if (command != null) {
command.execute(request, response);
}
}
}
这是我的命令Factory和它将根据操作字符串触发的命令类:
public Command createCommand(String actionString) {
Command command = null;
System.out.println(actionString);
//Instantiate the required Command object...
if (actionString.equalsIgnoreCase("login")) {
command = new LoginUserCommand();
} else if (actionString.equalsIgnoreCase("manageRegistrar")) {
command = new ManageRegistrarCommand();
} else if (actionString.equalsIgnoreCase("modifyReg")) {
command = new ModifyRegistrarCommand();
} else if (actionString.equalsIgnoreCase("logout")) {
//command = new LogoutUserCommand();
} else if (actionString.equalsIgnoreCase("something")) {
//command = new ManageRegistrarCommand();
} else if (actionString.equalsIgnoreCase("delete")) {
command = new DeleteRegistrarCommand();
} else if (actionString.equalsIgnoreCase("changePassword")) {
// command = new ChangePasswordCommand();
} else if (actionString.equalsIgnoreCase("verifyEmail")) {
//command = new VerifyEmailCommand();
} else if (actionString.equalsIgnoreCase("resendVerificationMail")) {
//command = new ResendVerificationMailCommand();
}else if(actionString.equalsIgnoreCase("modify")){
//command = new ModifyUserCommand();
}else if(actionString.equalsIgnoreCase("addRegistrar")){
//command = new CreateUserCommand();
}
//Return the instantiated Command object to the calling code...
return command;// may be null
}
这是我正在尝试获取实际参数值的命令:
public class DeleteRegistrarCommand implements Command {
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {
int userId = Integer.parseInt(request.getParameter("regId"));
//System.out.println(userIdList.get(0));
UserServices userservice = new UserServices();
HttpSession session = request.getSession();
try {
if (userId > 0) {
Registrar registrar = userservice.getRegById(userId);
if (userservice.delete(registrar.getRegId())) {
session.setAttribute("status", 0);
session.setAttribute("statusMessage", "Delete Success");
} else {
session.setAttribute("status", 1);
session.setAttribute("statusMessage", "Delete Failed");
}
} else {
session.setAttribute("status", 6);
session.setAttribute("statusMessage", "Ohoooo no.. Nothing to Delete.. ha ha ha");
}
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/ProcessResult.jsp");
dispatcher.forward(request, response);
} catch (ServletException | IOException ex) {
Logger.getLogger(DeleteUserCommand.class.getName()).log(Level.SEVERE, null, ex);
session.setAttribute("status", 4);
session.setAttribute("statusMessage", ex.getMessage());
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/ProcessResult.jsp");
try {
dispatcher.forward(request, response);
} catch (ServletException | IOException ex1) {
Logger.getLogger(DeleteUserCommand.class.getName()).log(Level.SEVERE, null, ex1);
}
}
}
}
确定所有这些都应该正常工作(希望如此)。我在客户端(JSP页面)遇到问题。我正在尝试将操作类型和命令所需的值发送到我的servlet ...这就是我在href
上的内容,但似乎不正确。
所以就是这样:
<td class="text-center"><a href="WebActionServlet?action=delete?regId=<%=deleteId%>"><samp class="glyphicon glyphicon-remove"></samp></td>
但这似乎不起作用,当我点击它时,我只是向我显示一个空白页面并且没有执行实际操作..有谁知道如何使用href链接执行此操作或者它们是否更好方式可能是java脚本或ajax方法用于执行此操作并获取id并基于该表数据链接发送操作?
感谢任何帮助。提前谢谢。