使用MVC模式开发程序,完成数据的模糊查询
编写程序:使用MVC模式开发程序,完成数据的模糊查询。
要求:
(1)index.jsp用于输入要查询的数据。
(2)result.jsp:用于显示结果。
(3)queryServlet:用于验证数据、实例化JavaBean、调用连接数据库、控制页面跳转
(4)queryDAO:用于连接数据库及进行数据库的操作如:查询、删除、更改等
(5)Student:JavaBean用于数据的封装,方便将查询结果在servlet与jsp页面之间进行传递等
以上几个部分共同构成了MVC模式,JSP为MVC模式当中的V,Servlet为C,queryDAO与JavaBean合在一起为M。
[html]
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
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>
请输入要查询的内容:
<form action="queryServlet" method="post">
<input name="name">
<input type="submit" value="模糊查询">
</form>
</body>
</html>
[html]
<%@ page language="java" import="java.util.*,com.mars.*" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>查询结果</title>
</head>
<body>
查询结果如下:
<table border="1">
<tr>
<td>学号</td>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
<td>地址</td>
</tr>
<%
ArrayList arrayList = (ArrayList)request.getAttribute("arrayList");
for(int i=0; i<arrayList.size();i++){
Student student = (Student)arrayList.get(i);
%>
<tr>
<td><%=student.getId()%></td>
<td><%=student.getName()%></td>
<td><%=student.getAge()%></td>
<td><%=student.getSex()%></td>
<td><%=student.getAddress()%></td>
</tr>
<%
}
%>
</table>
</body>
</html>
[html]
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>queryServlet</servlet-name>
<servlet-class>com.mars.queryServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>queryServlet</servlet-name>
<url-pattern>/queryServlet</url-pattern>
</servlet-mapping>
</web-app>
[java]
package com.mars;
public class Student {
private int id;
private String name;
private String 易做图;
private int age;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return 易做图;
}
public void setSex(String 易做图) {
this.易做图 = 易做图;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
&nbs
补充:Web开发 , Jsp ,