一个很简单的MVC程序
servlet.java(MVC中的C)
package zz;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class servlet extends HttpServlet{
public voiddoGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException,IOException{
HttpSession session=request.getSession();
NameBean nameBean=new NameBean();
String name=request.getParameter("name");
nameBean.setname(name);
session.setAttribute("nameBean",nameBean);
String add="/WEB-INF/classes/ShowName.jsp";
RequestDispatcher dispatcher=request.getRequestDispatcher(add);
dispatcher.forward(request,response);
}
}
NameBean.java(MVC中的M)
package zz;
public class NameBean{
private String name;
public NameBean(){}
public NameBean(String name){
setname(name);
}
public String getname(){
return name;
}
public void setname(String name){
this.name=name;
}
}
ShowNmae.jsp(MVC中的V)
<%@ page session="true"%>
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<jsp:useBean id="nameBean" type="zz.NameBean"
scope="session"/>
<jsp:getProperty name="nameBean" property="name"/>
</body>
</html>