欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JavaWeb_MVC模式的简单demo

程序员文章站 2022-04-02 10:21:13
...

JavaWeb_MVC模式的简单demo

本文通过实现简单的登录功能来介绍MVC模式

1. 项目的目录

JavaWeb_MVC模式的简单demo

2. login.jsp (包含一个表单,提交用户名、密码等信息)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login page</title>
</head>
<body>
    <form action="../login.do" method="post">
        <label>用户名  </label><input type="text" name="userName" /><br /> <br />
        <label>密码   </label><input type="password" name="password" /><br /> <br />
        <input type="submit" value="登录" />
    </form>
</body>
</html>

3. UserBean.java (JavaBean文件,对应登录页面的表单信息)

package com.mxh.mvcdemo.demo1;

public class UserBean {
    private String userName;
    private String password;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

4. Login.java (servlet类,接收login.jsp页面的请求,将表单信息封装到UserBean中,再将请求转发给hone.jsp页面,home.jsp页面作为响应页面返回客户端)

package com.mxh.mvcdemo.demo1;

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;

@SuppressWarnings("serial")
public class Login extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        UserBean user = new UserBean();
        user.setUserName(req.getParameter("userName"));
        user.setPassword(req.getParameter("password"));

        req.setAttribute("user", user);

        RequestDispatcher view = req.getRequestDispatcher("/demo1/home.jsp");
        view.forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

5. web.xml (配置servlet类)

<servlet>
    <servlet-name>Login</servlet-name>
    <servlet-class>com.mxh.mvcdemo.demo1.Login</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Login</servlet-name>
    <url-pattern>/login.do</url-pattern>
</servlet-mapping>

6. home.jsp (使用jstl标签输出用户表单提交的内容)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Home Page</title>
</head>
<body>
    <jsp:useBean id="user" class="com.mxh.mvcdemo.demo1.UserBean" scope="request"></jsp:useBean>
    用户名:<jsp:getProperty property="userName" name="user"    /><br/>
    密码 :<jsp:getProperty property="password" name="user"    />
</body>
</html>

7. 程序运行效果图

JavaWeb_MVC模式的简单demo

JavaWeb_MVC模式的简单demo

8. 总结

jsp为视图层,servlet为控制器,javabean为模型。视图层的数据提交到控制器,控制器将数据封装到模型中,再由视图层操作模型输出数据。