I need to get request.getParameter("action"), which I use in my JSP page, in order to define what command should execute (<input type="hidden" name="action" value="open_list"/>). Result is null. What is the problem? Or how I can pass command in another way?
CommandFactory.java
public class CommandFactory { public Command defineCommand(HttpServletRequest request) { Command current = new ErrorCommand(); String action = request.getParameter("action"); if (action == null || action.isEmpty()) { return current; } try { CommandEnum currentEnum = CommandEnum.valueOf(action.toUpperCase()); current = currentEnum.getCommand(); } catch (IllegalArgumentException e){ request.setAttribute("wrongAction", action); } return current; } } MainServlet.java
public class MainServlet extends HttpServlet { private static final long serialVersionUID = 1L; public MainServlet() { super(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { CommandFactory client = new CommandFactory(); Command command = client.defineCommand(request); String page = command.execute(request, response); if (page != null) { RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(page); dispatcher.forward(request, response); }else { response.sendRedirect(PageURL.ERROR_PAGE); } } } JSP page
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <title>Title</title> </head> <body> <form name="hotelList" method="GET" action="/MainServlet"> <input type="hidden" name="action" value="open_list"/> <table> <tr> <th>ID</th> <th>NAME</th> <th>ADDRESS</th> <th>RATING</th> <th>OWNERNAME</th> </tr> <c:forEach items="${list}" var="hotel"> <tr> <td>${hotel.id}</td> <td>${hotel.name}</td> <td>${hotel.address}</td> <td>${hotel.rating}</td> <td>${hotel.ownerName}</td> </tr> </c:forEach> </table> </form> </body> </html>
null. Suppose I doing something wrong<table>orCommandFactorythen your problem is totally solved. But this is not true. Why are you including so many noise in the code for us to crawl through in order to find the cause and give you an answer? Try helping us to help you.