JavaWeb--ServletRequest和ServletResponse

 public void service(ServletRequest servletRequest, ServletResponse servletResponse)
            throws ServletException, IOException {

        //1.获取请求参数  获取到的是当前输入的用户名和密码
        String user = servletRequest.getParameter("username");
        String password = servletRequest.getParameter("password");

        //2.获取当前web应用的初始化参数,获取到的是web.xml中配置的用户名和密码
        //使用ServletContext对象
        ServletContext servletContext = servletConfig.getServletContext();
        String initUser = servletContext.getInitParameter("user");
        String initPassword = servletContext.getInitParameter("password");


        //3.对比结果输出
        PrintWriter printWriter = servletResponse.getWriter();
        if(user.equals(initUser) && password.equals(initPassword)){
            printWriter.print("Hello: " + user);
        }else{
            printWriter.print("Sorry: user is " + user + " initUser is " + initUser);
        }
    }

  

posted @ 2017-12-06 14:47  SkyeAngel  阅读(169)  评论(0编辑  收藏  举报