首页 > 代码库 > servlet_jsp 常用操作及错误
servlet_jsp 常用操作及错误
1.servlet 返回操作结果在Jsp页面显示:
实例1:
boolean result=empbiz.saveEmpInfo(emp);
response.setContentType("text/html; charset=UTF-8");
String s="添加失败!";
if (result) {
s="添加成功!";
}
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8"); //不加可能乱码
PrintWriter out=response.getWriter();
out.write("<script type=‘text/javascript‘>alert(‘"+s+"‘);window.location=‘Jsp/AddNewEmp.jsp‘;</script>");
out.flush();
out.close();
实例2:
boolean result=empbiz.saveEmpInfo(emp);
response.setContentType("text/html; charset=UTF-8");
String s="添加失败!";
if (result) {
s="添加成功!";
}
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8"); //不加可能乱码
2.servlet之间传值:
getServletConfig().getServletContext().getRequestDispatcher("/DeptList").forward(request, response);
3.提示:纯文本文件的字符编码未声明。如果该文件包含 US-ASCII 范围之外的字符,该文件将
jsp页面缺少:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4.级联添加属性时:
// 根据页面传来的deptno 给emp 对象赋值
if (null != deptno && !"".equals(deptno)) {
Dept dept = deptbiz.getDeptInfo(Integer.parseInt(deptno));
emp.setDept(dept);
}
5.Date编码:
// 格式化
if (null != hiredate && !"".equals(hiredate)) {
Date hDate;
try {
hDate = DateUtil.strToDate(hiredate, "yyyy-MM-dd");
emp.setHiredate(hDate);
} catch (Exception e) {
System.out.println("时间转换失败!");
e.printStackTrace();
}
}
包:
package com.TwoTable.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
/**
* 把日期字符串转为java.util.Date类型
*/
public static Date strToDate(String dateStr, String parttern)
throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat(parttern);
return sdf.parse(dateStr);
}
/**
* 获取近若干个月的日期
*/
public static Date getDate(int num) throws Exception {
// 得到当前系统日历
Calendar calendar = Calendar.getInstance();
// 获取近一个月的日历
calendar.add(Calendar.MONTH, num);
// 把日历转为日期类型
Date date = calendar.getTime();
System.out.println(date);
return date;
}
}
2.jsp页面Date 编码 添加日期控件
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<script type="text/javascript" src="http://www.mamicode.com/
<script type="text/javascript" src="http://www.mamicode.com/
<td>入职时间:</td>
<td><input class="input" type="text" id="hiredate"
name="hiredate" value="http://www.mamicode.com/${hiredate}"
onFocus="WdatePicker({dateFmt:‘yyyy-MM-dd‘})" class="Wdate" /></td>
6.jstl实现分页
<table border="0" style="border:#FFFFFF">
<tr>
<td>当前第${page}页</td>
<td id="upPage"><a href="javascript:goPage(${page-1})">上一页</a>
</td>
<c:if test="${totalPage!=1}">
<c:choose>
<c:when test="${requestScope.page<=5}">
<c:forEach var="i" begin="2" end="${requestScope.page}">
<td><a href="http://www.mamicode.com/ViewServlet?currentPage=${i}">[${i }]</a> </td>
</c:forEach>
</c:when>
<c:otherwise>
...
<c:forEach var="i" begin="${requestScope.page-3}"
end="${requestScope.page}">
<td><a href="javascript:goPage(${i})">[${i }]</a> </td>
</c:forEach>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${requestScope.page>=totalPage-4
|| totalPage-4<=0}">
<c:forEach var="i" begin="${requestScope.page+1}"
end="${totalPage}">
<td><a href="javascript:goPage(${i})">[${i }]</a> </td>
</c:forEach>
</c:when>
<c:otherwise>
<c:forEach var="i" begin="${requestScope.page+1}"
end="${requestScope.currentPage+3}">
<td><a href="javascript:goPage(${i})">[${i }]</a> </td>
</c:forEach>
...
<td><a href="javascript:goPage(${totalPage})">
[${totalPage}]</a> </td>
</c:otherwise>
</c:choose>
</c:if>
<td><a href="javascript:goPage(${page+1})">下一页</a></td>
<td>共${totalPage}页</td>
</tr>
</table>
7.实现下拉框填充
<td>工作岗位:</td>
<td>
<div>
<select id="deptno" name="deptno">
<option value="">--请选择--</option>
<c:forEach var="d" items="${deptList}">
<option value="http://www.mamicode.com/${d.deptno}"
<c:if test="${d.deptno==deptno}">selected</c:if>>${d.dname}</option>
</c:forEach>
</select>
</div>
</td>
8.javascript 实现页面跳转的几种方式
1.
<script language="javascript" type="text/javascript">
window.location.href="http://www.mamicode.com/login.jsp?backurl="+window.location.href;
</script>
2.
<script language="javascript" type="text/javascript">
alert("返回");
window.history.back(-1);
</script>
3.
<script language="javascript" type="text/javascript">
window.navigate(top.jsp);
</script>
4.
<script language="javascript" type="text/javascript">
self.location="top.html";
</script>
servlet_jsp 常用操作及错误