用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - 其他代码库

简单的MVC格式

2015-07-28 作者: 锋客举报

[其他]代码库

==============================前台界面===============================
<%@ 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 '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>
  <div align="center">
       <input type="button" onclick="window.location.href('jsp/addmessage.jsp')" value="添加日记">
       <input type="button" onclick="window.location.href('ListMessageServlet.action')" value="查询日记">
       </div>
  </body>
</html>
================================web.xml==========================================
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Demo</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  <servlet-name>AddMessageServlet</servlet-name>
  <servlet-class>fengke.mvc.servlet.AddMessageServlet</servlet-class>
  </servlet>
  <servlet>
  <servlet-name>ListMessageServlet</servlet-name>
  <servlet-class>fengke.mvc.servlet.ListMessageServlet</servlet-class>
  </servlet>
  

  <servlet-mapping>
  <servlet-name>AddMessageServlet</servlet-name>
  <url-pattern>/AddMessageServlet.action</url-pattern>
  </servlet-mapping>
  
  <servlet-mapping>
  <servlet-name>ListMessageServlet</servlet-name>
  <url-pattern>/ListMessageServlet.action</url-pattern>
  </servlet-mapping>
  
  
  
</web-app>
==============================前台界面==================================================
<%@ page language="java" import="java.util.*,fengke.mvc.vo.*" 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 'listmessage.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>
    <h1 align="center">查询日记</h1>
    
    <table align="center">
    <form action="AddMessageServlet.action">
              <tr>
              <th>序号</th>
              <th>日期</th>
              <th>简介</th>
              <th>内容</th>
              </tr>    
              <%  List<Message> list=(List<Message>)request.getAttribute("list"); 
                 for(Message mg:list){
              %>
              
              <tr>
              <td><input type="text" name="id" value="<%=mg.getId() %>" ></td>
              <td><input type="text" name="time" value="<%=mg.getTime() %>"></td>
              <td ><input type="text" name="infor" value="<%=mg.getInfor() %>"></td>
              <td ><input type="text" name="message" value="<%=mg.getMessage() %>"></td>
              </tr>
              <%} %>
              <tr>
              <td colspan="2"><input type="submit" align="middle" value="添加"></td>
              <td colspan="2"><input type="reset" align="middle" value="重置"></td>
              </tr>
              
       </form>
    </table>
  </body>
</html>


====================================前台界面=====================================
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" 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 'addmessage.jsp' starting page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
	<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>
    <h1 align="center">添加日记</h1>
    
    <table align="center">
    <form action="AddMessageServlet.action">
              <tr>
              <th>日期</th>
              <th>简介</th>
              <th colspan="2">内容</th>
              </tr>    
              
              <tr>
              <td><input type="text" name="time" ></td>
              <td><input type="text" name="infor" ></td>
              <td colspan="2"><input type="text" name="message" ></td>
              </tr>
              
              <tr>
              <td colspan="2"><input type="submit" align="middle" value="添加"></td>
              <td colspan="2"><input type="reset" align="middle" value="重置"></td>
              </tr>
              
       </form>
    </table>
  </body>
</html>
==============================servlet   控制层===================================
package fengke.mvc.servlet;

import java.io.IOException;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fengke.mvc.service.MessageService;
import fengke.mvc.vo.Message;

/**
 * 控制层
 * 
 * @author 锋客 添加日记信息
 */
public class AddMessageServlet extends HttpServlet {

	/**
	 * 1.接收前台信息 2.将前台信息传入到逻辑层中
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// 接收前台信息
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		/*
		 * parameter与attribute的区别:
		 * 
		 *String time=(String) req.getAttribute("time");
		 * String infor=(String) req.getAttribute("infor");
		 * String message=(String) req.getAttribute("message");
		*/
		//接收时间信息
		String time =new String(req.getParameter("time").getBytes("ISO8859-1"),"utf-8");
		//接收简介
		String infor = new String(req.getParameter("infor").getBytes("ISO8859-1"),"utf-8");
		//接收内容
		String message = new String(req.getParameter("message").getBytes("ISO8859-1"),"utf-8");
		// 检查是否成功接收
		System.out.println(time + " " + infor + " " + message);
		//创建一个AddMessageService对象,实现数据传输
		MessageService addMessage = new MessageService();
		//实例化一个message对象,保存信息,注意包:java.sql.Date
		Date date=Date.valueOf(time);
		Message mesg = new Message(1,date,infor,message);
		addMessage.addMessage(mesg);
		//跳转
		resp.sendRedirect("index.jsp");
		

	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//使其调用doPost方法
		this.doPost(req, resp);
	}
	

}
==============================servlet   控制层================================================
package fengke.mvc.servlet;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.w3c.dom.ls.LSInput;
import fengke.mvc.service.MessageService;
import fengke.mvc.vo.Message;
/**
 * 查询所有日记信息
 * @author 锋客
 *
 */
