基于Web的出题系统
首先说软件的架构
在工程里一共建立7个包,具体如下图:
一丶建立数据库dtk
1.创建数据库
create database dtk;
2.创建表
相信大家都会建表,我就偷个懒(●ˇ∀ˇ●)
3.插入数据
数据就自己插入把,我找不自己插入数据的文件了(后期有时间把项目托管一下)
二丶建立数据库连接类
1.导入数据库连接Connection包
我用的是mysql-connector-java-5.1.29.jar
下载后把它放在WEB-INF下的lib中,以后所有的包都放在这下面,如果没有就建立一个
2.在util中新建一个DBbean类
这个类的作用是来连接和释放数据库的,你会发现在这里没有MYSQL的用户名和密码,下面我们进行配置
public class DBbean {
private static String url="", driver = "", userName = "", password = "";
static {
ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
url = bundle.getString("jdbc.url");
driver = bundle.getString("jdbc.driver");
userName = bundle.getString("jdbc.username");
password = bundle.getString("jdbc.password");
}
private static String getUrl() {
return url;
}
private static String getDriver() {
return driver;
}
private static String getUserName() {
return userName;
}
private static String getPassword() {
return password;
}
public static Connection getConnection()throws SQLException,ClassNotFoundException{
Class.forName(getDriver());
Connection conn=DriverManager.getConnection(getUrl(),getUserName(),getPassword());
return conn;
}
public static void release(Statement stmt,Connection conn){
if(stmt!=null){
try{
stmt.close();
}catch(Exception e){
e.printStackTrace();
}
stmt=null;
}
if(conn!=null){
try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
conn=null;
}
}
public static void release(ResultSet rs,Statement stmt,Connection conn){
if(rs!=null){
try{
rs.close();
}catch(Exception e){
e.printStackTrace();
}
rs = null;
}
release(stmt,conn);
}
public static void release(PreparedStatement psmt,Connection conn){
if(psmt!=null){
try{
psmt.close();
}catch(Exception e){
e.printStackTrace();
}
psmt=null;
}
if(conn!=null){
try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
conn=null;
}
}
public static void release(ResultSet rs,PreparedStatement psmt,Connection conn){
if(rs!=null){
try{
rs.close();
}catch(Exception e){
e.printStackTrace();
}
rs = null;
}
release(psmt,conn);
}
}
3.连接数据库的配置文件
在src文件夹下新建一个jdbc.properties文件,内容如下:
3306后面是你建的数据库名称,一般username不用改,password是你登录数据库时的密码(这里一般不一样,我的是123456)
jdbc.url=jdbc:mysql://localhost:3306/dtk
jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=123456
三丶建立question对象
在包entity下建立Question.java类,建立题目对象,代码如下:
public class Question {
private int id;
private int q_type;
private String title;
private String optionA;
private String optionB;
private String optionC;
private String optionD;
private String tips;
private String answer;
private String expound;
public Question(){
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getQ_type() {
return q_type;
}
public void setQ_type(int q_type) {
this.q_type = q_type;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getOptionA() {
return optionA;
}
public void setOptionA(String optionA) {
this.optionA = optionA;
}
public String getOptionB() {
return optionB;
}
public void setOptionB(String optionB) {
this.optionB = optionB;
}
public String getOptionC() {
return optionC;
}
public void setOptionC(String optionC) {
this.optionC = optionC;
}
public String getOptionD() {
return optionD;
}
public void setOptionD(String optionD) {
this.optionD = optionD;
}
public String getTips() {
return tips;
}
public void setTips(String tips) {
this.tips = tips;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
public String getExpound() {
return expound;
}
public void setExpound(String expound) {
this.expound = expound;
}
public String getMyanswer() {
return myanswer;
}
public void setMyanswer(String myanswer) {
this.myanswer = myanswer;
}
private String myanswer;
}
四丶DAO层的建立,也就是数据库操作
1.建立数据库操作类
在包dao/impl下新建一个questionDAO.java文件,代码如下:
这里主要做的是数据库的增删改查操作
public class QuestionDAO {
protected static final String FIELDS_INSERT="id,q_type,title,optionA,optionB,optionC,optionD,tips,answer,expound";
protected static String INSERT_SQL="insert into question("+FIELDS_INSERT+")"+" values(?,?,?,?,?,?,?,?,?,?)";
protected static String SELECT_SQL="select * from question where id = ?";
protected static String FIND_ALL_SQL = "select * from question";
protected static String UPDATE_SQL="update question set "+
"q_type=?,title=?,optionA=?,optionB=?,optionC=?,optionD=?,tips=?,answer=?,expound=? where id=?";
protected static String DELETE_SQL="delete from question where id=?";
public int create(Question qut) throws Exception{
Connection conn = null;
PreparedStatement psmt = null;
int result = 0;
try {
conn = (Connection) DBbean.getConnection();
psmt = (PreparedStatement) conn.prepareStatement(INSERT_SQL);
psmt.setInt(1, qut.getId());
psmt.setInt(2, qut.getQ_type());
psmt.setString(3, qut.getTitle());
psmt.setString(4, qut.getOptionA());
psmt.setString(5, qut.getOptionB());
psmt.setString(6, qut.getOptionC());
psmt.setString(7, qut.getOptionD());
psmt.setString(8, qut.getTips());
psmt.setString(9, qut.getAnswer());
psmt.setString(10, qut.getExpound());
result = psmt.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
DBbean.release(psmt, conn);
}
return result;
}
public List<Question> findAll() throws Exception{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
List<Question> questions = new ArrayList<Question>();
try {
conn = (Connection) DBbean.getConnection();
stmt = (Statement) conn.createStatement();
rs = stmt.executeQuery(FIND_ALL_SQL);
while(rs.next()){
Question ques = new Question();
ques.setId(rs.getInt("id"));
ques.setQ_type(rs.getInt("q_type"));
ques.setTitle(rs.getString("title"));
ques.setOptionA(rs.getString("optionA"));
ques.setOptionB(rs.getString("optionB"));
ques.setOptionC(rs.getString("optionC"));
ques.setOptionD(rs.getString("optionD"));
ques.setTips(rs.getString("tips"));
ques.setAnswer(rs.getString("answer"));
ques.setExpound(rs.getString("expound"));
questions.add(ques);
}
return questions;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
DBbean.release(rs,stmt, conn);
}
return null;
}
public int updata(Question qut) throws Exception{
int result = 0;
Connection conn = null;
PreparedStatement psmt = null;
try {
conn = (Connection) DBbean.getConnection();
psmt = conn.prepareStatement(UPDATE_SQL);
psmt.setInt(1, qut.getQ_type());
psmt.setString(2, qut.getTitle());
psmt.setString(3, qut.getOptionA());
psmt.setString(4, qut.getOptionB());
psmt.setString(5, qut.getOptionC());
psmt.setString(6, qut.getOptionD());
psmt.setString(7, qut.getTips());
psmt.setString(8, qut.getAnswer());
psmt.setString(9, qut.getExpound());
psmt.setInt(10, qut.getId());
result = psmt.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
DBbean.release(psmt, conn);
}
return result;
}
public Question find(int id) throws Exception{
Connection conn = null;
PreparedStatement psmt = null;
ResultSet rs = null;
Question quest = new Question();
try {
conn = (Connection) DBbean.getConnection();
psmt = conn.prepareStatement(SELECT_SQL);
psmt.setInt(1, id);
rs = psmt.executeQuery();
if (rs.next()) {
quest.setId(rs.getInt("id"));
quest.setQ_type(rs.getInt("q_type"));
quest.setTitle(rs.getString("title"));
quest.setOptionA(rs.getString("optionA"));
quest.setOptionB(rs.getString("optionB"));
quest.setOptionC(rs.getString("optionC"));
quest.setOptionD(rs.getString("optionD"));
quest.setTips(rs.getString("tips"));
quest.setAnswer(rs.getString("answer"));
quest.setExpound(rs.getString("expound"));
}else {
quest = null;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
DBbean.release(rs, psmt, conn);
}
return quest;
}
public int remove(int id) throws Exception{
int result = 0;
Connection conn = null;
PreparedStatement psmt = null;
try {
conn = (Connection) DBbean.getConnection();
psmt = conn.prepareStatement(DELETE_SQL);
psmt.setInt(1, id);
result = psmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
DBbean.release(psmt, conn);
}
return result;
}
}
2.建立数据库操作抽象类
在dao包下新建IQustionDAO.java文件(Question写错了,我也懒得改了),抽象类由interface修饰,抽象方法由abstract修饰,主要是对DAO层的数据库操作增删改查方法进行抽象,它的作用是让用户不可见底层使前后端分离保证数据的安全性,代码如下:
public interface IQustionDAO {
public abstract List<Question> findAll() throws Exception;
public abstract Question find(int id) throws Exception;
public abstract int remove(int id) throws Exception;
public abstract int updata(Question qut) throws Exception;
public abstract int create(Question qut) throws Exception;
}
五丶在service层实现对用户可见方法
在service包中新建一个QuestionService.java文件,这里的方法是供给用户调用的,对用户是可见的,而DAO层的数据库方法对用户是不可见的,service层代码如下:
public class QuestionService {
private QuestionDAO dao = new QuestionDAO();
public List<Question> finAll(){
List<Question> questions = new ArrayList<Question>();
try {
questions = dao.findAll();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return questions;
}
public int UpdataQut(Question qut){
int result = 0;
try {
result = dao.updata(qut);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return result;
}
public Question FindById(int id){
Question ques = new Question();
try {
ques = dao.find(id);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return ques;
}
public int insert(Question questi){
int result = 0;
try {
result = dao.create(questi);
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
return result;
}
public int DeleteQut(int id){
int result = 0;
try {
result = dao.remove(id);
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
return result;
}
}
六丶建立过滤器
过滤器的作用是在插入数据或者是修改数据时不会产生中文乱码
1.建立filter文件
在包filter下新建一个CharsetFilter.java,注意文件类型是filter,不是class,代码如下:
@WebFilter("/CharsetFilter")
public class CharsetFilter implements Filter {
/**
* Default constructor.
*/
public CharsetFilter() {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
@SuppressWarnings("unused")
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// place your code here
HttpServletRequest req =(HttpServletRequest)request;
HttpServletResponse resp =(HttpServletResponse)response;
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
CharacterRequest characterRequest = new CharacterRequest(req);
// pass the request along the filter chain
chain.doFilter(characterRequest, response);
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
class CharacterRequest extends HttpServletRequestWrapper{
@SuppressWarnings("unused")
private HttpServletRequest request;
public CharacterRequest(HttpServletRequest request) {
super(request);
this.request = request;
// TODO Auto-generated constructor stub
}
public String getParameter(String name){
String value = super.getParameter(name);
if(value==null){
return null;
}
String method = super.getMethod();
if("get".equalsIgnoreCase(method)){
try{
value=new String(
value.getBytes("iso-8859-1"),"utf-8");
}catch(UnsupportedEncodingException e){
throw new RuntimeException(e);
}
}
return value;
}
}
2.建立配置过滤器
建立CharsetFilter.java后还得在xml文件中配置它才能有效,在WEB-INF下建立web.xml文件,你需要更改的地方是filter-class,改为CharsetFilter.java所在的路径名,也就是在项目的那个包下,代码如下:
<?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">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<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>
<filter>
<filter-name>CharsetFilter</filter-name>
<filter-class>li.filter.CharsetFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharsetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
七丶controller层,做增删改查的servlet控制层
在这之前我们得在lib下导入jstl.jar包和standard.jar包
jstl.jar下载
standard.jar下载
1.新建admin丶images文件夹
在WebContent下新建一个admin文件夹用于存放增删改查的jsp文件,imges是存放图片的
2.搭一个欢迎的框架
2.1 index.jsp页面丶index_left.jsp页面丶index_right.jsp页面丶index_title.jsp页面
它们全都放在WebContent根目录下,用于欢迎用户
index.jsp代码如下
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>题目信息管理系统</title>
</head>
<frameset rows="80,*">
<frame src="index_title.jsp" scrolling="no">
<frameset cols="140,*">
<frame src="index_left.jsp" scrolling="no">
<frame src="index_right.jsp" name="right" scrolling="auto">
</frameset>
</frameset>
</html>
index_left.jsp代码如下:
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>功能列表</title>
<style type="text/css">
*{
font-size:10pt;
text-align: center;
}
div {
background: #87CEFA;
margin: 3px;
padding: 3px;
}
a {
text-decoration: none;
}
table {
border-collapse: collapse;
font-family: Futura, Arial, sans-serif;
}
table {
border-collapse: collapse;
font-family: Futura, Arial, sans-serif;
}
caption {
font-size: larger;
margin: 1em auto;
}
th,td {
padding: .65em;
}
th,td {
border-bottom: 1px solid #ddd;
border-top: 1px solid #ddd;
text-align: center;
}
tbody tr:hover {
background: linear-gradient(#fff,#aaa);
font-size: 17px;
}
</style>
</head>
<table class="table">
<tr>
<td>
<p><a href="admin/insert_qut.jsp" target="right">新增题目</a></p></td></tr>
<tr><td>
<p><a href="qut.list" target="right">查询全部题目</a></p></td></tr>
<tr><td>
<p><a href="admin/find_qut.jsp" target="right">查询题目信息</a></p></td></tr>
<tr><td>
<p><a href="admin/updata_qut.jsp" target="right">修改题目信息</a></p></td></tr>
<tr><td>
<p><a href="admin/delete_qut.jsp" target="right">删除题目</a></p></td></tr>
</table>
</html>
index_right.jsp页面代码如下:
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>信息显示页面</title>
</head>
<body>
<body style="background:url(images/qian.jpg);text-align:center;background-size:100% 100%;background-attachment: fixed;">
<h1>欢迎,欢迎进入命题系统!</h1>
</body>
</body>
</html>
index_title.jsp页面代码如下:
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
body {
background: #4682B4;
}
a {
text-transform:none;
text-decoration:none;
}
a:hover {
text-decoration:underline;
}
</style>
</head>
<body>
<center><h1><b>题目信息管理系统</b></h1></center>
</body>
</html>
运行后图:
2.2 新建两个反馈页面error.jsp和success.jsp
它们全都放在WebContent根目录下
error.jsp页面代码如下:
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>失败</title>
<style type="text/css">
.body {
background: url(../images/shuimo5.jpg);
background-position: center center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
}
</style>
</head>
<body class="body">
<h1>操作失败!</h1><br/>
</body>
</html>
success.jsp页面
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>成功</title>
</head>
<body>
<h1>操作成功!</h1><br/>
</body>
</html>
3.查询所有题目
3.1 建jsp页面
在admin文件夹下新建list_question.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"
prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>题目列表</title>
<style type="text/css">
body {
background: url(../images/shuimo5.jpg);
background-position: center center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
}
.main2{
text-align: center;
background-color: #FFDEAD;
border-radius: 20px;
width:100%;
height: 100%;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
table {
border-collapse: collapse;
font-family: Futura, Arial, sans-serif;
}
caption {
font-size: larger;
margin: 1em auto;
}
th,td {
padding: .65em;
}
th,td {
border-bottom: 1px solid #ddd;
border-top: 1px solid #ddd;
text-align: center;
}
tbody tr:hover {
background: linear-gradient(#fff,#aaa);
font-size: 17px;
}
</style>
</head>
</head>
<body>
<div >
<c:if test="${que==null }"> 没有题目!<br/></c:if>
<table border="1" >
<caption>Topic Information Management System</caption>
<thead>
<tr>
<th>ID
<th>题型
<th>题目
<th>答案A
<th>答案B
<th>答案C
<th>答案D
<th>提示
<th>正确答案
<th>解析
</tr>
</thead>
<c:forEach var="question" items="${que }">
<tr>
<td>${question.id }</td><td>${question.q_type }</td>
<td>${question.title }</td><td>${question.optionA }</td>
<td>${question.optionB }</td><td>${question.optionC }</td>
<td>${question.optionD }</td><td>${question.tips}</td>
<td>${question.answer }</td><td>${question.expound}</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
3.2 servlet请求QutAll.java实现
在controller包下新建QutAll.java,注意这是servlet文件类型,代码如下:
/**
* Servlet implementation class QtuAll
*/
@WebServlet("/qut.list")
public class QutAll extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public QutAll() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
QuestionService service = new QuestionService();
List<Question> que = new ArrayList<Question>();
que = service.finAll();
request.setAttribute("que", que);
request.getRequestDispatcher("admin/list_question.jsp").forward(request, response);
}
}
3.3 运行效果图
4.更新题目信息
4.1 建jsp页面
在admin文件夹下新建updata_qut.jsp代码如下:
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改题目</title>
<style type="text/css">
body {
background: url(../images/shuimo4.jpg);
background-position: center center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
}
.main3{
text-align: center;
background-color: #fff;
border-radius: 20px;
width:400px;
height: 570px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
.text{
width:95%;
height:80%;
}
.button { /* 按钮美化 */
width: 100px; /* 宽度 */
height: 40px; /* 高度 */
border-width: 0px; /* 边框宽度 */
border-radius: 3px; /* 边框半径 */
background: #1E90FF; /* 背景颜色 */
cursor: pointer; /* 鼠标移入按钮范围时出现手势 */
outline: none; /* 不显示轮廓线 */
font-family: Microsoft YaHei; /* 设置字体 */
color: white; /* 字体颜色 */
font-size: 17px; /* 字体大小 */
}
.button:hover { /* 鼠标移入按钮范围时改变颜色 */
background: #5599FF;
}
table {
border-collapse: collapse;
font-family: Futura, Arial, sans-serif;
}
caption {
font-size: larger;
margin: 1em auto;
}
th,td {
padding: .65em;
}
th,td {
border-bottom: 1px solid #ddd;
border-top: 1px solid #ddd;
text-align: center;
}
tbody tr:hover {
background: linear-gradient(#fff,#aaa);
font-size: 17px;
}
</style>
</head>
<body >
<div class="main3">
<form action="${pageContext.request.contextPath}/qut.updata" method="post">
<table border="1" width="400" height="500" class="table">
<caption>修改目标题目信息</caption>
<tr><td>ID:</td><td><input type="text" name="uid" class="text"></td></tr>
<tr><td>题型:</td><td><input type="text" name="uq_type" class="text"></td></tr>
<tr><td>题目:</td><td><input type="text" name="utitle" class="text"></td></tr>
<tr><td>答案A:</td><td><input type="text" name="uoptionA" class="text"></td></tr>
<tr><td>答案B:</td><td><input type="text" name="uoptionB" class="text"></td></tr>
<tr><td>答案C:</td><td><input type="text" name="uoptionC" class="text"></td></tr>
<tr><td>答案D:</td><td><input type="text" name="uoptionD" class="text"></td></tr>
<tr><td>提示:</td><td><input type="text" name="utips" class="text"></td></tr>
<tr><td>正确答案:</td><td><input type="text" name="uanswer" class="text"></td></tr>
<tr><td>解析:</td><td><input type="text" name="uexpound" class="text"></td></tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="修改" class="button">
<input type="reset" value="取消" class="button">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
4.2 servlet请求QutUpdatas.java实现
在controller包下新建QutUpdatas,注意这是servlet文件类型,代码如下:
/**
* Servlet implementation class QutIpate
*/
@WebServlet("/qut.updata")
public class QutUpdata extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public QutUpdata() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
int id = Integer.parseInt(request.getParameter("uid"));
int q_type = Integer.parseInt( request.getParameter("uq_type"));
String title = request.getParameter("utitle");
String optionA = request.getParameter("uoptionA");
String optionB = request.getParameter("uoptionB");
String optionC = request.getParameter("uoptionC");
String optionD = request.getParameter("uoptionD");
String tips = request.getParameter("utips");
String answer = request.getParameter("uanswer");
String expound = request.getParameter("uexpound");
Question ques_u = new Question();
ques_u.setId(id);
ques_u.setQ_type(q_type);
ques_u.setTitle(title);
ques_u.setOptionA(optionA);
ques_u.setOptionB(optionB);
ques_u.setOptionC(optionC);
ques_u.setOptionD(optionD);
ques_u.setTips(tips);
ques_u.setAnswer(answer);
ques_u.setExpound(expound);
QuestionService service = new QuestionService();
int result = service.UpdataQut(ques_u);
if (result>0) {
response.sendRedirect("success.jsp");
}else {
response.sendRedirect("error.jsp");
}
}
}
4.3 运行效果图
5.新增题目
5.1 建jsp页面
在admin文件夹下新建insert_qut.jsp代码如下:
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>新增题目</title>
<style type="text/css">
body {
background: url(../images/back.jpg);
background-position: center center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
}
.main1{
text-align: center;
background-color: #fff;
border-radius: 20px;
width:400px;
height: 565px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
.text{
width:95%;
height:80%;
}
.button { /* 按钮美化 */
width: 100px; /* 宽度 */
height: 40px; /* 高度 */
border-width: 0px; /* 边框宽度 */
border-radius: 3px; /* 边框半径 */
background: #1E90FF; /* 背景颜色 */
cursor: pointer; /* 鼠标移入按钮范围时出现手势 */
outline: none; /* 不显示轮廓线 */
font-family: Microsoft YaHei; /* 设置字体 */
color: white; /* 字体颜色 */
font-size: 17px; /* 字体大小 */
}
.button:hover { /* 鼠标移入按钮范围时改变颜色 */
background: #5599FF;
}
table {
border-collapse: collapse;
font-family: Futura, Arial, sans-serif;
}
caption {
font-size: larger;
margin: 1em auto;
}
th,td {
padding: .65em;
}
th,td {
border-bottom: 1px solid #ddd;
border-top: 1px solid #ddd;
text-align: center;
}
tbody tr:hover {
background: linear-gradient(#fff,#aaa);
font-size: 17px;
}
</style>
</head>
<body>
<div class="main1">
<form action="${pageContext.request.contextPath}/qut.insert" method="post">
<table border="1" width="400" height="500" class="table">
<caption>新增题目(New Topics)</caption>
<tr><td>ID:</td><td><input type="text" name="iid" class="text"></td></tr>
<tr><td>题型:</td><td><input type="text" name="iq_type" class="text"></td></tr>
<tr><td>题目:</td><td><input type="text" name="ititle" class="text"></td></tr>
<tr><td>答案A:</td><td><input type="text" name="ioptionA" class="text"></td></tr>
<tr><td>答案B:</td><td><input type="text" name="ioptionB" class="text"></td></tr>
<tr><td>答案C:</td><td><input type="text" name="ioptionC" class="text"></td></tr>
<tr><td>答案D:</td><td><input type="text" name="ioptionD" class="text"></td></tr>
<tr><td>提示:</td><td><input type="text" name="itips" class="text"></td></tr>
<tr><td>正确答案:</td><td><input type="text" name="ianswer" class="text"></td></tr>
<tr><td>解析:</td><td><input type="text" name="iexpound" class="text"></td></tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="添 加" class="button">
<input type="reset" value="取消" class= "button">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
5.2 servlet请求QutInsert实现
在controller包下新建QutInsert.java,注意这是servlet文件类型,代码如下:
/**
* Servlet implementation class QutInsert
*/
@WebServlet("/qut.insert")
public class QutInsert extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public QutInsert() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
int id = Integer.parseInt(request.getParameter("iid"));
int q_type = Integer.parseInt( request.getParameter("iq_type"));
String title = request.getParameter("ititle");
String optionA = request.getParameter("ioptionA");
String optionB = request.getParameter("ioptionB");
String optionC = request.getParameter("ioptionC");
String optionD = request.getParameter("ioptionD");
String tips = request.getParameter("itips");
String answer = request.getParameter("ianswer");
String expound = request.getParameter("iexpound");
Question iques = new Question();
iques.setId(id);
iques.setQ_type(q_type);
iques.setTitle(title);
iques.setOptionA(optionA);
iques.setOptionB(optionB);
iques.setOptionC(optionC);
iques.setOptionD(optionD);
iques.setTips(tips);
iques.setAnswer(answer);
iques.setExpound(expound);
QuestionService iservice = new QuestionService();
int result = iservice.insert(iques);
if (result>0) {
response.sendRedirect("success.jsp");
}else {
response.sendRedirect("error.jsp");
}
}
}
5.3 运行效果图
6.寻找题目
6.1 jsp页面
在admin文件夹下新建find_qut.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"
prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询题目</title>
<style type="text/css">
body {
background: url(../images/shuimo5.jpg);
background-position: center center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
}
.main{
text-align: center;
border-radius: 20px;
width:100%;
height: 200px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
.button { /* 按钮美化 */
width: 100px; /* 宽度 */
height: 40px; /* 高度 */
border-width: 0px; /* 边框宽度 */
border-radius: 3px; /* 边框半径 */
background: #1E90FF; /* 背景颜色 */
cursor: pointer; /* 鼠标移入按钮范围时出现手势 */
outline: none; /* 不显示轮廓线 */
font-family: Microsoft YaHei; /* 设置字体 */
color: white; /* 字体颜色 */
font-size: 17px; /* 字体大小 */
}
.button:hover { /* 鼠标移入按钮范围时改变颜色 */
background: #5599FF;
}
</style>
</head>
<body >
<div class="main" >
<form action="${pageContext.request.contextPath }/qut.find" method="post">
<table border="1" >
<caption><h2>请在下面输入要查询的题号</h2></caption>
<tr>
<td>题号:</td>
<td><input type="text" name="fid"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="查询" class= "button"/>
<input type="reset" value = "重置" class= "button"/>
</td>
</tr>
</table>
</form>
<hr>
<c:if test="${singleQut!=null }">
<table border="1">
<tr>
<td>ID</td><td>题型</td><td>题目</td><td>答案A</td><td>答案B</td><td>答案C</td><td>答案D</td><td>提示</td><td>正确答案</td><td>解析</td><td>操作</td>
</tr>
<tr>
<td>${singleQut.id }</td>
<td>${singleQut.q_type }</td>
<td>${singleQut.title }</td>
<td>${singleQut.optionA }</td>
<td>${singleQut.optionB }</td>
<td>${singleQut.optionC }</td>
<td>${singleQut.optionD }</td>
<td>${singleQut.tips}</td>
<td>${singleQut.answer }</td>
<td>${singleQut.expound}</td>
<td><a href="qut.updata?id=${singleQut.id }">修改</a> |
<a href="qut.delete?id=${singleQut.id }">删除</a>
</td>
</tr>
</table>
</c:if>
<c:if test="${singleQut==null }">
没有此题。
</c:if>
</div>
</body>
</html>
6.2 servlet请求QutFind实现
在controller包下新建QutFind.java,注意这是servlet文件类型,代码如下:
/**
* Servlet implementation class QutFind
*/
@WebServlet("/qut.find")
public class QutFind extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public QutFind() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
if("".equals(request.getParameter("fid"))){
response.sendRedirect("admin/find_qut.jsp");
}else {
int id = Integer.parseInt(request.getParameter("fid"));
QuestionService service = new QuestionService();
Question singleQut = new Question();
singleQut = service.FindById(id);
request.setAttribute("singleQut", singleQut);
request.getRequestDispatcher("admin/find_qut.jsp").forward(request, response);
}
}
}
6.3 运行效果图
7.删除题目
7.1 jsp页面
在admin文件夹下新建delete_qut.jsp代码如下:
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>删除题目</title>
<style type="text/css">
body {
background: url(../images/shuimo1.jpg);
background-position: center center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
}
.button { /* 按钮美化 */
width: 100px; /* 宽度 */
height: 40px; /* 高度 */
border-width: 0px; /* 边框宽度 */
border-radius: 3px; /* 边框半径 */
background: #1E90FF; /* 背景颜色 */
cursor: pointer; /* 鼠标移入按钮范围时出现手势 */
outline: none; /* 不显示轮廓线 */
font-family: Microsoft YaHei; /* 设置字体 */
color: white; /* 字体颜色 */
font-size: 17px; /* 字体大小 */
}
.button:hover { /* 鼠标移入按钮范围时改变颜色 */
background: #5599FF;
}
</style>
<script>
function sc1() {
document.getElementById("tt").style.top = (document.documentElement.scrollTop + (document.documentElement.clientHeight - document.getElementById("tt").offsetHeight) / 2) + "px";
document.getElementById("tt").style.left = (document.documentElement.scrollLeft + (document.documentElement.clientWidth - document.getElementById("tt").offsetWidth) / 2) + "px";
}
</script>
</head>
<body onload="sc1()">
<div id="tt" style="width:240px;height:150px;position:absolute; ">
<form action="${pageContext.request.contextPath }/qut.delete" method="post">
<table border="1" >
<h2>请输入要删除的题号</h2>
<tr>
<td>题号:</td>
<td><input type="text" name="did"/></td>
</tr>
</table>
<input type="submit" value="删除" class="button"/>
<input type="reset" value = "重置" class="button"/>
</form>
</div>
</body>
</html>
7.2 servlet请求QutDelete实现
在controller包下新建QutDelete.java,注意这是servlet文件类型,代码如下:
@WebServlet("/qut.delete")
public class QutDelete extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public QutDelete() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
int id = Integer.parseInt(request.getParameter("did"));
QuestionService dservice = new QuestionService();
int result = dservice.DeleteQut(id);
if (result>0) {
response.sendRedirect("success.jsp");
}else {
response.sendRedirect("error.jsp");
}
}
}
7.3 运行效果图
八丶总结
这是工作室课程的一个作业,搞了两天终于完成,通过这次做我也理清了思路。第一次写博客还希望大家见谅,转载请注明出处哦,码出来还是不易的。呼~睡觉!