How to use spring mvc controller make a post request? - spring-mvc

How to make a controller method to redirect to other site with a post request?
#RequestMapping("/link)
public RedirectView goToTheSite(ModelMap model) {
model.put("name", "wow");
return new RedirectView("https://www.thesite.com", true, false, false);
}
but, this is not working,
How to do that in right way?

How to make a controller method to redirect to other site with a post
request?
Redirect means you telling browser to make a GET request, There is no out of the box feature in Spring MVC to make POST request from controller.
How to do that in right way?
Use Apache HTTP client:
Request.Post("https://www.thesite.com/login")
.bodyForm(Form.form().add("username", "vip").add("password", "secret").build())
.execute().returnContent();

The only way to send a POST is to send a populated HTML form to the browser, with the action parameter pointing to the new site and method="POST".
<form name="myform" action="https://www.thesite.com" method="post">
<input name="name" type="hidden" value="true" />
<input name="phone" type="hidden" value="false" />
<input name="phone" type="hidden" value="false" />
<noscript>
<input type="submit" value="Click here to continue" />
</noscript>
</form>
Then Add javascript to automatically submit the form :
<script type="text/javascript">
$(document).ready(function() {
document.myform.submit();
});
</script>

Related

Form tag in html not working

I am not a skilled programmer and I am building a site using a third party's CMS. I have created a <form> in order to gather information and send it to an external site. My code doesn't work. The reason I am told is that the site is built on ASP.NET and as the 'master'page already has a <form> element in it and <form> elements cannot be embedded inside one another; so what I've written will never work. I have been advised to look for a java based solution, but I'm at a loss.
My present code is pretty basic...
<form action="http://ExternalWebSite.com" method="post" id="subForm">
<label for="name">Name:</label><br /><input type="text" name="name" id="name" /><br />
<label for="Email">Email Address:</label><br /><input type="text" name="email" id="email" /><br />
<label for="Dog name">Your dog's name:</label><br /><input type="text" name="dogname" id="dogname" /><br />
<label for="Town where you live">Town where you live:</label><br /><input type="text" name="town" id="town" /><br />
<input type="submit" value="Subscribe" />
</form>
How can I make this execute without using the tag inside my html code?
ASP.NET web forms adds one <form runat="server"> in your application. Therefore, you can't nest forms, but you can get multiple forms through a hack, mentioned here.
You can make an ajax call (post in this case) too to that url
$.ajax({
type: "POST",
url: "http://ExternalWebSite.com",
data: { name: $("#name").val(), $("#email").val(), $("#dogname").val(), $("#town").val() },
success: function(result){
//here you handle what to do after the post
}
});
For further reference on how to use jquery go to: This link The most imortant here is the ajax called which is foinf to make a post the specified utl sending the named parameters,the success function which will receive anything you write on the response on the url you are calling, and the selectors $("#name").val() which means "give me the value of the element with the id name"

asp.net add value to form method

Basically I want to place a url in the form action method.
The ltlUrl.text value is added on the server side in pageLoad.
How do i use the ltlUrl as the action method?
<form action="<%# ltlurl.text%>" enctype="multipart/form-data">
<input type="text" name="title" value="test" />
<input type="file" name="file" />
<input type="submit" />
</form>
Since you are populating the text control on code behind, you can simply do this:
this.Page.Form.Action = "http://someour";
I believe the VB.NET syntax would be:
Me.Page.Form.Action = "http://someurl"

Dynamic HTTP POST instead of form action in asp.net

I have a simple asp.net page where a form action is done, which it takes to the 3d party url and that will return some data as a response. How can I achieve the job done without using static form action.
Below is the form action:
<form name="theForm" method="GET" action="page.aspx" >
<input type="hidden" name="asp" value="hidden values" />
<input type="hidden" name="url" value="http://www.google.com" />
<input type="submit" name="submit" />
</form>
Thanks in Advance.

login form with https and http

i want to use https only for login form and after the user logs in send it to http .
login form is in page "a.php" and the action of login form is "b.php" .
my question is:
which one should i call: https://mysite.com/a.php or http://mysite.com/a.php
the action should be : https://mysite.com/b.php or http://mysite.com/b.php
i mean post data is send according to form page or action page protocol??
The page with input fields (a.php) should be on HTTP, while the page with authentication logic (b.php) should be on HTTPS.
In a.php there will be something like this:
<form method="post" action="https://[domain-name]/b.php">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" value="login" />
</form>

how can we cal two servlets from the same html page?

I need to design a page to register as well as login a user.By clicking login button login servlet should be called.And on clicking register button,register servlet will be called.how can i do this?
Having two forms that post to different places is the simplest method
Name your buttons:
<input type="submit" name="form_action" value="Login" />
and
<input type="submit" name="form_action" value="Register" />
When it comes to processing the form, just hookout form_action and it should equal Login or Register.
This requires some more server-side logic but should work if you need your two forms to be tightly combined.
You can include some Javascript code for this.
<form name="Form1" method="post">
Your Name <input type="text" name="text1" size="10" /><br />
<INPUT type="button" value="ButtonLogin" name=button1 onclick="return OnButtonLogin();">
<INPUT type="button" value="ButtonRegister" name=button2 onclick="return OnButtonRegister();">
</form>
like this:
<script language="Javascript">
<!--
function OnButtonLogin()
{
document.Form1.action = "Login.do"
document.Form1.target = "_blank";
document.Form1.submit();
return true;
}
function OnButtonRegister()
{
document.Form1.action = "Register.do"
document.Form1.target = "_blank";
document.Form1.submit();
return true;
}
-->
</script>

Resources