public class ListMessageServlet extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//设置格式
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		//创建service对象
		MessageService listMessage = new MessageService();
		List<Message> list = listMessage.listAll();
		//向前台传值
		req.setAttribute("list", list);
		//跳转
		req.getRequestDispatcher("jsp/listmessage.jsp").forward(req, resp);
	}
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	
		this.doPost(req, resp);
	}

}


=================================service   逻辑层=============================================
package fengke.mvc.service;

import java.sql.Date;
import java.util.List;

import fengke.mvc.dao.MessageDao;
import fengke.mvc.vo.Message;

/**
 * 逻辑控制层
 * @author 锋客
 * 内容:将信息传入到MessageDao中
 */

public class MessageService {
	
	//添加日记信息(需要传入一个Message对象)
	public void addMessage(Message message){
		MessageDao dao = new MessageDao();
		dao.addMessage(message.getId(), message.getTime(), message.getInfor(), message.getMessage());
		
	}
	/*
	 * 测试service层到dao层以及db层,是否成功搭建
	 * 
	* public static void main(String[] args) {
	*	//包:java.sql.Date;
	*	Date date=Date.valueOf("2012-02-01");
	*	System.out.println(date);
	*	Message mesg=new Message(2,date,"约会","小李");
	*	AddMessageService addMessageService=new AddMessageService();
	*	addMessageService.addMessage(mesg);
	*	}
	*
	*/
	
	/**
	 * 查询所有信息
	 * @return
	 * 内容:
	 * 通过调用dao层,查找数据库中的信息
	 * 
	 */
	public List<Message> listAll(){
		MessageDao dao = new MessageDao();
		List<Message> list = dao.listMessage();
		return list;
	}

}
==============================jdbc   获得链接=============================================
package fengke.mvc.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
 * 获得链接
 * @author 锋客
 * 创建与数据库的链接
 */
public class Jdbc {
	/**
	 * userName  用户名
	 * passWord  密码
	 * url       地址(必须记)
	 * con       链接
	 */
	static String userName="root";
	static String passWord="root";
	static String url="jdbc:mysql://127.0.0.1:3309/demo";
	static Connection con=null;
	
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con=DriverManager.getConnection(url, userName, passWord);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static Connection getConnection(){
		//检测是否正确获得链接
		//System.out.println(con);
		return con;
		
	}
	

}
============================dao    操作层=============================================
package fengke.mvc.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import fengke.mvc.db.Jdbc;
import fengke.mvc.vo.Message;
/**
 * 对dairy表的操作
 * @author 锋客
 *
 */
public class MessageDao {
	private Jdbc jdbc=new Jdbc();
	/*
	 * 添加日记信息:
	 * 通过获得链接,将信息传入到数据库中
	 * 
	 */
	public void addMessage(int id, Date time, String infor, String message){
		Connection con = jdbc.getConnection();
		//防止sql注入
		String sql="insert into dairy(time,infor,message)values(?,?,?)";
		try {
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setDate(1, time);
			ps.setString(2, infor);
			ps.setString(3, message);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public List<Message> listMessage(){
		Connection con = jdbc.getConnection();
		//查询语句
		String sql="select id,time,infor,message from dairy";
		//接收信息
		List<Message> list=new ArrayList<Message>();
		/*创建Message对象
		 * Message mg=new Message();
		 * 不能放在while之外,当使用上述方式创建对象时,内存地址给定,
		 * list中的所有对象都指向同一个,最后一个
		 */
		
		try {
			PreparedStatement ps=con.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				//注意位置(对引用的理解)
				Message mg=new Message();
				//设置mg对象属性
				mg.setId(rs.getInt(1));
				mg.setTime(rs.getDate(2));
				mg.setInfor(rs.getString(3));
				mg.setMessage(rs.getString(4));
				list.add(mg);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return list;
		
	}
   /*  
    * 测试:是否可以查询到结果集
	*public static void main(String[] args) {
	*	MessageDao dao = new MessageDao();
	*	List<Message> list = dao.listMessage();
	*   for(Message mg:list){
	*  	System.out.println(mg.getId()+"   "+mg.getTime()+"    "+mg.getInfor()+"    "+mg.getMessage());
	* 	
	*}
	*}
    */
}
===============================vo  实体类===============================================
package fengke.mvc.vo;

import java.sql.Date;
import java.sql.Time;

/**
 * 实体层
 * @author 锋客
 * 用于简化数据传输
 */
public class Message {
	/*
	 * id: 主键
	 * time:时间
	 * infor:简介
	 * message:详细信息
	 */
	private int id;
	private Date time ;
	private String infor ;
	private String message;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Date getTime() {
		return time;
	}
	public void setTime(Date time) {
		this.time = time;
	}
	public String getInfor() {
		return infor;
	}
	public void setInfor(String infor) {
		this.infor = infor;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public Message() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Message(int id, Date time, String infor, String message) {
		super();
		this.id = id;
		this.time = time;
		this.infor = infor;
		this.message = message;
	}
	

}
===================================结束================================

[源代码打包下载]




网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...