7.2
MVC ,MVC 。
7.2.1
triangle.jsp JSP+JavaBean , , , MVC JSP+JavaBean+servlet 。 ,M (JavaBean) , Triangle( ) Stringtonum( )。 MVC , servlet TriangleMVC, 。
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Stringtonum;
import model.Triangle;
public class TriangleMVC extends HttpServlet {
public TriangleMVC() {super();}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String str=request.getParameter("boy");
Stringtonum angle=new Stringtonum();
boolean flag=false;
RequestDispatcher dispatcher = null;
Triangle tri=null;
if(!angle.strtonum(str))
request.setAttribute("result", " ");
else{
tri=new Triangle(angle.getNum1(),angle.getNum2(),angle.getNum3());
if(!tri.isTriangle())
request.setAttribute("result", " ");
else{
request.setAttribute("result", tri);
flag=true;
}
}
if (flag ==false)
dispatcher= getServletContext().getRequestDispatcher(
"/trierror.jsp");
else
dispatcher=getServletContext().getRequestDispatcher(
"/trisucess.jsp");
dispatcher.forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
servlet , :
1、 ( )。
2、 Stringtonum , , ; , trierror.jsp, " "。
3、 Triangle , , ; , trierror.jsp, " "。
4、 trisucess.jsp, (tri) 。
JSP 。
trierror.jsp
<%@page contentType="text/html;charset=GB2312" %>
<%=(String)request.getAttribute("result") %>
trisucess.jsp
<%@page contentType="text/html;charset=GB2312" %>
<%@page import="modle.*"%>
<% Triangle tri=new Triangle();
tri=(Triangle)request.getAttribute("result"); %>
:
<%=tri.getEdge1()%>
<%=tri.getEdge2()%>
<%=tri.getEdge3()%>
:<%=tri.calArea()%>
trimvc.jsp
<%@page contentType="text/html;charset=GB2312" %>
, :
" name=submit>
7.2.2 MVC
, MVC 。 M (JavaBean) 。 , MVC , V M , (car) session , 。 , V (JSP) V C , servlet( :OderServlet ModifyMVC)。MVC 。 7-2:MVC 。
goodsmvc.jsp, 。
<%@ page contentType="text/html;charset=GB2312" %>
1 computer 12
>
>
2 Cprogram 20
>
3 database 30
>
。 JSP servlet (OrderServlet), , JS , 。
lookmvc.jsp 。 , JSP servlet(ModifyServlet)。 。
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="shop.*"%>
<%@page import="java.util.*" %>
trisucess.jsp
<%@page contentType="text/html;charset=GB2312" %>
<%@page import="modle.*"%>
<% Triangle tri=new Triangle();
tri=(Triangle)request.getAttribute("result"); %>
:
<%=tri.getEdge1()%>
<%=tri.getEdge2()%>
<%=tri.getEdge3()%>
:<%=tri.calArea()%>
trimvc.jsp
<%@page contentType="text/html;charset=GB2312" %>
, :
" name=submit>
7.2.2 MVC
, MVC 。 M (JavaBean) 。 , MVC , V M , (car) session , 。 , V (JSP) V C , servlet( :OderServlet ModifyMVC)。MVC 。 7-2:MVC 。
goodsmvc.jsp, 。
<%@ page contentType="text/html;charset=GB2312" %>
1 computer 12
>
>
2 Cprogram 20
>
3 database 30
>
。 JSP servlet (OrderServlet), , JS , 。
lookmvc.jsp 。 , JSP servlet(ModifyServlet)。 。
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="shop.*"%>
<%@page import="java.util.*" %>
<% car car1=(car)session.getAttribute("shop");
if(car1==null){car1=new car();}// ;
Vector v=car1.getV();%>
<% for(int i=0;i
{ Book book=(Book)v.get(i);
out.println("
");
out.println("
");
out.println("
");
out.println("
");
out.println("
");
out.println("
");
}
%>
"+book.getBooknum()+" "+book.getBookname()+" "+book.getPrice()+" ");
out.println(" value="+book.getNum()+">");
out.println("
");
String str=book.getBooknum();
out.println("");
out.println("
>
(C ), servlet。 OrderServlet ModifyMVC。 。
OrderServlet.java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*
import shop.car;
public class OrderServlet extends HttpServlet {
public OrderServlet() {super();}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String goods=request.getParameter("goods");
HttpSession session=request.getSession();
car car1=(car)session.getAttribute("shop");
if(car1==null){ car1=new car();}// ;
car1.add(car1.newgoods(goods));
session.setAttribute("shop",car1);
RequestDispatcher dispatcher = null;
dispatcher=getServletContext().getRequestDispatcher("/goodsmvc.jsp");
dispatcher.forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
,OrderServlet success.jsp 。
ModifyMVC,java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import shop.car;
public class ModifyMVC extends HttpServlet {
public ModifyMVC() {super();}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session=request.getSession();
car car1=(car)session.getAttribute("shop");
String str[]= request.getParameterValues("book");
if(str!=null){
for(int i=0;ilength;i++)
car1.delete(str[i]);
}
String strbook=null;
String booknum=null;
for(int n=0;n
strbook=car1.getBook(n).getBooknum();// ( );
booknum=request.getParameter(strbook);// ;
if(booknum==null)booknum="";
int num=Integer.parseInt(booknum);
car1.modify(strbook,num);// ;
}
RequestDispatcher dispatcher = null;
dispatcher=getServletContext().getRequestDispatcher(
"/lookmvc.jsp");
dispatcher.forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
,ModifyMVC modify.jsp 。
7.2.3
。 7-3