如何重定向提交按钮另一个servlet

时间:2015-10-20 19:40:03

标签: java forms servlets

所以我正在为一个网络应用程序创建一个注册页面。但是,我无法通过提交按钮重定向到确认页面。注册servlet接收用户的信息,所有确认servlet只显示用户的名字并打印出来#34; Welcome(用户名)"
这是我的代码:
注册servlet

Sub Pi()

    Dim enne As Variant, phi As Variant
    phi = CDec(1)

    enne = 100000000

    For i = 0 To enne
        phi = 6 + ((2 * enne + 1) * (2 * enne + 1)) / phi
        enne = enne - 1
    Next
    phi = phi - 3

    Debug.Print " 3.14159265358979323846264338327950288419716"
    Debug.Print phi

End Sub


确认servlet

package register.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
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("/Register")
public class Register extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Register() {
        super();
    }

    public void init() throws ServletException {
        ArrayList<HW1User> HW1Users = new ArrayList<HW1User>();

        getServletContext().setAttribute("HW1Users", HW1Users);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");

        PrintWriter out = response.getWriter();
        String docType = "<!doctype html public \"-//w3c//dtd html 4.0 transitional//en\">\n";

        out.println(docType);
        out.println("<html>" + "<head>" + "<meta charset=\"UTF-8\">" + "<title>Register</title>"
                + "<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css\">"
                + "</head>" + "<body>" + "<div class=\"container\">" + "<div class=\"jumbotron\">"
                + "<h1>Welcome to Bat!</h1>"
                + "<p>Bat! is a cloud-based note taking app that allows you to create, store, edit, and share notes that you create directly in your browser!</p>"
                + "<p>To begin, register below.</p>" + "</div>"

                + "<div class=\"panel panel-primary\">" + "<div class=\"panel-heading\">"
                + "<h3 class=\"panel-title\"> New User Registration</h3>"

                + "</div>" + "<div class=\"panel-body\">" + "<form>" + "<div class=\"form-group\">"
                + "<label for=\"name\">Full Name</label>"
                + "<input type=\"name\" class=\"form-control\" name=\"fullName\" id=\"fullName\" placeholder=\"First and Last Name\">"

                + "</div>"

                + "<div class=\"form-group\">" + "<label for=\"email\">Email Address</label>"
                + "<input type=\"email\" class=\"form-control\" name=\"email\" id=\"email\" placeholder=\"Email\">"

                + "</div>" + "<div class=\"form-group\">" + "<label for=\"password1\">Password</label>"
                + "<input type=\"password\" class=\"form-control\" name=\"password1\" id=\"password1\" placeholder=\"Password\">"

                + "</div>" + "<div class=\"form-group\">" + "<label for=\"password2\">Re-Enter Password</label>"
                + "<input type=\"password\" class=\"form-control\" name=\"password2\" id=\"password2\" placeholder=\"Re-Enter Your Password\">"

                + "</div>"

                + "<button type=\"submit\" value =\"Post\" class=\"btn btn-primary\">Register</button>" + "</form>"
                + "</div>" + "</div> <!--  End Well -->" + "</div>" + "</body>" + "</html>");


    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        int id = Integer.parseInt(request.getParameter("id"));
        ArrayList<HW1User> HW1Users = (ArrayList<HW1User>) getServletContext().getAttribute("HW1Users");

        for (HW1User entry : HW1Users)
            if (entry.getId() == id){
                entry.fullName = request.getParameter("fullName");
                entry.email = request.getParameter("email");
                entry.password1 = request.getParameter("password1");
                entry.password2 = request.getParameter("password2");
                break;
            }

        ServletContext context = getServletContext();
        RequestDispatcher rd = context.getRequestDispatcher("/Confirm");
        rd.forward(request, response);
        doGet(request, response);
    }

}


1 个答案:

答案 0 :(得分:0)

如果您想对post方法执行某些操作,则应将method="post"添加到form标记。