I'm trying to do a simple web project in eclipse. My goal is to have a .jsp/html file be a presentation layer, javascript to handle logic, and Java to handle server side stuff. For my simple test, I just want to be able for my javascript code to contact the web server and have the java code return a date. Here is what I have right now (note I'm only displaying the "trouble" parts)
.jsp (Timer.jsp):
<div ><h2 id="date" class="main"></h2></div>
JavaScript (timer.js):
var xhr = new XMLHttpRequest();
document.getElementById("date").innerHTML = xhr.responseText;
xhr.open("GET", "CoopTimer", true);
xhr.send();
Java:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
request.setAttribute("date", dateFormat.format(date).toString());
request.getRequestDispatcher("/Timer.jsp").forward(request, response);
}
The JavaScript I am sure is wrong and is probably the trouble area. So essentially, my goal is to have the header id "date" get a date from the javascript, and the javascript will get the value from the java servlet.
If anyone can point me in the right direction, whether it's resources, what have you, that would be terrific. Thank you!
EDIT: Here is the final code I used. After hours of trying to figure out what was wrong...nothing mentioned in eclipse about right clicking the HTML and Run As on server. A humiliating mistake, but one I won't ever forget again.
Here is the code I used to return a simple date string:
.jsp (Timer.jsp):
<div ><h2 id="date" class="main"></h2></div>
Javascript (timer.js)
$("#date").load("http://127.0.0.1:14949/CoopTimer/CoopTimer");
Java:
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(date.toString());
//System.out.println("PING");
}
servlets. You should check out the documentation we have at SO: stackoverflow.com/tags/servlets/info