在Servlet中编写html的画面输出不是好的主意,在JSP上编写Java语言也不是很建议;对于Java程序代码与呈现画面的html等混杂在一起,非但撰写不易、日后对维护人员也不友好,而且对大型项目的分工合作也是一大困扰,将来若需要转换为其它页面模板技术上也会遇到很大麻烦。
MVC是Model、View、Controller的缩写,是编写JSP的一种模式。
也称为数据可持续层
主要用到JavaBean技术
实现对业务逻辑处理,数据类(结果的保存)
主要是用JSP/HTML技术来实现
对页面的输入和结果的输出
主要使用Servlet技术
接收用户的请求
调用方法,处理业务来得到结果
根据结果来跳转目的页面(实现页面跳转)
MVC模式关键在结果:
MVC中数据传递步骤:
一、在Servlet中
JavaBeanClass bean = new JavaBeanClass();
//request对象
request.setAttribute("keyWord",bean);
//session对象
HttpSession session = request.getSession(true);
session.setAttribute("keyWord",bean);
//request
RequestDispatcher dis = request.getRequestDispatcher(结果URL地址"xxx.Jsp")dispatcher.forward(request,respone);//请求xxx.Jsp中的结果
//session
forward或者是重定向都可以
二、输出JSP页面
<jsp:useBean id = "keyword" type = "flower.grass.crateJavaBeanclass"
scope = "request/session"/>
<jsp:getProperty name="keyword" property="Java属性"
${keyword.属性名}
EL的具体使用可以看:大佬连接
题目:用户通过JSP页面输入三角形的三边或梯形的上底、下底和高给一个servlet控制器,控制器负责计算三角形和梯形的面积,并将结果存储到数据模型中,然后请求JSP页面显示数据模型中的数据。。
package com.example;
public class Compute_Bean {
private double number1 = 0;
private double number2 = 0;
private double number3 = 0;
private double result;
private String name = "null";
private String number1_name = "null";
private String number2_name = "null";
private String number3_name = "null";
public void setNumber1(double newNumber1) {
number1 = newNumber1;
}
public double getNumber1() {
return number1;
}
public void setNumber2(double newNumber2) {
number2 = newNumber2;
}
public double getNumber2() {
return number2;
}
public void setNumber3(double newNumber3) {
number3 = newNumber3;
}
public double getNumber3() {
return number3;
}
public void setResult(double newResult) {
result = newResult;
}
public double getResult() {
return result;
}
public void setName(String newName) {
name = newName;
}
public String getName() {
return name;
}
public void setNumber1_name(String newNumber1_name) {
number1_name = newNumber1_name;
}
public String getNumber1_name() {
return number1_name;
}
public void setNumber2_name(String newNumber2_name) {
number2_name = newNumber2_name;
}
public String getNumber2_name() {
return number2_name;
}
public void setNumber3_name(String newNumber3_name) {
number3_name = newNumber3_name;
}
public String getNumber3_name() {
return number3_name;
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd">
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>求 三角形/梯形 面积</title>
</head>
<body>
<jsp:useBean id="compute_bean" class="com.example.Compute_Bean" scope="request" />
<h1>求 三角形/梯形 面积</h1>
<form action="Compute_Servlet" method="post">
边1/上底:<input type="text" name="number1" value=<jsp:getProperty name="compute_bean" property="number1" />> <br>
边2/下底:<input type="text" name="number2" value=<jsp:getProperty name="compute_bean" property="number2" />> <br>
边3/高: <input type="text" name="number3" value=<jsp:getProperty name="compute_bean" property="number1" />> <br>
<input type="submit" name="submit" value="求三角形面积"><br>
<input type="submit" name="submit" value="求梯形面积"> <br>
</form>
<table border="1">
<tr>
<th><jsp:getProperty name="compute_bean" property="number1_name" /></th>
<th><jsp:getProperty name="compute_bean" property="number2_name" /></th>
<th><jsp:getProperty name="compute_bean" property="number3_name" /></th>
<th><jsp:getProperty name="compute_bean" property="name" />: </th>
</tr>
<tr>
<td><jsp:getProperty name="compute_bean" property="number1" /> </td>
<td><jsp:getProperty name="compute_bean" property="number2" /> </td>
<td><jsp:getProperty name="compute_bean" property="number3" /> </td>
<td><jsp:getProperty name="compute_bean" property="result" /> </td>
</tr>
</table>
</body>
</html>
package com.example;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import org.apache.jasper.tagplugins.jstl.core.Out;
/**
* Servlet implementation class Compute_Servlet
*/
public class Compute_Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Compute_Servlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
Compute_Bean compute_bean = new Compute_Bean();
request.setAttribute("compute_bean", compute_bean);
double number1 = Double.parseDouble(request.getParameter("number1"));
double number2 = Double.parseDouble(request.getParameter("number2"));
double number3 = Double.parseDouble(request.getParameter("number3"));
String mess = request.getParameter("submit");
compute(number1, number2, number3, mess, compute_bean);
RequestDispatcher dispatcher = request.getRequestDispatcher("login.jsp");
dispatcher.forward(request, response);
System.out.print(compute_bean.getResult());
}
public void compute(double num1, double num2,double num3, String mess, Compute_Bean bean) {
boolean flag = mess.contains("三角");
bean.setNumber1(num1);
bean.setNumber2(num2);
bean.setNumber3(num3);
if(flag) {
double p = (num1 + num2 + num3) * 0.5;
double result = Math.sqrt(p * (p-num1) * (p-num2) * (p-num3));
bean.setResult(result);
bean.setName("三角形面积");
bean.setNumber1_name("边1");
bean.setNumber2_name("边2");
bean.setNumber3_name("边3");
}
else {
double result = (num1 + num2) * num3 * 0.5;
bean.setResult(result);
bean.setName("梯形面积");
bean.setNumber1_name("上底");
bean.setNumber2_name("下底");
bean.setNumber3_name("高");
}
}
}