2014-12-30 2 views
1

Я хочу позвонить doLogOut метод сервлета post, но каждый раз, когда я вызываю сервлет, метод Get был вызван вместо метода post!Не удается вызвать метод передачи сервлета

Вот мой jsp:

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title> Checkout </title> 
    </head> 
    <body> 
     <form method="post" action="doLogOut"> 
      <p style="text-align: right;"> 
       <a href="doLogOut"> LogOut </a> 
      </p> 
     </form> 
     ... 
</html> 

А вот мой doLogOut сервлета:

@WebServlet(urlPatterns = {"/doLogOut"}) 
public class doLogOut extends HttpServlet { 

    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     response.setContentType("text/html;charset=UTF-8"); 
     PrintWriter out = response.getWriter(); 
     out.println("<html>"); 
     out.println("<head>"); 
     out.println("<title>Servlet doLogOut</title>"); 
     out.println("</head>"); 
     out.println("<body>"); 
     out.println("<h3> Get method: </h3>"); 
     out.println("</body>"); 
     out.println("</html>"); 
    } 

    @Override 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     response.setContentType("text/html;charset=UTF-8"); 
     PrintWriter out = response.getWriter(); 
     out.println("<html>"); 
     out.println("<head>"); 
     out.println("<title>Servlet doLogOut</title>"); 
     out.println("</head>"); 
     out.println("<body>"); 
     out.println("<h3> Post method: </h3>"); 
     out.println("</body>"); 
     out.println("</html>"); 
    } 
} 

Но я вижу Get method: сообщений в результате чего?

ответ

1

Нажав на кнопку <a href="..." />, вы выполняете запрос GET по умолчанию. Вы должны либо отправить свою форму, либо выполнить запрос ajax с помощью метода POST. Вот пример, чтобы описать идею:

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
     <title> Checkout </title> 
     <script type="text/javascript"> 
     function logoutWithAjax() { 
      $.ajax({ 
       type: "POST", 
       url: "doLogOut", 
       success: function() { 
       console.log("Logged out"); 
       } 
      }); 
     } 
     </script> 
    </head> 
    <body> 
     <form id="form" method="post" action="doLogOut"> 
      <p style="text-align: right;"> 
       <a href="javascript:{}" onclick="document.getElementById('form').submit();"> LogOut with Form Submit</a> 
       <a href="javascript:{}" onclick="logoutWithAjax();"> LogOut with Ajax Request</a> 
      </p> 
     </form> 
    </body> 
</html> 
1

Вы можете попробовать использовать кнопки Bootstrap в классе btn-link для того, чтобы использовать ваши кнопки отправки в качестве ссылки.

<form method="post" action="doLogOut"> 
    <p style="text-align: right;"> 
      <button type="submit" class="btn-link"> LogOut </button> 
    </p> 
<form> 
Смежные вопросы