简体   繁体   中英

Redirecting to other servlet based on HTML page action

Here i've have 2 servlets

@WebServlet("/Login")
public class Login extends HttpServlet {

.........
.........

}


@WebServlet("/Create")
public class Create extends HttpServlet {

.........
.........

}

And a HTML page like this.

<form name="loginForm" method="post" action="Login">
  <table width="20%" bgcolor="0099CC" align="center">
    <tr>
      <td colspan=2>
        <center>
          <font size=4><b>HTML Login Page</b></font>
        </center>
      </td>
    </tr>
    <tr>
      <td>Username:</td>
      <td><input type="text" size=25 name="username"></td>
    </tr>
    <tr>
      <td>Password:</td>
      <td><input type="Password" size=25 name="password"></td>
    </tr>
    <tr>
      <td><input type="submit" onclick="return check(this.form)" value="Login"></td>
    </tr>
    <tr>
      <td><input type="submit" onclick="return check(this.form)" value="Create profile"></td>
    </tr>

  </table>
</form>

I want to redirect to Create servlet when the user clicks on create profile. Can anyone help me with this?

You have one form and you want it to go to two different servlet. You can't do that. If you want to goto login servlet first and then goto create servlet. You can do that view requestDispatcher or httpServletresponse.sendRedirect()

RequestDispatcher rd=request.getRequestDispatcher("servlet Name")
rd.forward(request, response);

or by

response.sendRedirect("/url");

Or if you dont want to go the login servlet at all and directly want to go to create then use two forms one for your Login and another one for your Create Profile. <form name="loginForm" method="post" action="Create"> <tr> <td><input type="submit" onclick="return check(this.form)" value="Create profile"></td> </tr>

You need to select the action based on the button which is clicked. You can do it using JavaScript. Given below is the minimal, working example which you can extend as per your requirement:

HTML

<!DOCTYPE html>
<html>
    <head>
    <meta charset="UTF-8">
    <script>
        function check(form, button){
            if(button.id=='login'){
                form.action="Login";
            }else if(button.id=='create'){
                form.action="Create";
            }
            form.submit();
        }
    </script>
    <title>Insert title here</title>
    </head>
    <body>
        <form name="loginForm" method="post">
            <table width="20%" bgcolor="0099CC" align="center">
                <tr>
                    <td colspan=2>
                        <center>
                            <font size=4><b>HTML Login Page</b></font>
                        </center>
                    </td>
                </tr>
                <tr>
                    <td>Username:</td>
                    <td><input type="text" size=25 name="username"></td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><input type="Password" size=25 name="password"></td>
                </tr>
                <tr>
                    <td><input type="button" id="login" onclick="check(this.form,this)"
                        value="Login"></td>
                </tr>
                <tr>
                    <td><input type="button" id="create" onclick="check(this.form,this)"
                        value="Create profile"></td>
                </tr>

            </table>
        </form>
    </body>
</html>

Login.java

package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Login")
public class Login extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("Login");
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}

Create.java

package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Create")
public class Create extends HttpServlet{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("Create");
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM