MVC模式与案例
MVC设计模式:
M:Model ,模型 :一个功能。用JavaBean实现。
V:View,视图: 用于展示、以及与用户交互。使用html js css jsp jquery等前端技术实现
C:Controller,控制器 :接受请求,将请求跳转到模型进行处理;模型处理完毕后,再将处理的结果
返回给 请求处 。 可以用jsp实现, 但是一般建议使用 Servlet实现控制器。
Jsp->Java(Servlet)->JSP
Servlet:
Java类必须符合一定的 规范:
a.必须继承 javax.servlet.http.HttpServlet
b.重写其中的 doGet()或doPost()方法
doGet(): 接受 并处 所有get提交方式的请求
doPost():接受 并处 所有post提交方式的请求
Servlet要想使用,必须配置
Serlvet2.5:web.xml
Servle3.0: @WebServlet
Serlvet2.5:web.xml:
项目的根目录:WebContent 、src
<a href="WelcomeServlet">所在的jsp是在 WebContent目录中,因此 发出的请求WelcomeServlet 是去请求项目的根目录。
Servlet流程:
请求 -><url-pattern> -> 根据<servlet-mapping>中的<servlet-name> 去匹配 <servlet> 中的<servlet-name>,然后寻找到<servlet-class>,求中将请求交由该<servlet-class>执行。
2个/:
jsp:/ localhost:8888
web.xml: / http://localhost:8888/项目名/
登录实例(使用MVC模式)
LoginDao.java
package org.abyss.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import org.abyss.entity.Login; //模型层,用于处理登录 public class LoginDao { public static int login(Login login){ Connection connection = null; PreparedStatement pstmt = null; ResultSet rs = null; try { String URL = "jdbc:mysql://localhost:3306/mydatabase?serverTimezone=GMT%2B8"; String USERNAME = "root"; int result = -1; String PWD = "password"; Class.forName("com.mysql.cj.jdbc.Driver"); connection = DriverManager.getConnection(URL,USERNAME,PWD); String sql = "select count(*) from login where uname=? and upwd=?"; pstmt = connection.prepareStatement(sql); pstmt.setString(1, login.getUname()); pstmt.setString(2, login.getUpwd()); rs = pstmt.executeQuery(); if (rs.next()) { result = rs.getInt(1); System.out.println("result="+result); } if (result > 0) { return 1; } else if(result==0){ return 0; }else { return -1; } } catch (Exception e) { e.printStackTrace(); } finally { try { if (pstmt != null) pstmt.close(); if (connection != null) connection.close(); if (rs != null) rs.close(); } catch (Exception e2) { e2.printStackTrace(); } } return 0; } }
Login.java
package org.abyss.entity; public class Login { private int id; private String uname; private String upwd; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpwd() { return upwd; } public void setUpwd(String upwd) { this.upwd = upwd; } public Login() { } public Login( String uname, String upwd) { this.uname = uname; this.upwd = upwd; } }
LoginServlet.java
package org.abyss.servlet; import java.io.IOException; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.abyss.dao.LoginDao; import org.abyss.entity.Login; import org.apache.jasper.tagplugins.jstl.core.Out; //控制器层 public class LoginServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String uname=request.getParameter("uname"); String upwd=request.getParameter("upwd"); System.out.println(uname+upwd); Login login=new Login(uname, upwd); int result = LoginDao.login(login); if(result>0) { response.sendRedirect("welcome.jsp"); } else response.sendRedirect("login.jsp"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登录</title> </head> <body> <form action="LoginServlet" method="post"> 用户名:<input type="text" name="uname"><br/> 密码:<input type="password" name="upwd"><br/> <input type="submit" value="登录"> </form> </body> </html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> 登录成功! </body> </html>
报错信息
java.lang.ClassNotFoundException: com.jdbc.cj.mysql.Driver at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1360) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1182) at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Class.java:333) at org.abyss.dao.LoginDao.login(LoginDao.java:24)
正确代码: Class.forName("com.mysql.cj.jdbc.Driver");
(感谢@夜月薇凉映银弩 帮忙修改代码)