0

I'm forwording a sending a request after a click on a form button to a controller servlet and it needs to check if the user is logged in and which button was pressed.

The first part works fine but when im tring to set the result as a parameter and forward the request the JSP page ,the parameter "Button" always returns null. Can someone help me understand why this is happening? maybe there is a better way to do this?

Servlet:

request.setAttribute("Buttons", "Add a new coupon");
request.getRequestDispatcher("/admin_main.jsp").forward(request,response);

JSP:

if (request.getParameter("Buttons") != null) {
    // ...
} 
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
moshe
  • 439
  • 2
  • 6
  • 16
  • 1
    You should show some code so that bugtracking is possible. – Sven Hager May 10 '12 at 20:05
  • 2
    Please, please, please, please read up on JSP EL and perhaps find a JSP tutorial that wasn't written in 1999. (I appreciate that's easier said than done.) While it CAN be done, JSP simply ISN'T done "this way" today. There's just no reason for it. This book is actually not too bad. http://www.theserverside.com/news/1369772/Free-Book-Servlets-and-JavaServer-Pages-The-J2EE-Technology-Web-Tier – Will Hartung May 10 '12 at 21:02
  • thank for the book link...i start reading it but for the course project the way i did things will have to do... – moshe May 12 '12 at 06:32
  • Related: [How to avoid Java code in JSP files?](http://stackoverflow.com/questions/3177733) – BalusC Apr 04 '16 at 11:37

1 Answers1

3

if(request.getAttribute("Buttons") != null) will work.

Info:

Always do a request.getParameter() to extract request parameters (i.e. data sent by posting a html form ). The request.getParameter() always returns String value and the data come from client.

Always use request.getAttribute() to get an object added to the request scope on the server side i.e. using request.setAttribute().

Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96