使用Tomcat+easyui+httpclient+json+MySQL技术写的一个入门级web应用。
Github-StudentInfoManager
该包内的文件包含大部分的业务逻辑处理,继承HttpServlet,处理get和post请求。
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", "");
jsonObject.put("gradeName", "请选择班级...");
jsonArray.add(jsonObject);
jsonArray.addAll(JsonUtil.formatRsToJsonArray(gradeDao.gradeList(con, null, null)));
com.redsheep.util.ResponseUtil.write(response, jsonArray);
JSONObject result = new JSONObject();
String str[] = delIds.split(",");
for (int i = 0; i < str.length; i++) {
boolean f = studentDao.getStudentByGradeId(con, str[i]);
if (f) {
result.put("errorIndex", i);
result.put("errorMsg", "班级下面有学生,不能删除!");
ResponseUtil.write(resp, result);
return;
}
}
int delNums = gradeDao.gradeDelete(con, delIds);
if (delNums > 0) {
result.put("success", "true");
result.put("delNums", delNums);
} else {
result.put("errorMsg", "删除失败");
}
ResponseUtil.write(resp, result);
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
//分页,获取页数和行数
String page = req.getParameter("page");
String rows = req.getParameter("rows");
String gradeName = req.getParameter("gradeName");
if (gradeName == null) {
gradeName = "";
}
Grade grade = new Grade();
grade.setGradeName(gradeName);
PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
Connection con = null;
try {
con = dbUtil.getCon();
JSONObject result = new JSONObject();
JSONArray jsonArray = JsonUtil.formatRsToJsonArray(gradeDao.gradeList(con, pageBean, grade));
int total = gradeDao.gradeCount(con, grade);
//获取总行数和总条量
result.put("rows", jsonArray);
result.put("total", total);
ResponseUtil.write(resp, result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
DbUtil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置请求的编码
request.setCharacterEncoding("utf-8");
String gradeName = request.getParameter("gradeName");
String gradeDesc = request.getParameter("gradeDesc");
String id = request.getParameter("id");
Grade grade = new Grade(gradeName, gradeDesc);
if (StringUtil.isNotEmpty(id)) {
grade.setId(Integer.parseInt(id));
}
Connection con = null;
try {
con = dbUtil.getCon();
int saveNums = 0;
JSONObject result = new JSONObject();
if (StringUtil.isNotEmpty(id)) {
saveNums = gradeDao.gradeModify(con, grade);
} else {
saveNums = gradeDao.gradeAdd(con, grade);
}
if (saveNums > 0) {
result.put("success", "true");
} else {
result.put("success", "true");
result.put("errorMsg", "保存失败");
}
ResponseUtil.write(response, result);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
dbUtil.closeCon(con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String userName = req.getParameter("userName");
String password = req.getParameter("password");
req.setAttribute("userName", userName);
req.setAttribute("password", password);
//身份验证
if (StringUtil.isEmpty(userName) || StringUtil.isEmpty(password)) {
req.setAttribute("error", "用户名或密码为空");
req.getRequestDispatcher("index.jsp").forward(req, resp);
return;
}
User user = new User(userName, password);
Connection con = null;
try {
con = dbUtil.getCon();
User currentUser = userDao.login(con, user);
if (currentUser == null) {
req.setAttribute("error", "用户名或密码错误");
// 服务器跳转
req.getRequestDispatcher("index.jsp").forward(req, resp);
} else {
// 获取Session
HttpSession session = req.getSession();
session.setAttribute("currentUser", currentUser);
// 客户端跳转
resp.sendRedirect("main.jsp");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
dbUtil.closeCon(con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<form action="login" method="post">
<table style="border-collapse:separate; border-spacing:10px;">
<tr>
<td>姓名:td>
<td><input type="text" name="userName" value="${userName }" id="userName"/>td>
tr>
<tr>
<td>密码:td>
<td><input type="password" name="password" value="${password }" id="password"/>td>
tr>
<tr>
<td align="left"><input type="button" value="重置" onclick="resetValue()"/>td>
<td align="right"><input type="submit" value="登录"/>td>
tr>
table>
<servlet>
<servlet-name>loginServletservlet-name>
<servlet-class>com.redsheep.web.LoginServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>loginServletservlet-name>
<url-pattern>/loginurl-pattern>
servlet-mapping>
<%
if(session.getAttribute("currentUser")==null){
response.sendRedirect("index.jsp");
return;
}
%>
欢迎使用
也可以编写JS代码来创建
$('#cc').combobox({
url: ...,
required: true,
valueField: 'id',
textField: 'text'
});
编号
班级名称
班级描述
<div id="tb">
<div>
<a href="javascript:openGradeAddDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a>
<a href="javascript:openGradeModifyDialog()" class="easyui-linkbutton" iconCls="icon-edit" plain="true">修改</a>
<a href="javascript:deleteGrade()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除</a>
</div>
<div> 班级名称: <input type="text" name="s_gradeName" id="s_gradeName"/><a href="javascript:searchGrade()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a></div>
</div>
性别:
自动填充班级信息数据功能
所属班级:
出生日期:
->