SpringMVC
MVC
m:model:模型,javabean
v:view:视图,html/jsp
c:controller:控制器:servlet
MyMVC模拟实现
⼀. 阶段⼀
- index.html⻚⾯
<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>index</title>
</head> <body>
<h1>this is index page.</h1>
<form method="post" action="ProductServlet">
pid:<input type="text" name="pid" /><br />
pname:<input type="text" name="pname" /><br />
price:<input type="text" name="price" /><br />
img:<input type="text" name="img" /><br />
<input type="submit" value="submit" /><br />
</form>
</body>
</html>
- ProductServlet.java
package com.qfedu.controller;
import com.qfedu.bean.Product;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@javax.servlet.annotation.WebServlet(urlPatterns = "/ProductServlet")
public class ProductServlet extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//System.out.println(123);
String pid = request.getParameter("pid");
String pname = request.getParameter("pname");
String sprice = request.getParameter("price");
String img = request.getParameter("img");
double price = sprice == null ? 0.0 :
Double.parseDouble(sprice);
System.out.println("pid : " + pid);
System.out.println("price : " + sprice);
System.out.println("pname : " + pname);
System.out.println("img : " + img);
Product p = new Product();
p.setPid(pid);
p.setPname(pname);
p.setImg(img);
p.setPrice(price);
request.setAttribute("p", p);
request.getRequestDispatcher("product.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
- Product.jsp
可以实现⻚⾯的跳转以及数据的展示
问题:⻚⾯都在webapp下,安全性不⾼
<%--
Created by IntelliJ IDEA.
User: james
Date: 2020/3/2
Time: 2:52 PM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<