MVC框架

来源:互联网 发布:pycharm和python 编辑:程序博客网 时间:2024/06/05 17:25

JSP+JavaBean的形式是SUN公司提供的一种简单开发模型,经常用于项目中某个较小的功能。

为了应付更复杂的应用系统,SUN公司推出了另一种开发模型即:JSP+JavaBean+Servlet。

MVC – 是指Model(即模型如JavaBean)、View(视图如JSP,只提供展示)、Control(控制层由Servlet担当).
在实际的开中,用的都是这种开发模式。甚至大家以后学习的webwork、jsf、struts,spring等都是MVC框架。(当然Spring拥有更多的功能)。

这里写图片描述

这里写图片描述
index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>MVC2</title>  </head>  <body>            <h2>JJSP的第3代开发技术(MVC):JSP+servlet+JavaBean+service(MODEL 2)技术</h2>    <br/>    <form action="<%=request.getContextPath()%>/loginServlet2" method="post">       Name:<input type="text" name="name"/><br/>       age:<input type="password" name="pwd"/><br/>       <input type="submit" value="登录"/>    </form>  </body></html>

loginServlets:

package cn.hncu.servlets;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import cn.hncu.jspV2.domain.Person;import cn.hncu.jspV2.domain.student;import cn.hncu.service.StudentServiceImpl;public class loginServlet2 extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doPost(request, response);    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        StudentServiceImpl simpl = new StudentServiceImpl();        // 1获取参数        request.setCharacterEncoding("utf-8");        String name = request.getParameter("name");        String pwd = request.getParameter("pwd");        // 2把信息封装成javabean        student s = new student();        s.setName(name);        s.setPwd(pwd);        // 3调用service层        boolean boo = simpl.login(s);        // 4根据service层业务方法的返回值,导向不同的结果页面(如果需要的话,把数据存储到容器)        if (boo) {            request.setAttribute("s", s);            request.getRequestDispatcher("/jsps/v2/welcome.jsp").forward(                    request, response);        } else {            request.getRequestDispatcher("/jsps/index4.jsp").forward(request,                    response);        }    }}
原创粉丝点击