项目场景1:过滤器课程作业
问题描述:
在过滤器章节的作业,出现了过滤器正常运行,但运行后却无法跳转至期望页面的现象。@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("过滤器开始");
HttpServletRequest request = (HttpServletRequest) servletRequest;
request.setCharacterEncoding("utf-8");
HttpServletResponse response = (HttpServletResponse) servletResponse;
//仅限登录用户访问
String URI = request.getRequestURI();
HttpSession session = request.getSession();
Object username = session.getAttribute("username");
if ((URI.endsWith("download.jsp") && username == null) || (URI.endsWith("comment.jsp") && username == null)) {
response.sendRedirect("index.jsp");
}
System.out.println("过滤器结束");
}
原因分析:
原因是缺少了关键的语句...
filterChain.doFilter(request,response);
...
在听课的时候老师说这个方法的作用是如果存在第二个过滤器,则会在第一个过滤器结束之前调用第二个过滤器。
但实际上,只有一个过滤器的时候,如果缺少了这个语句,就会导致上述现象的出现。猜测是因为这个函数除了老师所说的功能外,还有跳出过滤器执行过程的功能。无论有几个过滤器,都要加入这个语句,确保过滤器进行过滤后,还能回到主流程继续进行操作。
解决方案:
在doFilter方法的最后加入’filterChain.doFilter(request,response);‘
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("过滤器开始");
HttpServletRequest request = (HttpServletRequest) servletRequest;
request.setCharacterEncoding("utf-8");
HttpServletResponse response = (HttpServletResponse) servletResponse;
//仅限登录用户访问
String URI = request.getRequestURI();
HttpSession session = request.getSession();
Object username = session.getAttribute("username");
if ((URI.endsWith("download.jsp") && username == null) || (URI.endsWith("comment.jsp") && username == null)) {
response.sendRedirect("index.jsp");
}
System.out.println("过滤器结束");
filterChain.doFilter(request,response);
}
即可进行正常跳转
项目场景2:AJAX课程作业
问题描述:
做了这样一个作业,效果是使用AJAX技术,达到点击点赞按钮,动态更新点赞总数的效果。
但是出现了点击按钮,无法更新总数的现象。
浏览器中按F12,报错如下
是程序中出现了错误
原因分析:
NumberFormatException,数字格式化异常,指向了源代码中的doPost方法@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String oneClick = req.getParameter("oneClick");
int point = Integer.parseInt(oneClick);
resp.setContentType("text/html;charset=utf-8");
PrintWriter writer = resp.getWriter();
writer.print(point + 1);
}
但实际上,doPost方法并没有出什么问题,至少应该说,逻辑上并没有出什么问题。那就是说,从oneClick属性中获取的参数有问题。
oneClick参数是通过js代码设置的
<script src="js/jquery-3.5.0.min.js"></script>
<script>
$(function(){
$("#powerUp").click(function () {
var point = $("#point").val();
$.post("powerUp","oneClick=" + point,function(rs){
$("#point").html(rs);
})
})
})
</script>
其中,point事件来自:
<img src="img/班尼特觉得很赞.jpg" id="1">
<button id="powerUp"><img src="img/热情过载.png" id="2"></button>
获得力量:<span id="point">0</span>点
最后通过咨询老师得知,要获取span标签中的内容,不能使用val()方法,要用text()方法。
非常无语的错误- -,都怪自己前端课程学艺不精
解决方案:
将val()方法替换为text()方法即可使程序正常运行<script src="js/jquery-3.5.0.min.js"></script>
<script>
$(function(){
$("#powerUp").click(function () {
var point = $("#point").text();
$.post("powerUp","oneClick=" + point,function(rs){
$("#point").html(rs);
})
})
})
</script>
项目场景:MVC框架之拼写检查的重要性
问题描述:
在跟随课程视频编写MVC框架时,遇到了这个问题
说servlet初始化出现问题,因为流参数是空的
public class DispatcherServlet extends HttpServlet {
@Override
public void init(ServletConfig config) throws ServletException {
String path = config.getInitParameter("contentConfigLocation");//指向了application.properties
//在类加载的时候通过ClassLoader读取配置文件
InputStream is = DispatcherServlet.class.getClassLoader().getResourceAsStream(path);
// Properties ppt = new Properties();//创建一个ppt对象用于解析ppt类型的输入流
// try {
// ppt.load(is);
// } catch (IOException e) {
// e.printStackTrace();
// }//重复的流读取操作会使后面读取同源的流时读不到内容
HandlerMapping.load(is);
}
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>com.ywy.mvc.DispatcherServlet</servlet-class>
<init-param>
<param-name>contentConfigLocation</param-name>
<param-value>application.properties</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
但实际上参数的映射写的也没有问题,指向了application.properties文件
原因分析:
这波只能说操作不够细节 当我检查过程序里流的路劲,检查过web.xml文件里的映射,发现都没有问题,甚至怀疑起了是不是因为我项目设置哪里有问题,导致了我Propertie对象的load方法执行过程跟正常的有点不一样。这时旭哥出现了:“你单词写错了”
。。。。。。。如听仙乐耳暂明
有点无语,这种错误跟把adidas写成abibas差不多一个水平了
所以以后再出现这种错误的时候,就要学会扩大一些检查范围。
越是觉得不可能犯错的地方,往往越容易犯错。
解决方案:
改名总结:
看起来都是些无关紧要的小错误,没有什么大的技术性的问题,我不知道我是不是应该高兴一下。
之后写程序要更加细心一点才行。