-1

How can I enable and disable a link based on some value match in a JSP page. So I have a column called person having values in as A, T, L etc. and based on each value a link in JSP page needs to get disabled and enabled say-

I wanted to disable a settings link on my JSP page if the value from Servlet response is A or enable if the value from servlet is T

Servlet code-

public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
HttpSession session = req.getSession(true);
String person= profile.getPerson(); // person = 'A' (assume)
req.setAttribute("pers", person); // 'A' is sent to JSP
this.getServletContext().getRequestDispatcher( "/myPage.jsp" ).forward( req, response );
}

ProfileVO.java--

@Entity
@Table(name = "profile")

public class ProfileVO implements Serializable {
 private static final long serialVersionUID = 1L;
   @NotNull
   private String person;
   private String email;

   public String getPerson() {
        return person;
    }

    public void setPerson(String person) {
        this.person = person;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("Profile")
                .append(", email='").append(email).append('\'')
                .append(", person='").append(person).append('\'')
                .append('}');
        return sb.toString();
    }

HTML code--

<div id="header">
    <p><a href="/test.jsp">Settings</a> | Terms & Conditions </p>
</div>

In HTML code, I needed to enable/disable the link on settings based on the value I get from a servlet.

How should I do that using JQuery?? or is there any other way I can check value in JSP or Jquery to disable or enable a settings link as soon as a person logs in?

AKIWEB
  • 19,008
  • 67
  • 180
  • 294
  • what else Servlet code is doing here? How this servlet is called from client side? What is the flow? – Braj Jun 12 '14 at 20:50
  • This question answers it I think. Just add into your links a class like class="disabled", and then make a selector like$("a .disabled") and follow the example on the link. http://stackoverflow.com/questions/3788946/how-do-i-dynamically-enable-disable-links-with-jquery – Gyhth Jun 12 '14 at 20:53
  • is the servlet the controller for the JSP or something called by Ajax? – developerwjk Jun 12 '14 at 20:54
  • @developerwjk, yes! Do you want me to write the whole method in a servlet? even the forwarding to my testAB.jsp? – AKIWEB Jun 12 '14 at 20:58
  • @akiiddweeber, That wasn't really a yes or no question. – developerwjk Jun 12 '14 at 21:00
  • @akiiddweeber you haven't answered my question. What is the flow of the application? – Braj Jun 12 '14 at 21:08
  • Servlet is passing the value to the JSP page which it retrieves from the database. So if you look at my code it only gets the value from the VO which is a class that stores a table data, and I am passing it to JSP in order to put a check point if the value is this (which is passed) do this behavior else do this. either, using JQuery or JSTL/JSP. I am simply figuring it out the better way to do this. – AKIWEB Jun 12 '14 at 21:56

2 Answers2

0

This can be done better from server side in your JSP page, using java scriplets.

Since you can access the HttpServletRequest being sent to the "myPage.jsp" JSP page from the RequestDispatcher, you can access the "pers" parameter and use it in a scriplet:

<div id="header">
    <p><a href="<%= request.getParameter("pers").equals("A")? "/test.jsp": "javascript:void(0);" ">Settings</a> | Terms & Conditions </p>
</div>

In this scriplet, you can see it checks the pers parameter and if it equals "A", It adds the correct "/test.jsp" value to the href attribute. Otherwise, It adds a "javascript:void(0);" placeholder so nothing happens when the user clicks the link.

Roberto Linares
  • 2,215
  • 3
  • 23
  • 35
0

Change the HTML like this (I hope its a JSP file)

<p><a href=<%=((String)req.getAttribute("pers")).equals("A")?"'/test.jsp'":"'javascript:;'"%>>Settings</a> | Terms & Conditions </p>

Or

<%  String pers = (String)req.getAttribute("pers"); %>
<a href=<%=pers.equals("A")?"'/test.jsp'":"'javascript:;'"%>>Settings</a> | Terms & Conditions </p>
Teddy
  • 4,009
  • 2
  • 33
  • 55