2022-09-03 第二组刘禹彤 学习笔记
打卡46天
###学习内容
MVC架构
是一种软件架构模式,把整个软件分为三层: Model、 View、 Controller
Model(模型)
获取数据,并且处理数据,返回给controller。
entity(数据库实体类)
User----user表
service(业务控制层)
其余的活都交给service
dao(数据模型层)
操作数据库,执行sql
View(视图)
看见的页面,渲染数据,渲染页面。
Controller(控制器)
servlet,接请求,给响应。.
耦合度
代码之间的关联关系。
为什么要分层?
MVC,降耦合。重用性高。可维护性高。
调用关系
- View层发起请求---Controller--- Service---Dao---Service---Controller---View
- 导入jar包,导入js文件, JDBC工具类,字符集的过滤器
- 一个servlet,可以处理多个post请求---------利用反射
@WebServlet("*.do")
public class VipController extends HttpServlet {
private VipService vipService = new VipServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String servicePath = req.getServletPath();
String methodName = servicePath.substring(1);
methodName = methodName.substring(methodName.indexOf("/")+1,methodName.length()-3);
try {
Method method = getClass().getDeclaredMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
try {
method.invoke(this,req,resp);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
- MVC设计模式理念: 一张表,一 个entity, 一个service, 一个dao, 一个controller
加密加盐
public class MD5Util {
/*
* 获取用来加密的盐
* */
public static String getSalt(){
//从定义的一组数据中拿出几个字母或者数字或者符号当作盐
String words = "abcdefghijklmn123456*(&^%+@!";
StringBuilder strb = new StringBuilder();
//从定义的数据中取出8个字符,当作盐
for (int i = 0; i < 8; i++) {
/*随机取
* 生成一个0-字符串的长度的随机数[0,长度)*/
strb.append(words.charAt((int) Math.floor(Math.random()*words.length())));
}
return strb.toString();
}
/*这个方法参数是原始密码,controller通过request.getParameter("")写在密码框中的密码
* 返回值是经过加密处理过后的密码
* 加盐salt
* MD5+盐 加密
* MD5算法*/
public static String stringToMD5(String str){
return DigestUtils.md5Hex(str.getBytes());
}
}
密码加密加盐处理
public int register(Vip vip) {
try {
//密码加密处理
//生成盐
String salt = MD5Util.getSalt();
//对密码进行加密
//加密过后还要重新赋值给vip
vip.setPassword(MD5Util.stringToMD5(vip.getPassword() + salt));
vip.setSalt(salt);
return dao.save(vip);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
###学习心得
今天将JavaWEB的整体内容进行了一个综合的练习,感觉掌握情况还可以
###掌握情况:一般
浙公网安备 33010602011771号