cdLegend
cdLegend

Reputation: 71

How do I call a Java method on button click event of JSP

<p>
    <form action="logout.java" method="post">
        <input type="button" value="Logout">
    </form>
</p>
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.sendRedirect("login.jsp");
}

but when i click the button nothing happens?

Upvotes: 1

Views: 15357

Answers (2)

user2121527
user2121527

Reputation: 19

Use this:

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.getRequestDispatcher("/login.jsp").forward(request, response);
 }

Upvotes: 0

Happy
Happy

Reputation: 1855

You have to use a submit input :

<input type="submit" value="Logout">

Make sure the servlet which contains the doPost() method is mapped to logout.java. If not, you have to set the action in your form tag.

Upvotes: 2

Related Questions