我使用 JQuery 以 json 格式将数据从 login.html
发送到 servlet,匹配记录后我想打开一个新页面 home.html
我还想将数据发送到 home.html
:
DTORegisteration dto=new DTORegisteration();
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
String id = req.getParameter("id");
String password = req.getParameter("password");
System.out.println("id "+id);
System.out.println("password "+password);
dto.setId(id);
dto.setPassword(password);
String str=new ServiceRegisteration().getDetails(dto);
JSONObject json=new JSONObject();
if(str.equals("success")) {
try { json.put("welcome", "welcome"); json.put("name",
DAORegisteration.name);
}catch(JSONException e) { e.printStackTrace(); }
pw.print(json);
}
}
最佳答案
HttpServletResponse 接口(interface)的 sendRedirect() 方法可用于将响应重定向到另一个资源,该资源可以是 servlet、jsp 或 html 文件。
if(check your param values here) { // if its as expected
res.sendRedirect("home.html");
}
实际上 我们无法使用 sendRedirect() 方法发送 post 请求,因为根据标准重定向应该使用 get。
您可以使用RequestDispatcher类来转发带有参数的请求,
例如-
req.setAttribute("Greetings", "Greeting of day");
RequestDispatcher dispatcher = servletContext().getRequestDispatcher("url");
dispatcher.forward(req, res);
关于html - 如何从servlet重定向到新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54824803/