html - 如何从servlet重定向到新页面

标签 html mysql database servlets servlet-3.0

我使用 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/

相关文章:

database - 导出时生成的 postgreSQL 转储文件为空

ruby-on-rails - 添加继承后检索现有的 MongoDB 集合

database - Facebook 数据库设计可能应该是什么

html - 为什么 Bootstrap 导航栏没有出现在这个 Ruby on Rails 页面上?

javascript - 隐藏 <Div> 和子标签

html - Bootstrap 输入组内数字输入的溢出宽度

php - 在新窗口中打开 index.php?id=2

MySQL错误您无法在FROM子句中指定更新目标表

javascript - 在生成的 HTML 中注入(inject)或初始化 Vue 组件

php - 如何使用 Ajax、PHP 和 JQuery 检查多个输入复选框?