zoukankan      html  css  js  c++  java
  • 记账本开发记录——第十二天(2020.1.30)

    今天,学习了HTTP协议的相关内容和Servlet的运行原理。HTTP协议,全称叫做超文本传输协议,是一种网络协议,用来定义WEB浏览器与WEB服务器之间交换数据的过程。

    在HTTP协议中,分为HTTP请求和HTTP响应(request和response)在HTTP请求中,根据请求方式的不同,内容也有所不同。对于get请求,会将内容放在浏览器URL内,不如post请求安全。post请求会把内容放到request内,而不会显示在浏览器URL内,相对来说要更加的安全。

    在请求中有很多固定的参数,作为了解。如Referer(浏览器通知服务器当前请求来自何处),If-Modified-Since(浏览器通知服务器本地缓存的最后变更时间),Cookie(与会话有关技术,用于存放浏览器缓存的cookie信息).

    HTTP响应中则有一些比较重要的信息,如状态码:200:请求成功,302:请求重定向,304:请求资源没有改变,访问本地缓存,404:请求资源不存在,500:服务器内部错误。通常配合这些状态码来完成页面的跳转等操作。

    WEB开发,是用的是B/S通信模式,即使用浏览器的方式来和服务器进行交互。

    对于Servlet来说,Servlet需要编写实现类,并且在3.0以上版本也可以不用在web.xml进行配置,直接使用@webServlet来进行配置,更加方便快捷。实现类通常继承HttpServlet类,并且复写doGet和doPost方法。下面是一个小demo:

    package com.itheima.login;
    
    import java.io.IOException;
    import java.sql.SQLException;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.commons.dbutils.QueryRunner;
    import org.apache.commons.dbutils.handlers.BeanHandler;
    
    import com.itheima.domain.User;
    import com.itheima.utils.DataSourceUtils;
    
    public class LoginServlet extends HttpServlet {
        
        @Override
        public void init() throws ServletException {
            //在Seveltcontext域中存一个数据count
            int count = 0;
            this.getServletContext().setAttribute("count", count);
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            
            
            
            //username=zhangsan&password=123
            
            //1、获得用户名和密码
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            
            //2、从数据库中验证该用户名和密码是否正确
            QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
            String sql = "select * from user where username=? and password=?";
            User user = null;
            try {
                user = runner.query(sql, new BeanHandler<User>(User.class), username,password);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            
            //3、根据返回的结果给用户不同显示信息
            if(user!=null){
                //从servletcontext中取出count进行++运算
                ServletContext context = this.getServletContext();
                Integer count = (Integer) context.getAttribute("count");
                count++;
                //用户登录成功
                response.getWriter().write(user.toString()+"---you are success login person :"+count);
                context.setAttribute("count", count);
            }else{
                //用户登录失败
                response.getWriter().write("sorry your username or password is wrong");
            }
            
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doGet(request, response);
        }
    }
  • 相关阅读:
    网上搜索整理的前端开发工程师面试题附答案
    Python-RabbitMQ-topic(细致消息过滤的广播模式)
    Python-RabbitMQ-direct(广播模式)
    Python-RabbitMQ-fanout(广播模式)
    rabbitmq中关于exchange模式type报错
    Python-RabbitMQ(持久化)
    Python-RabbitMQ(简单发送模型)
    python-gevent模块实现socket大并发
    python-gevent模块(自动切换io的协程)
    python-greenlet模块(协程)
  • 原文地址:https://www.cnblogs.com/wushenjiang/p/12243248.html
Copyright © 2011-2022 走看看