请问jsp和servlet如何调试?麻烦帮我看下这段代码
package com.imooc.listcouse;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/ListCouse")
public class ListCouse extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String flag=request.getParameter("flag");
String data="";
if("1".equals(flag)) {
data="哈哈";
}else if("2".equals(flag)){
data="嗯嗯";
}
response.getOutputStream().write(data.getBytes("utf-8"));
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<input type="button" value="哈哈" flag="1" onclick="showJava()"/>
<input type="button" value="哈哈" flag="2" onclick="showShow()"/>
<div style="height: 100px,width:100px,background-color:red" id="div1"></div>
</body>
<script type="text/javascript">
function showJava() {
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","<%=basePath%>/ListCouseServlet?flag=1",true);
xmlhttp.send;
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4&&xmlhttp.readyState==200){
document.getElementById("div1").innerHTML=xmlhttp.responseText;
}
}
}
function showShow() {
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","<%=basePath%>/ListCouseServlet?flag=2",true);
xmlhttp.send;
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4&&xmlhttp.readyState==200){
document.getElementById("div1").innerHTML=xmlhttp.responseText;
}
}
}
</script>
</html>0
收起
正在回答
3回答
不是把readyState改成readyStates,应该是把readyState 改为status

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
好帮手慕阿莹
2018-07-31 14:06:07
1、你的xmlhttp.send;后应该加():xmlhttp.send();
2、你的xmlhttp.open("GET","<%=basePath%>/ListCouseServlet?flag=2",true);应改为xmlhttp.open("GET","<%=basePath%>/ListCouse?flag=1",true); 因为你的ListCouseServlet的虚拟路径是ListCouse
3、请问jsp和servlet如何调试? 同学是问如何测试问题出在哪里么?
初学阶段,同学要记住一些语法,
1、可以在浏览器的控制台看看有没有报错信息。
2、可以在ListCouse中输出一句话,测试有没有调用都Servlet。
3、可以在js中添加个alert()看看有没有执行到这个函数:例如:

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星