经过一个慢慢渐进的过程,基本上完成了一个简单的课表查询,其中这几周的博客就是按着这个过程来写的、接下来我们按这个过程去把这些功能逐一去实现起来,若有不太懂的可以去翻看之前的博客,欢迎留言。
JSP部分
JSP只是做一个简单的显示页面,即把从数据所拿到的数据把它显示出来,这里并没有用得上多大的作用。接下来在代码中解释每个语句的作用。
<%@ page language="java" import="java.util.*" pageEncoding="UTF- 8"%>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="dao.User"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>课表查询</title>
</head>
<body>
<%
<%--ServletConfig对象的getServletContext()方法得到servlet上下文的引用--%>
ServletContext context = this.getServletContext();
<%--创建一个集合--%>
List<User>TL=new ArrayList<User>();
<%--获取servlet上下文中封装的对象--%>
TL=(List<User>)context.getAttribute("CLASSLIST");
User u=new User();
%>
<%--用表单来打印课表信息--%>
<table border="2" align="center" width="90%" cellpadding="2" cellspacing="2">
<caption><h2><%u.getValuesemeter();%>学期<%u.getValueclasscode(); %>我的课表</h2></caption>
<thead>
<td>学期</td>
<td>班级</td>
<td>节次</td>
<td>星期一</td>
<td>星期二</td>
<td>星期三</td>
<td>星期四</td>
<td>星期五</td>
<td>星期六</td>
<td>星期日</td>
</thead>
<tbody>
<%for(User L:TL){ %>
<%--循环显示课表内容 --%>
<tr>
<td><%=L.getsemester() %></td>
<td><%=L.getclassCode() %></td>
<td><%=L.getSection() %></td>
<td><%=L.getOne() %></td>
<td><%=L.getTwo() %></td>
<td><%=L.getThree() %></td>
<td><%=L.getFour() %></td>
<td><%=L.getFive() %></td>
<td><%=L.getSix()%></td>
<td><%=L.getSeven()%></td>
</tr>
<%} %>
</tbody>
</table>
</body>
</html>
Servlet部分
Servlet主要是接收表单传来的数据,把数据传给业务层进行处理,最后转发到JSP把数据显示出来。那么在servlet中最重要的就是如何去获得前端所输入的值了,这就得涉及到getParameterNames()方法了,getParameterNames()方法将会一次获取表单name的值并返回一个枚举类型集合。通过其枚举方法就能把这些值给一次获取出来,再把这些封装到一个类中去就完成任务了。
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import UserSerlevet.userService;
import dao.User;
public class RegisterSerlvet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
int i=0;
User user=new User();
String []registetrUser=new String[2];
request.setCharacterEncoding("UTF-8");
Enumeration<String> enums = request.getParameterNames();
while(enums.hasMoreElements()){
String name = enums.nextElement();
String value = request.getParameter(name);
registetrUser[i]=value;
i++;
}
i=0;
user.setValuesemeter(registetrUser[0]);
user.setValueclasscode(registetrUser[1]);
userService userservice=new userService();
try {
List<User>classlist=new ArrayList<User>();
classlist=userservice.getWebMethods(user.getValueclasscode(),user.getValuesemeter());
ServletContext context = this.getServletContext();
context.setAttribute("CLASSLIST",classlist);
request.setAttribute("CLASSLIST", classlist);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//把CLASSLIST封装到显示页面
response.sendRedirect(request.getContextPath()+"/Timetable.jsp");
}
}
JDBC部分
JDBC主要是为了连接数据库的Dao层,从基本的注册驱动,连接驱动等一步步获取数据库内容。
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.mysql.fabric.xmlrpc.base.Array;
public class classUserDao{
public List<User> finAcess(String className,String semester) throws Exception {
Connection conn = null;
PreparedStatement prep =null;
List<User>list=new ArrayList<User>();
ResultSet rs =null;
String sql="select * from timetable inner join class on class.classCode=timetable.classCode where className=? and semester=?";
try {
conn=JdbcUtil.getConnection();
prep =conn.prepareStatement(sql)
prep.setString(1, className);
prep.setString(2, semester);
rs =prep.executeQuery();
while(rs.next()){
String classCode=rs.getString("classCode");
String semest=rs.getString("semester");
int section=rs.getInt("section");
String one=rs.getString("one");
String two=rs.getString("two");
String three=rs.getString("three");
String four=rs.getString("four");
String five=rs.getString("five");
String six=rs.getString("six");
String seven=rs.getString("seven");
User user =new User(classCode,semest,section,one,two,three,four,five,six,seven);
list.add(user);
}
}
catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}finally
{
JdbcUtil.close(rs);
JdbcUtil.close(prep);
JdbcUtil.close(conn);
}
return list;
}
}
业务层部分
业务层主要是处理数据用的,把前端的数据传给Dao层,Dao层获取数据库数据后把数据交给业务层。
package UserSerlevet;
import java.util.ArrayList;
import java.util.List;
import dao.User;
import dao.classUserDao;
public class userService {
public List<User> getWebMethods(String seme,String code) throws Exception {
classUserDao ud=new classUserDao();
List<User>classlist=new ArrayList<User>();
String classCode=null;
String semester=null;
User reUser=new User();
classCode=seme;
semester=code;
classlist= ud.finAcess(classCode,semester);//把值带回
return classlist;
}
}