异常一 创建人 | | | | | | | | | | | public class ServletDemo5 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
servletContext.setAttribute("msg","hello");
String realPath = servletContext.getRealPath("/a.txt");
System.out.println(realPath);
String realPath2 = servletContext.getRealPath("/WEB-INF/b.txt");
System.out.println(realPath2);
String realPath3 = servletContext.getRealPath("/WEB-INF/classes/a.txt");
System.out.println(realPath3);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(“request”, “response”);
}
} | | | 问题解决方法: Dopost方法应该传对象,而不是传进字符串 |
异常二 创建人 | | | | | | | | | public class UserDao {
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
public User login(User loginUser) {
try {
String sql = "select * from user where username= ? and password=?";
User user = template.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), loginUser.getUsername(), loginUser.getPassword());
return user;
}
//注意此行,一定要捕捉这个异常!并返回null值
catch (DataAccessException e){
return null;
}
}
} | | | | 问题分析: 没有捕获DataAcessException异常,导致null无法返回数据 | 问题解决方法: 添加DataAccessExeption的catch。 |
异常三 创建人 | | | | | | | | | | | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
window.onload = function () {
var elementById = document.getElementById("checkCode");
var elementById2 = document.getElementById("change");
elementById.onclick = function () {
var date = new Date().getTime();
elementById.src = "/checkCodeServlet?" + date;
}
elementById2.onclick = function () {
var date = new Date().getTime();
elementById.src = "/checkCodeServlet?" + date;
}
}
</script>
</head>
<body>
<img src="/checkCodeServlet" id="checkCode">
<a id="change" style="cursor:pointer;color: blue;"><u>看不清?换一张</u></a>
</body>
</html> | | | |
异常四 创建人 | | | | | | | | | public class ServletRedirect1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("R1....");
response.sendRedirect("/test/ServletRedirect2");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
} | | | | | |
异常五 创建人 | | | | | | | | | | | @WebServlet("/successServlet") | | 问题分析: 程序无法找正确的servlet,是因为servlet中路径编写不正确 | |
|
|
|
|