通过<a href=""> and jstl tags

时间:2017-05-02 12:11:47

标签: java jsp servlets jstl href

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>

1 个答案:

答案 0 :(得分:1)

解决方案1:

修改doGet方法

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

解决方案2:

删除doGet()并将doPost()更改为service()

<强> EDIT1:

请注意,Hyperlinks<a>代码)用于发送 GET 请求,但不是 POST

因此,如果您希望使用超链接发送 POST 请求,则没有直接的方法。但是,Javascript可能是你的帮助。

使用Javascript,您可以在<a>的帮助下指导<form>发送 POST 请求。

我刚刚修改了你的代码。这应该对你有帮助。

<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>