会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132454个问题

from typing import Mapping
import pymysql


class DBUtil :
    config ={
        'host':'localhost',
        'user':'root',
        'password':'root',
        'db':'text01',
        'charset':'utf8'
    }
    def __init__(self):
        self.connection = pymysql.connect(**DBUtil.config)
        self.cursor = self.connection.cursor()
    def close (self):
        if  self.cursor:
            self.cursor.close()
        if self.connection:
            self.connection.close()
    #插入数据,更新数据,删除数据
    def exedml(self,sql,*args):
        try :
            count = self.cursor.execute(sql,args)
            self.connection.commit()
            
        except Exception as e :
            print(e)
            if self.connection:
                self.connection.rollback()
        finally :
            self.close()

    def query_one (self,sql,*args):
        try :
            count = self.cursor.execute(sql,args)
          
            return self.cursor.fetchone()
            
        except Exception as e :
            print(e)
            if self.connection:
                self.connection.rollback()
        finally:
            self.close()
    def query_all(self,sql,*args):
        try :
            self.cursor.execute(sql,args)
            emp = self.cursor.fetchall()
            return emp 
        except Exception as e :
            print(e)
            if self.connection:
                self.connection.rollback()
        finally:
            self.close()
if __name__ == '__mian__':
    dbutil = DBUtil()
    #sql = "insert into emp (empno,ename,sal) values(%s,%s,%s)"
    #dbutil.exedml(sql,9999,'lili',12000)
    sql = "select * from emp"
    emps = dbutil.query_all(sql)
    for e in emps:
        print(e,end="\n")
    

为什么运行完啥都没有


Python 全系列/第五阶段:数据库编程/python操作mysql(旧) 16486楼

from flask import Flask,request,render_template

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'

@app.route('/article/<id>/')
def list(id):
    print(id)
    return 'success your id is %s'%id

@app.route('/article2/<uuid:id>/')
def list2(id):
    print(id)
    return 'success your id is %s'%id
# import uuid
# print(uuid.uuid4())
#@app.route('/list7')#这种写法方式只支持get请求方式   不支持post请求方式
@app.route('/list7',methods=['GET','POST'])#这种写法方式支持get请求方式 支持post请求方式
def list7():
    if request.method=='GET':
        pwd = request.args.get('pwd')
        uname = request.args.get('uname')
        # return 'success: %s ,%s'%(uname,pwd)
        return render_template('login.html')
    elif request.method=="POST":
        uname=request.form.get('uname')
        pwd=request.form.get('pwd')
        return 'post请求成功: %s ,%s'%(uname,pwd)
if __name__ == '__main__':
    app.run(debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h4>登陆页面</h4>
    <form action="/list7" method="post"></form>
    用户名:<input type="text" name="uname"><br>
    密&nbsp;码:<input type="password" name="pwd"><br>
    <input type="submit" value="登录">
</body>
</html>

image.png

为什么点击登录没有反应

Python 全系列/第八阶段:轻量级Web开发利器-Flask框架/Flask视图基础和URL 16487楼
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 16488楼
JAVA 全系列/第九阶段:Spring Boot实战/Spring Boot 16489楼
JAVA 全系列/第六阶段:项目管理与SSM框架/SpringMVC 16491楼
Python 全系列/第一阶段:Python入门/序列 16492楼
WEB前端全系列/第二十阶段:Vue2企业级项目(旧)/易购商品后台管理系统 16493楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 16495楼
Python 全系列/第十阶段:Flask百战电商后台项目/Flask百战电商后台项目 16496楼
Python 全系列/第七阶段:网页编程基础/html5 16498楼
Python 全系列/第三阶段:Python 网络与并发编程/并发编程 16499楼

package WEB.web.filter;

import WEB.commons.Constants;
import WEB.pojo.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 判断当前客户端浏览器是否登录的 Filter
 */
@WebFilter(urlPatterns = {"*.do","*.jsp"})
public class UserLoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String uri = request.getRequestURI();
        StringBuffer url = request.getRequestURL();
        System.out.println("---------------" + uri);
        System.out.println("---------------" + url);
        //判断当前请求的是否为login.jsp 或者login.do,如果请求的是用户登录的资源那么需要放行。
        if (uri.indexOf("login.do") != -1 || uri.indexOf("login.jsp") != -1){
            filterChain.doFilter(servletRequest, servletResponse);
        }else{
            //不在登录页面,要进行操作验证是否有该对象
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute(Constants.USER_SESSION_KEY);
            //判断session中是否有该对象
            if (user != null){
                filterChain.doFilter(servletRequest, servletResponse);
            }else{
                //在客户端打印一句话
                request.setAttribute("mess","不登录不好使");
                //通过请求转发进行跳转
                request.getRequestDispatcher("login.jsp").forward(servletRequest, servletResponse);
            }
        }
    }

    @Override
    public void destroy() {

    }
}

我直接去访问的main.jsp页面,但是没有打印不登录不好使这句话怎么回事

JAVA 全系列/第五阶段:JavaWeb开发/Web实战案例 16500楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637