0
所以我正在爲一個Web應用程序註冊頁面。但是,我無法將提交按鈕重定向到確認頁面。註冊的servlet需要在用戶的信息和所有的確認的servlet也只是顯示了用戶的姓名,並打印出「歡迎(用戶名)」
這裏是我的代碼:
寄存器的servlet如何重定向提交按鈕另一個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);
}
}
確認的servlet
package register.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
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("/Confirm")
public class Confirm extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HW1User entry = null;
try{
int id = Integer.parseInt(request.getParameter("id"));
ArrayList<HW1User> HW1Users = (ArrayList<HW1User>) getServletContext().getAttribute("HW1Users");
for (HW1User e : HW1Users){
if (e.getId() == id){
entry = e;
break;
}
}
}catch(Exception e){}
finally{
if (entry == null){
response.sendRedirect("Register");
return;
}
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println(" <meta charset=\"UTF-8\">");
out.println(" <title>Cervantes, Albert - cs320stu100</title>");
out.println(" <link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css\">");
out.println("</head>");
out.println("<body>");
out.println("<div class=\"container\">");
out.println("Welcome " + entry.getName() + " !!");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
我建議將[servlets]標籤懸停在您的問題下方,直到黑色彈出窗口出現,然後單擊其中的「信息」鏈接。 – BalusC