jsp MVC学习笔记
Model层:
四个包:
com.maker.bean存放数据库里面的字段信息。
package com.maker.bean; public class User
{ private String username;
private String password;
private int id;
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;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
} }
在编写get set方法可以编写完数据库字段之后生成。
com.maker.util包:
package com.maker.util; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class DBUtil
{
public Connection getConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException
{
String Url="jdbc:mysql://localhost/jsptest";
String Driver="com.mysql.jdbc.Driver";
String username="root";
String password="w111206";
Class.forName(Driver).newInstance();
Connection conn = DriverManager.getConnection(Url,username,password);
return conn; }
public void closeConnection(Connection conn)
{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
里面包含数据库连接打开,关闭代码
com.maker.dao包:
package com.maker.dao;
import java.sql.*;
import java.util.ArrayList; import com.maker.bean.User;
import com.maker.util.DBUtil;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement; public class UserData
{
public void Del(int Id)
{
String sql="delete from user where id="+Id;
DBUtil util=new DBUtil();
Connection conn=null;
try {
conn=util.getConnection();
PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql); pstmt.executeUpdate(); } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
util.closeConnection(conn);
} }
public void save(User us)
{
String sql="insert into User (username,password) values(?,?)";
DBUtil util=new DBUtil();
Connection conn = null;
try {
conn = util.getConnection();
PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);
pstmt.setString(1, us.getUsername());
pstmt.setString(2,us.getPassword());
pstmt.executeUpdate(); } catch (Exception e) { e.printStackTrace();
}
finally{
util.closeConnection(conn);
}
}
public ArrayList List()
{
ArrayList list=new ArrayList();
String sql="select * from user";
DBUtil util=new DBUtil();
Connection conn = null;
try {
conn = util.getConnection();
java.sql.Statement stmt=conn.createStatement();
java.sql.ResultSet rs=stmt.executeQuery(sql);
while(rs.next())
{
int id=rs.getInt(1);
String username=rs.getString(2);
String password=rs.getString(3);
User us=new User();
us.setId(id);
us.setPassword(password);
us.setUsername(username);
list.add(us);
} } catch (Exception e) { e.printStackTrace();
}
finally{
util.closeConnection(conn);
}
return list;
} }
里面是数据库操作代码
Control控制层:
com.maker.Servlet包
package com.maker.Sevlet; import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.maker.bean.User;
import com.maker.dao.UserData; public class UpdateServlet extends HttpServlet { /**
* Constructor of the object.
*/
public UpdateServlet() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { String action=request.getParameter("action");
int Id=Integer.parseInt(action); UserData da=new UserData();
da.Del(Id);
ArrayList<User> list=da.List();
request.setAttribute("UserList",list); request.getRequestDispatcher("/index.jsp").forward(request, response);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { String action=request.getParameter("action");
int Id=Integer.parseInt(action); UserData da=new UserData();
da.Del(Id);
ArrayList<User> list=da.List();
request.setAttribute("UserList",list);
request.setAttribute("Id",Id); request.getRequestDispatcher("/index.jsp").forward(request, response);
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
} }
里面是Servlet,如果测试里面数据传值等是否正确,想输出一个数值,可采用
<%@page import="com.maker.bean.User"%>
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!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>
<table>
<%
@SuppressWarnings("unchecked")
ArrayList<User> arr=(ArrayList<User>)request.getAttribute("UserList");
for(User us:arr)
{
out.println("<tr>");
out.println("<td>"+us.getUsername()+"</td>");
out.println("<td>"+us.getPassword()+"</td>");
out.println("<td><a href='servlet/UpdateServlet?action="+us.getId()+"'>编辑</a></td>");
out.println("</tr>");
}
%>
<br/>
<br/>
</table>
<table>
<c:forEach var="fuwa" items="${UserList}" >
<tr>
<td>
<c:out value="${fuwa.id}" />
</td>
<td><td><a href="servlet/UpdateServlet?action=${fuwa.id}">编辑</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
jsp MVC学习笔记的更多相关文章
- Spring MVC 学习笔记一 HelloWorld
Spring MVC 学习笔记一 HelloWorld Spring MVC 的使用可以按照以下步骤进行(使用Eclipse): 加入JAR包 在web.xml中配置DispatcherServlet ...
- Spring MVC 学习笔记12 —— SpringMVC+Hibernate开发(1)依赖包搭建
Spring MVC 学习笔记12 -- SpringMVC+Hibernate开发(1)依赖包搭建 用Hibernate帮助建立SpringMVC与数据库之间的联系,通过配置DAO层,Service ...
- Spring MVC 学习笔记11 —— 后端返回json格式数据
Spring MVC 学习笔记11 -- 后端返回json格式数据 我们常常听说json数据,首先,什么是json数据,总结起来,有以下几点: 1. JSON的全称是"JavaScript ...
- Spring MVC 学习笔记10 —— 实现简单的用户管理(4.3)用户登录显示全局异常信息
</pre>Spring MVC 学习笔记10 -- 实现简单的用户管理(4.3)用户登录--显示全局异常信息<p></p><p></p>& ...
- Spring MVC 学习笔记9 —— 实现简单的用户管理(4)用户登录显示局部异常信息
Spring MVC 学习笔记9 -- 实现简单的用户管理(4.2)用户登录--显示局部异常信息 第二部分:显示局部异常信息,而不是500错误页 1. 写一个方法,把UserException传进来. ...
- Spring MVC 学习笔记8 —— 实现简单的用户管理(4)用户登录
Spring MVC 学习笔记8 -- 实现简单的用户管理(4)用户登录 增删改查,login 1. login.jsp,写在外面,及跟WEB-INF同一级目录,如:ls Webcontent; &g ...
- Spring MVC 学习笔记1 - First Helloworld by Eclipse【& - java web 开发Tips集锦】
Spring MVC 学习笔记1 - First Helloworld by Eclipse reference:http://www.gontu.org 1. 下载 Spring freamwork ...
- MVC学习笔记索引帖
[MVC学习笔记]1.项目结构搭建及单个类在各个层次中的实现 [MVC学习笔记]2.使用T4模板生成其他类的具体实现 [MVC学习笔记]3.使用Spring.Net应用IOC(依赖倒置) [MVC学习 ...
- ASP.NET MVC学习笔记-----Filter2
ASP.NET MVC学习笔记-----Filter(2) 接上篇ASP.NET MVC学习笔记-----Filter(1) Action Filter Action Filter可以基于任何目的使用 ...
随机推荐
- Django数据库设置
设置数据库,创建您的第一个模型,得到一个简单介绍 Django的自动生成管理网站. 数据库设置 现在,打开 mysite / settings.py . 这是一个普通的Python模块 模块级变量代表 ...
- Azure平台 对Twitter 推文关键字进行实时大数据分析
Learn how to do real-time sentiment analysis of big data using HBase in an HDInsight (Hadoop) cluste ...
- 小例子(一)、MD5加密
一个MD5加密的小案例 代码如下: using System; using System.Text; using CCWin; using System.Security.Cryptography; ...
- 匹配session
package dl.zhang.test.util; import java.io.IOException; import java.io.Reader; import org.apache.iba ...
- PHP中的CURL函数库
PHP中的CURL函数库(Client URL Library Function) curl_close — 关闭一个curl会话curl_copy_handle — 拷贝一个curl连接资源的所有内 ...
- linux----关于定位和查找
1.top --查看进程2.su --临时切换用户命令[root@tomato2 ~]# sudo su gongxijun[gongxijun@tomato2 root]$ 3.whoami --- ...
- hdu-----(2807)The Shortest Path(矩阵+Floyd)
The Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- App开发
设置App图标 在Assets.xcassets的AppIcon中添加图片. 设置App名称 工程 -> Info -> 添加Key:“Bundle Display Name“ 和 Val ...
- jquery ajax请求方式与提示用户正在处理请稍等,等待数据返回时loading的显示
1.jquery ajax请求方式与提示用户正在处理请稍等 为了提高用户体验度,我们通常会给出 “正在处理,请稍等!”诸如此类的提示.我们可通过设置$.ajax()下的参数beforeSend()来实 ...
- BZOJ1937 [Shoi2004]Mst 最小生成树
首先由贪心的想法知道,树边只减不加,非树边只加不减,令$w_i$表示i号边原来的边权,$d_i$表示i号边的改变量 对于一条非树边$j$连接着两个点$x$.$y$,则对于$xy$这条路径上的所有树边$ ...