DispatherServlet Test(MVC)
一、Model
package Bank;
public class BankCustomer {
private String userName;
private int age;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
二、Controller
package Bank;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Controller extends HttpServlet {
private static final long serialVersionUID = 1L;
String next;
public Controller() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request,response);
}
public void init() throws ServletException {
}
protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
if(request.getParameter("command").equals("showarticle")){
Contribute command = new Contribute();
next = command.getAllArticle(request, response);
}
dispatcher(request,response,next);
}
protected void dispatcher(HttpServletRequest request,HttpServletResponse response,String page) throws ServletException, IOException {
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(page);
dispatcher.forward(request, response);
}
}
三、util class
package Bank;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Contribute {
public Contribute() {
}
public String getAllArticle(HttpServletRequest request,
HttpServletResponse response) {
BankCustomer bc = new BankCustomer();
bc.setAge(3);
bc.setUserName("lunzi");
request.setAttribute("bc", bc);
return "/fristc.jsp";
}
}
四、View
toFrist.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'frist.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<a href="/DispatherServlet/servlet/Controller?command=showarticle">显示用户信息</a>
</body>
</html>
frist.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'frist.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<jsp:useBean id="bc" type="Bank.BankCustomer" scope="request"></jsp:useBean>
Customer Name:<jsp:getProperty name="bc" property="userName"/><br/>
Customer age:<jsp:getProperty name="bc" property="age"/>
</body>
</html>
lunzi
2007-12-13 00:26:08
评论:0
阅读:77
引用:0
