0

I'm having trouble to submit the form by clicking button in another form.When i hit the button,it is not redirected to the action servlet, but it redirects into the same page,with a url like this,

http://localhost:8080/OnlineDD/index.jsp/login.jsp?

with a '?' at the end.This method was working in my sample project.

HTML code

<form autocomplete="off">
    <label>Email</label>
    <input autocomplete="off" autofocus type="text" required="" id="emailIDForm1">

    </label>
    </div>
    <label>Password</label>
    <input autocomplete="off" type="password" min="6" required="" id="logPassId" />

    </div>
    <div>
        <button id="submitForm" class="btn btn-primary">LOGIN</button>
    </div>
    </div>
</form>
<form action="LoginServlet" method="post" id="SecFormLogin" style="display:none">
    <input type="text" name="emailId" id="emailIdForm2" />
    <input type="password" name="password" id="passForm2" />
</form>

and Jquery code

<script>
    $(document).ready(function() {
        $('#logPassId').change(function() {
            var password= $(this).val();
            $('#passForm2').val(password );
        });
        $('#emailIDForm1').change(function() {
            var email= $(this).val();
            $('#emailIdForm2').val(email);
        });
        $("#submitForm").click(function() {
            $("#SecFormLogin").submit();
        });
    });
</script>

and Servlet code is,

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    System.out.println("Called post");}
Amal lal T L
  • 400
  • 5
  • 20
  • This approach makes no sense. Check section "Ajaxifying an existing form" in the above linked duplicate – BalusC Aug 14 '17 at 06:28
  • Iam not intented to use Ajax,it is just a part of my project(for encryption for password).My intention is as per the question and is different from the duplicated,it is to make the action of another form on button click from another form.Thankyou for noticing this question. – Amal lal T L Aug 14 '17 at 06:32
  • I made it correct.It was to use `` instead of `` – Amal lal T L Aug 14 '17 at 07:10

1 Answers1

0

You need to have Deployment Descriptor (web.xml)

<servlet>
  <servlet-name>servletName</servlet-name>
  <servlet-class>packageName.servletName</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>servletName</servlet-name>
  <url-pattern>/yourServletUrl</url-pattern>
</servlet-mapping>

Or you may use annotations in your Servlet since servlets 3.0.

import javax.servlet.annotation.WebServlet;

@WebServlet(name = "servletName", urlPatterns = { "/yourServletUrl" })
public class servletName extends HttpServlet {
Alexei Petrov
  • 331
  • 5
  • 14