The question has arisen, the reference is

<a href="myservlet?command=mycommand">show me something</a> 

Calls the default doGet method. Is it possible to change it to the doPost ? And if so, what will the link code look like?

  • Call the JavaScript method that will send what you need in the form that you need. - Anton Mukhin
  • and what's the problem instead of a link to put the usual form. for this, even javascript'a do not need to write 2-3 lines of html. but Mukhin deserves respect, he tried :) - jmu
  • 2
    The question is generally caused by ignorance of how the Internet works. Understand what GET and POST are, and what links, browser and HTML are. And then do Java and servlets. And then there will be no phrases like "a href calls the doGet method", and yes even by default. - cy6erGn0m

2 answers 2

 function postToUrl(path, params, method) { method = method || "post"; // Устанавливаем метод отправки. var form = document.createElement("form"); form.setAttribute("method", method); form.setAttribute("action", path); for(var key in params) { var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", key); hiddenField.setAttribute("value", params[key]); form.appendChild(hiddenField); } document.body.appendChild(form); form.submit(); } 

UPD1 :
I forgot to leave a call example:

 postToUrl('путь', {'parameter' : 'parameter value'}, 'POST') 

UPD2 :
Tax. I have the feeling that you have very little idea what the client side and the HTML page are.
So. The server part is nothing to do with when the task is sent to the server from the client request. Your HTML or 'JSP' page may contain HTML code (as you know it well), as well as JavaScript code. I can not describe here the basics of his work and the basics of dynamic 'HTML' or whatever they call it. I will give just an example of use.

 <head> <script type="text/javascript"> // Сюда ставите код, который я написал первым </script> </head> <body> <a href="#" onclick="postToUrl('myservlet', {'command':'mycommand'}, 'POST'");"> </body> 

Something like that. Well, you can see information about dynamic HTML, JavaScript. Without this, sites can no longer do today.

  • Can you set an onClick event for an item? - Anton Mukhin
  • beautiful code, only it is not java code. It is possible in more detail what it is and where it should be? I do not know how to set events on onclick. Only recently began to study. - Viacheslav
  • ))) Yes! This is the JavaScript code that occurs in the browser. And you need to link to it? Maybe an example with a button to give? - Anton Mukhin

Here is an example servlet:

 package edu.jtc.servlet; import javax.servlet.http.HttpServlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HandlerServlet extends HttpServlet{ 

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

  String username; PrintWriter output = response.getWriter(); username=request.getParameter("username"); if (!username.equals("")) { output.print("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">" + "<html><head><title>Hello</title>" + "<link rel=\"stylesheet\" type=\"text/css\" href=\"s/form-style.css\"/>" + "</head><body><h1>Hello</h1>" +"<p>Hello, "+username+"!!!</p></body></html>"); }else{ FormServlet serv=new FormServlet("You don't enter username"); serv.doGet(request, response); } } 

}

in web.xml write:

 <servlet> <servlet-name>authentication-handler-form</servlet-name> <servlet-class>edu.jtc.servlet.HandlerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>authentication-handler-form</servlet-name> <url-pattern>handler</url-pattern> </servlet-mapping> 

Here is the challenge:

 <form method="POST" action="handler"> <input type="submit" value="Submit"> </form>