javaWeb MVC入门 demo
程序员文章站
2024-02-26 09:39:58
...
M -- model 模型(自己写代码)
V -- View 视图(jsp)
V -- View 视图(jsp)
C -- Cotroller 控制器(Servlet)
Web层 --> 与Web相关的内容(Servlet,JSP,Servlet相关API:request、response、session、ServletContext)
业务层 --> 业务对象(Service)
数据层 --> 操作数据库(DAO Data Access Object)(所有对数据库的操作,不能跳出到DAO之外)
入门Demo
首先建一个实体类:
package cn.itcast.domain;
/**
* 把数据库中查询到的结果保存到这个对象中
* @author Administrator
*
*/
public class User {
private String username;
private String password;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
}
public void setPassword(String password) {
this.password = password;
}
}
Dao层
package cn.itcast.dao;
import cn.itcast.domain.User;
public class UserDao {
/**
* 把xml中的数据查询出来后 封装到user对象中
* @return
*/
public User find() {
return new User("zhangSan","123");
}
}
Service层
package cn.itcast.service;
import cn.itcast.dao.UserDao;
import cn.itcast.domain.User;
public class UserService {
//service层依赖dao层
private UserDao userDao = new UserDao();
/**
* service的查询 需要使用dao来完成
* @return
*/
public User find() {
return userDao.find();
}
}
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!-- 这里记得导入c库-->
<%
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 'index.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>
<!-- ${pageContext.request.contextPath}/UserServlet -->
<a href="<c:url value='/UserServlet'/>">点击这里查看</a>
</body>
</html>
Servlet
package cn.itcast.web.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.domain.User;
import cn.itcast.service.UserService;
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
* 在servlet层依赖service 然后通过service完成功能 把结果保存到request转发到jsp显示
*/
UserService userService = new UserService();
User user = userService.find();
request.setAttribute("user", user);
request.getRequestDispatcher("/show.jsp").forward(request, response);
}
}
show.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'show.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>
用户名:${user.username} <br/>
密 码:${user.password }<br/>
</body>
</html>
结果
上一篇: JAVA 百度地图 API
下一篇: 三分钟快速解析一道字节跳动经典算法面试题