---恢复内容开始---
---恢复内容开始---
1.post表单提交,中文乱码:request.setCharacterEncoding("utf-8");
2.提高用户体验,密码用户名输入错误后返回登录界面,表单内容未被清除:可以在servlet中传入值,在jsp页面接收
servlet中:
request.setAttribute("userName", userName);
request.setAttribute("password", password);
jsp页面中:
<input type="text" value="${userName }" name="userName" id="userName"/>
<input type="password" value="${password }" name="password" id="password"/>
3.权限验证 Session:
//获取Session
HttpSession session = request.getSession();
session.setAttribute("currentUser",currentUser);
---恢复内容结束---
4.页面级验证用户是否登录:
<%
//权限验证
if(session.getAttribute("currentUser")==null){
System.out.println("滚");
response.sendRedirect("index.jsp");
return;
}
%>
5.Mysql分页
page 当前页
rows 每页的记录数
String * from table limit start,size;
start=(page-1)*rows;
6.json jar包导入
Json-lib-2.2.3-jdk15.jar, JSONArray格式为name:value,
封装方法将ResultSet转成JSONArray格式
public static JSONArray formatRsToJsonArray(ResultSet rs) throws Exception{
ResultSetMetaData md = rs.getMetaData();
int num=md.getColumnCount();
JSONArray array = new JSONArray();
while(rs.next()){
JSONObject mapOfColValues = new JSONObject();
for(int i=1; i<=num; i++){
mapOfColValues.put(md.getColumnClassName(i),rs.getObject(i));
}
}
return null;
}
7.封装向页面输出:
public class ResponseUtil {
public static void write(HttpServletResponse response, JSONObject
jsonObject)throws Exception{
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println(jsonObject.toString());
out.flush();
out.close();
}
8.使用SimpleDateFormat封装日期格式,使用instanceof判断对象类型
public static String formatDate(Date date, String format){
String result = "";
SimpleDateFormat sdf = new SimpleDateFormat(format);
if(date != null){
result = sdf.format(date);
}
return result;
}
Object o = rs.getObject(i);
if(o instanceof Date){
mapOfColValues.put(md.getColumnName(i),DateUtil.formatDate((Date)o, "yyyy-MM-dd"));
}else{
mapOfColValues.put(md.getColumnName(i),rs.getObject(i));
}
9.多表关联时,要注意主键id命名时的区分,否则系统会不知道怎么处理
10.取异步数据
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Connection con = null;
try {
con = dbUtil.getCon();
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)));
ResponseUtil.write(response, jsonArray);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally{
try {
dbUtil.closeCon(con);
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
data-options="editable:false,valueField:'id',textField:'gradeName',url:'gradeComboList'"
11.jquery取值
$("#s_stuNo").val()
12.使用StringBuffer类的append方法时注意空格
public ResultSet studentList(Connection con, PageBean pageBean, Student student, String bbrithday, String ebrithday) throws Exception{
StringBuffer sb = new StringBuffer("select * from t_student s, t_grade g where g.id=s.gradeId");
if(StringUtil.isNotEmpty(student.getStuNo())){
sb.append(" and s.stuNo like '%"+student.getStuNo()+"%' ");
}
if(StringUtil.isNotEmpty(student.getStuName())){
sb.append("and s.stuName like '%"+student.getStuName()+"%' ");
}
if(StringUtil.isNotEmpty(student.getSex())){
sb.append("and s.sex ='"+student.getSex()+"' ");
}
if(StringUtil.isNotEmpty(bbrithday)){
sb.append("and TO_DAYS(s.brithday) >= TO_DAYS('"+bbrithday+"') ");
}if(StringUtil.isNotEmpty(ebrithday)){
sb.append("and TO_DAYS(s.brithday) <= TO_DAYS('"+ebrithday+"') ");
}
if(student.getGradeId() != -1){
sb.append("and s.gradeId ='"+student.getGradeId()+"' ");
}
if(pageBean != null){
sb.append(" limit "+pageBean.getStart()+","+pageBean.getRows());
}
PreparedStatement pstmt = con.prepareStatement(sb.toString());
return pstmt.executeQuery();
}
---恢复内容结束---