从servlet读取响应到html页面

时间:2016-03-22 12:33:46

标签: html servlets

以下是我的要求

  1. Login.html页面和用户名和密码文本字段,格式为
  2. Login.html - >提交btn - >调用LoginServlet。
  3. LoginServlet - >来自DB的验证用户
  4. 成功 - >调用DashboardServlet和DashboardServlet - >致电Dashboard.html。
  5. 如何在Dashboard.html上阅读响应对象[我在这里从DashboardServlet传递用户名]。
  6. 如果在步骤4中失败 - > LoginServlet将调用Login.html - >再次如何阅读此页面上的响应对象,让我说我要提醒(用户身份验证失败)
  7. LogingServlet

        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("I am in doPost of LoginServlet");
        String userName = request.getParameter("username");
        String passWord = request.getParameter("password");
    
        if(LoginDao.validate(userName, passWord))
        {
            HttpSession session=request.getSession();  
            if(session.isNew())
               {
                //New session creation
                  session=request.getSession(true);
               }
                else
                {
                   session = request.getSession(false);
                }
    
             //setting attribute on session
             session.setAttribute("userName",userName);
             //send request to Welcome.jsp page
             //response.sendRedirect("/Dashboard.html");
             response.sendRedirect("/zmcwebadmin/dashBoardServlet");
             response.getWriter().write(String.valueOf(userName));
             System.out.println("I have connected to DB");
        }
    
        else
        {
            //response.sendRedirect("error.html");
            String data = "User name and password do not match";
            response.setContentType("text/plain");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(data);
            /*RequestDispatcher rd = request.getRequestDispatcher("Loginpage.html"); -- This solution works
            rd.forward(request, response);*/
            //response.sendRedirect("Loginpage.html");
            request.setAttribute("error", "Unknown login, try again");
            response.sendRedirect("/zmcwebadmin/html/Login.html");
            //doGet(request, response);
    
            System.out.println("I have not connected to server");
           }
          }
    

    DashBoardServlet

        /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
    
    
        System.out.println("I am in doGet of dashBoardServlet");
        //New writing on 19th March where initial control given to servlet
    
            HttpSession session = request.getSession(false); 
            if(session.getAttribute("userName") == null) //session is not created
                response.sendRedirect("/zmcwebadmin/html/LoginPage.html");
            else
                response.sendRedirect("/zmcwebadmin/html/Dashboard.html");
    }
    
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
    
        System.out.println("I am in doPost of dashBoardServlet");
        String scannerState = request.getParameter("scannerState");
        System.out.println("Dashboard servlet called, scannerState: " + scannerState);
        System.out.println("I am in dashBoardServlet");
        ZebraDbDao zebraDbDao = new ZebraDbDao();
        List<Scanner> scannerList;
    
        try {
            scannerList = zebraDbDao.getScannerDetail(scannerState);
            System.out.println("I got the scanner list");
            String json = new Gson().toJson(scannerList);
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(json);
        } catch (SQLException e) {
    
            e.printStackTrace();
        } // online scanner
    }
    

    Dashboard.html

       <script>
    
      function changeLogoutImage() {
        var image = document.getElementById('logoutbtn'); /* function for logout image*/
        if (image.src.match("_selected")) {
            image.src = "../Images/logout_deselected.png";
        } else {
            image.src = "../Images/logout_selected-18.png";
        }
    }
    
       $(document).ready(function(){ 
       $('#form_logout').submit(function()  
      {
        $form = $(this);
    $.post($form.attr('action'), $form.serialize(),  function(responseText)  {
    $('#usenameText').text(responseText);                                    
            });
            return false;                                                            
        });
    });
    

     </head>
     <body background="../Images/zebra_background.jpg">
     <div id="header"> ZMC Server <img src="../Images/zebra_logo.png" width=150px height=50px
            style="float: right;  padding-top:15px;">
    
        <form id="form_logout" action="/zmcwebadmin/logoutServlet" method=post>
    
            <input type="image" class="logbuttonformat"
                     id="logoutbtn"
                    src="../Images/logout_deselected.png" 
                    onclick="changeLogoutImage()" alt="submit">
        </form>
        <img id="changepas" src="../Images/change_pswrd_deselected.png"
            onclick="window.location.href='../html/ChangePassword.html'"
             style="float: right"> 
             <font
            style="color: #ffb911; font-size: 9.3pt; font-family: 'Roboto(Medium)', sans-serif; float: right; padding: 6px">
            USERNAME </font> 
            <img id="usernammeicon" onclick="changeUserImage()"
            src="../Images/username_icon.png" style="float: right">
    </div>
    </body>
    </html>
    

0 个答案:

没有答案