-1

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> 
2
  • @ElliottFrisch same problem, it's null. Suppose I doing something wrong Commented Apr 22, 2020 at 13:00
  • Roman, during your registration you was being presented How to Ask and subsequently minimal reproducible example. Why did you ignore these? Right now your question implies that when you remove e.g. <table> or CommandFactory then 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. Commented Apr 22, 2020 at 13:09

1 Answer 1

0

I tried to reproduce your issue, you don't have any control with type="submit" on your page.

When you click submit button, it will get correct your request.getParameter("action") in servlet.

<form name="hotelList" method="GET" action="MainServlet"> <input type="hidden" name="action" value="open_list"/> <input type="submit" value="Send"> </form> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.