添加新课程界面

 

 

学生:陈天时

教师:王建民

日期:20171128

项目计划总结

        任务

日期

听课

编写程序

 

 

日总计

 

 

周二 11.28

60

165

 

 

225

 

 

 

时间记录日志

日期

开始时间

结束时间

中断时间

净时(分钟)

活动

备注

11.28

10:00

11:00

 

60

听课

 

 

11:15

12:00

 

45

编程

套用之前使用的框架

 

16:40

17;40

 

60

编程

编写判断语句

 

缺陷记录日志

日期

编号

类型

引入阶段

排除阶段

修复阶段

修复缺陷

11.28

1

 

编码

编译

20min

 

描述:漏掉了<%

 

2

 

编码

编译

30

 

描述:  addInput.jsp 缺少<%=request.getAttribute("error") %>

 

3

 

 

 

 

 

 

 

 

 

 

 

package com.jaovo.msg.dao;
import java.util.List;

import com.jaovo.msg.model.Student;
public interface IStudentDao 
{
	public void add(Student user);
	public List<Student> load();
}

  

package com.jaovo.msg.dao;

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

import com.jaovo.msg.Util.DBUtil;
import com.jaovo.msg.Util.UserException;
import com.jaovo.msg.model.Student;

public class StudentDaoImpl implements IStudentDao
{
	//向数据库添加的方法
	@SuppressWarnings("resource")
	public void add(Student user) 
	{
		//获得连接对象
		Connection connection=DBUtil.getConnection();
		//准备sql语句
		String sql="select count(*) from t_student where name=?";
		if(connection!=null)
		{
			System.out.print("连接成功");
		}
		else
		{
			System.out.print("连接失败");
		}
		//          查询数据的条数   条件:username=传进来的参数
		//创建语句传输对象
		PreparedStatement preparedStatement=null;
		ResultSet resultSet=null;
		try 
		{
			preparedStatement= connection.prepareStatement(sql);
			preparedStatement.setString(1, user.getName());
			//写几个问号,             1234,第几个问号   
			//接收结果集(数据条数)
			resultSet =preparedStatement.executeQuery();
			//遍历结果集
			while(resultSet.next())
			{
				if(resultSet.getInt(1)>0)
				//结果集的条数。获取结果集的第一条数据(这里只要一条)
				{
					throw new UserException("用户已存在");
				}
			}
			sql="insert into t_student(name,teacher,place) value(?,?,?)";
			preparedStatement=connection.prepareStatement(sql);
			preparedStatement.setString(1, user.getName());
			preparedStatement.setString(2, user.getTeacher());
			preparedStatement.setString(3, user.getPlace());
			preparedStatement.executeUpdate();
		} 
		catch (SQLException e) 
		{
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		finally
		{
			//关闭资源
			DBUtil.close(resultSet);
			DBUtil.close(preparedStatement);
			DBUtil.close(connection);
		}
	}
	public List<Student> load() 
	{
		Connection connection=DBUtil.getConnection();
		//准备sql语句
		String sql="selet * from t_student";
		//selet *  获取数据库所有的列
		//创建语句传输对象
		PreparedStatement preparedStatement=null;
		ResultSet resultSet=null;
		//集合中只能放入user对象
		List<Student> users=new ArrayList<Student>();
		Student user=null;
		try 
		{
			preparedStatement=connection.prepareStatement(sql);
			resultSet=preparedStatement.executeQuery();
			while(resultSet.next())
			{
				user=new Student();
				user.setId(resultSet.getInt("id"));
				user.setName(resultSet.getString("name"));
				user.setTeacher(resultSet.getString("teacher"));
				user.setPlace(resultSet.getString("place"));
				users.add(user);
			}
		} 
		catch (SQLException e) 
		{
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		finally
		{
			DBUtil.close(preparedStatement);
			DBUtil.close(connection);
		}
	return users;
	}
}

  

package com.jaovo.msg.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class CharFilter implements Filter
{
	String encoding=null;
	//初始化
	@Override
	public void init(FilterConfig filterConfig) throws ServletException 
	{
		// TODO 自动生成的方法存根
		//获取初始化的参数
		encoding=filterConfig.getInitParameter("encoding");
	}
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
			throws IOException, ServletException 
	{
		// TODO 自动生成的方法存根
//		request.setCharacterEncoding("utf-8");
		request.setCharacterEncoding(encoding);
		System.out.println(encoding);
		chain.doFilter(request, response);
		//调用下一个过滤器
		
	}
	@Override
	public void destroy()
	{
		// TODO 自动生成的方法存根
		
	}
}

  

package com.jaovo.msg.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jaovo.msg.model.Student;

public class Loginfilter implements Filter{
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		//首先获取session
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;
		
		HttpSession session = req.getSession();
		Student user = (Student) session.getAttribute("loginUser");
		
		if (user == null) {
			resp.sendRedirect(req.getContextPath()+"/LoginInput.jsp");
			return ;
		}
		chain.doFilter(req, resp);
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO 自动生成的方法存根
		
	}
}

  

package com.jaovo.msg.model;

public class Student 
{
	private int id;
	private String name;
	private String teacher;
	private String place;
	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 getTeacher() {
		return teacher;
	}
	public void setTeacher(String teacher) {
		this.teacher = teacher;
	}
	public String getPlace() {
		return place;
	}
	public void setPlace(String place) {
		this.place = place;
	}
}

  

package com.jaovo.msg.Util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil 
{
	public static Connection getConnection()
	{
		try 
		{
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		}
		catch(InstantiationException|IllegalAccessException | ClassNotFoundException e)
		{
			e.printStackTrace();
		}
		String user="root";
		String password="root";
		String url="jdbc:mysql://localhost:3306/jaovo_msg";
		Connection connection=null;
		try 
		{
			//2 创建连接对象
			connection=DriverManager.getConnection(url, user, password);
		}
		catch (SQLException e) 
		{
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return connection;
	}
	//关闭资源方法
	public static void close(Connection connection)
	{
		try 
		{
			if(connection!=null) 
			{
				connection.close();
			}
		} 
		catch (SQLException e) 
		{
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
	public static void close(PreparedStatement PreparedStatement)
	{
		try 
		{
			if(PreparedStatement!=null) 
			{
				PreparedStatement.close();
			}
		} 
		catch (SQLException e) 
		{
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
	public static void close(ResultSet resultSet)
	{
		try 
		{
			if(resultSet!=null) 
			{
				resultSet.close();
			}
		} 
		catch (SQLException e) 
		{
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
}

  

package com.jaovo.msg.Util;

@SuppressWarnings("serial")
public class UserException extends RuntimeException
{

	public UserException() 
	{
		super();
		// TODO 自动生成的构造函数存根
	}

	public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) 
	{
		super(message, cause, enableSuppression, writableStackTrace);
		// TODO 自动生成的构造函数存根
	}

	public UserException(String message, Throwable cause) 
	{
		super(message, cause);
		// TODO 自动生成的构造函数存根
	}

	public UserException(String message) 
	{
		super(message);
		// TODO 自动生成的构造函数存根
	}

	public UserException(Throwable cause) 
	{
		super(cause);
		// TODO 自动生成的构造函数存根
	}
	
}

  

package com.jaovo.msg.Util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class ValidateUtil 
//验证信息是否为空
{
	public static boolean validateNull(HttpServletRequest request,String[] fileds) 
	{
		boolean validate =true;
		//map对象用来装载不同的错误信息
		@SuppressWarnings({ "unchecked", "rawtypes" })
		Map<String, String> errorMsg= new HashMap();
		//map容器  <>泛型
		for(String filed:fileds)
		{
			//获取从客户端传递过来的参数
			String value=request.getParameter(filed);
			if(value==null||"".equals(value.trim()))
			{
				validate=false;
				errorMsg.put(filed, filed+"不能为空");
			}
			if(!validate)
			{
				request.setAttribute("errormsg", errorMsg);
			}
		}
		return validate;
	}
	public static String showError(HttpServletRequest request , String filed) {
		@SuppressWarnings("unchecked")
		Map<String, String> errorMsg = (Map<String,String>)request.getAttribute("errormsg");
		if (errorMsg == null) 
		{
			return "";
		}
		String msg = errorMsg.get(filed);
		if (msg == null) 
		{
			return "";
		}
		return msg;
	}
}

  

<%@page import="com.jaovo.msg.dao.StudentDaoImpl"%>
<%@page import="com.jaovo.msg.model.Student"%>
<%@page import="com.jaovo.msg.Util.ValidateUtil"%>
<%@page import="com.jaovo.msg.Util.UserException"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
	//接收客户端传递过来的参数
	String name = request.getParameter("name");
	String teacher = request.getParameter("teacher");
	String place = request.getParameter("place");
	boolean validate = ValidateUtil.validateNull(request, new String[]{"name","teacher","place"});
	if(teacher.equals("王建民")||teacher.equals("刘立嘉")||teacher.equals("刘丹")||teacher.equals("王辉")||teacher.equals("杨子光"))
	{
		
	}
	else
	{
		request.setAttribute("error", "教师姓名错误");
%>
		<jsp:forward page="addInput.jsp"></jsp:forward>
<%
	}
	if(place.startsWith("一教")||place.startsWith("二教")||place.startsWith("三教")||place.startsWith("基教"))
	{
		
	}
	else
	{
		request.setAttribute("error", "上课地点错误");
%>
		<jsp:forward page="addInput.jsp"></jsp:forward>
<%
	}
	Student user = new Student();
	user.setName(name);
	user.setTeacher(teacher);
	user.setPlace(place);
	StudentDaoImpl userDao = new StudentDaoImpl();
	try
	{
	userDao.add(user);
	out.print("添加成功");
	//重定向
	}catch(UserException e)
	{
%>
	<h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2>
	<%
	}
	%>
</html>

  

<%@page import="com.jaovo.msg.Util.ValidateUtil"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<title>课程表</title>
</head>
<body>


	<form action="add.jsp" method="get">
	<%=request.getAttribute("error") %>
	<table>
			<tr>
				<td>课程名称: </td>
				<td>
					<input type="text" name="name" />
					<%
// 						if(errorMsg != null){
// 							if(errorMsg.get("username") != null){
// 								out.println( errorMsg.get("username"));
// 							}
// 						}
						
					%>
					<%=ValidateUtil.showError(request, "name") %>
				</td>
			</tr>
				<tr>
    			<td>任课教师:</td>
    			<td>
    				<input type="text" name="teacher" />
    				<%
// 						if(errorMsg != null){
// 							if(errorMsg.get("password") != null){
// 								out.println( errorMsg.get("password"));
// 							}
// 						}
    				
					%>
					<%=ValidateUtil.showError(request, "teacher") %>
    			</td>
    		</tr>
    		<tr>
    			<td>上课地点:</td>
    			<td>
    				<input type="text" name="place" />
    				<%
// 						if(errorMsg != null){
// 							if(errorMsg.get("nickname") != null){
// 								out.println( errorMsg.get("nickname"));
// 							}
// 						}
    				
					%>
					<%=ValidateUtil.showError(request, "place") %>
    			</td>
    		</tr>
    		<tr align="center">
    			<td colspan="2">
    				<input type="submit" value="提交" />
    				<input type="reset" value="重置" />
    			</td>
    		</tr>
		</table>
	</form>
</body>
</html>

  

 

posted @ 2017-11-28 19:05  cts1234  阅读(279)  评论(0编辑  收藏  举报