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?
show me something Calls the default d...">
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?
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.
onClick
event for an item? - Anton MukhinHere 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>
Source: https://ru.stackoverflow.com/questions/65237/
All Articles