Jsp文件
login.jsp
info.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>MVC架构</title>
</head>
<body>
<p>JSP文件充当MVC中的执行层C</p>
<form action="loginCheck" method="post" >
请输入一个整数a:<input type="text" name="a"><br/>
请输入一个整数b:<input type="text" name="b"><br/>
<input type="submit" value="提交">
<input type="reset">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>输出页面</title>
</head>
<body>
<h1 align="center">苦学Java三十载,只留Servlet在人间</h1>
<p>JSP文件充当MVC中的执行层C</p>
<%=request.getAttribute("out") %>
</body>
</html>
Servlet
Hello.java
package test;
//Servlet充当MVC中的控制层V
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Hello
*/
@WebServlet("/loginCheck")
public class Hello extends HttpServlet {
private static final long serialVersionUID = 1L;
public Hello() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String aa=request.getParameter("a");
String bb=request.getParameter("b");
int a=Integer.parseInt(aa);
int b=Integer.parseInt(bb);
mulbeans xx=new mulbeans(a);
mulbeans yy=new mulbeans(b);
int c=xx.mul(yy).getX();
request.setAttribute("out",c);
request.getRequestDispatcher("/info.jsp").forward(request, response);
}
}
JavaBeans
mulbeans.java
package test;
//JavaBeans——>充当模拟层,担任MVC框架中的M角色
public class mulbeans {
//必须有set() get() 无参构造函数
public int x;
public mulbeans(){
}
public mulbeans(int x)
{
this.x=x;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
//真正的模拟函数
public mulbeans mul(mulbeans y)
{
int m=this.x*y.getX();
mulbeans c;
c=new mulbeans(m);
return c;
}
}