简体   繁体   中英

Passing parameter from JSTL to servlet through <a href> and jstl tags

So this is the code i'm using to send the parameter "idemp" that contains the value ${masession.idemp}

<a href="<c:url value="/consultertickets">
                <c:param name="idemp" value="${masession.idemp}"/>
         </c:url>">
<img src="<c:url value="/inc/liste.png"></c:url>" alt="consulter tickets" />
</a>

when redirected to the servlet "/consultertickets" the browser URL shows:

http://localhost:4040/monprojet2/consultertickets?idemp=64

so the parameter is passed and working but the method used to obviously GET and not POST, which is the method i'm using in the servlet, here's the servlet's code.

@WebServlet(urlPatterns= {"/consultertickets"})

public class ConsulterTickets extends HttpServlet {

private String VUE = "/WEB-INF/ListeTickets.jsp";

@EJB
private TicketDao ticketDao;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.getServletContext().getRequestDispatcher(VUE).forward(request, response);
}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    CreationTicketForm ticketform = new CreationTicketForm(ticketDao);
    List<Ticket> lticket = ticketform.recupererTickets(request);

    boolean resultat;
    if(lticket.isEmpty())
    {
        //resultat="Vous n'avez soumit aucun ticket";
        resultat = false;
        request.setAttribute("resultat", resultat);
        this.getServletContext().getRequestDispatcher("/ListeTickets2.jsp").forward(request, response);
    }else{
        //String VUE = "/ListeTickets.jsp";
        resultat=true;
        request.setAttribute("resultat", resultat);
        request.setAttribute("lticket", lticket);
        this.getServletContext().getRequestDispatcher(VUE).forward(request, response);
    }
}

}

is there any way to pass a parameter to a servlet through POST method, without going through the <form></form>

Solution 1:

Modifying doGet method

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //this.getServletContext().getRequestDispatcher(VUE).forward(request, response);
    doPost(request, response);
}

Solution 2:

Remove the doGet() and change doPost() to service()

Edit1:

See, Hyperlinks ( <a> tag) are meant to send GET request but not POST .

So, if you want to achieve sending POST request using Hyperlink there is no straight way. But, Javascript can be your help.

Using Javascript you can guide <a> to send POST request along with the help of <form> .

I just modified your code little bit. This should help you.

<a href="javascript:document.getElementById('form1').submit()">
    <img src="<c:url value="/inc/liste.png"></c:url>" alt="consulter tickets" />
</a>

<form action="<c:url value="/consultertickets"/>" method="post" id="form1">
    <input type="hidden" name="idemp" value="${masession.idemp}"/>
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM