1、防止中文乱码
代码语言:javascript复制<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
2、jsp页面跳转
代码语言:javascript复制request.getRequestDispatcher("./index.jsp").forward(request, response); //请求转发
response.sendRedirect("error.jsp"); //请求重定向
3、使用servlet注解
代码语言:javascript复制@WebServlet("/XXXServlet")
4、jsp获取当前路径
代码语言:javascript复制${pageContext.request.contextPath}/xxxx
5、js常用代码
代码语言:javascript复制<button onclick="funct1(1)" class="" type="submit"> 确认弹窗</button> <br>
<a class="" href="javascript:funct2(2)">警告弹窗</a> <br>
<a class="" href="javascript:history.back(-1)">返回上一级</a>
function funct1(id){
if(confirm("确定选择?")){
window.location.href="${pageContext.request.contextPath}/XXXServlet?method=Confirm&id=" id;
}
}
function funct2(id){
alert(id)
}
6、接收和传递参数
代码语言:javascript复制String username = request.getParameter("username"); //可从form表单或者页面对象获取
String password = request.getParameter("password");
request.setAttribute("list", list); //变量、对象和集合都可以存储
7、JSTL标签使用
代码语言:javascript复制<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:forEach var="list" items="${list }">
<tr>
<td>${list.name }</td>
<td>${list.sex }</td>
<td>${list.age }</td>
<td>${list.telphone}</td>
<td>${list.address }</td>
</tr>
</c:forEach>
8、CRUD实现步骤
(1)连接数据库 Connection conn = DBUtil.getConnection(); (2)预编译sql语句 PreparedStatement ps = conn.prepareStatement(sql); (3)查询返回对象 ResultSet rs = ps.executeQuery(); (4)遍历结果,将字段存入对象,再将对象存入list while(rs.next()){User user = new User(); user.setName(rs.getString("name")).. list.add(user)} return list
9、Maven配置(mysql、jstl、jsp、servlet)
代码语言:javascript复制 mysql
mysql-connector-java
5.1.47
javax.servlet
javax.servlet-api
3.1.0
provided
javax.servlet.jsp
javax.servlet.jsp-api
2.3.1
provided
jstl
jstl
1.2
taglibs
standard
1.1.2
10、jstl类型转换 int转String
代码语言:javascript复制 <c:set var="teaid">
<c:out value="${list.teaid }"/>
</c:set>
11、servlet实现javascript
代码语言:javascript复制1、获取PrintWriter对象
PrintWriter out = response.getWriter();
2、实现js
// 警告框
out.write("alert('警告框');");
// 确认框
out.write("confirm('确认弹框!');");
// 重新加载页面
out.write("location.reload();");
// 放回当前页面
out.write("history.go(-1);");
3、关闭流
out.flush();
out.close();