根据 HTML 页面操作重定向到其他 servlet

Redirecting to other servlet based on HTML page action

这里我有 2 个 servlet

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

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

}


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

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

}

还有一个 HTML 这样的页面。

<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>

我想在用户单击创建配置文件时重定向到创建 servlet。谁能帮我解决这个问题?

您有一个表单,您希望它转到两个不同的 servlet。你不能那样做。如果您想先登录 servlet,然后再创建 servlet。您可以查看 requestDispatcher 或 httpServletresponse.sendRedirect()

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

response.sendRedirect("/url");

或者,如果您根本不想进入登录 servlet 而直接想去创建,那么使用两种形式,一种用于您的登录,另一种用于您的创建配置文件。 <form name="loginForm" method="post" action="Create"> <tr> <td><input type="submit" onclick="return check(this.form)" value="Create profile"></td> </tr>

您需要select 根据点击的按钮执行操作。您可以使用 JavaScript 来完成。下面给出的是最小的工作示例,您可以根据需要对其进行扩展:

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);
    }
}