I have created a JSP file with Javascript functions and AJAX calls to invoke a servlet (ReadprojectInfo).
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
function displayProject()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","ReadProjectsInfo",true);
xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xmlhttp.onreadystatechange= function ()
{
if (xmlhttp.readyState==4)
{
if (xmlhttp.status == 200)
{
var time = xmlhttp.responseText;
//alert(time);
document.getElementById("center").innerHTML=xmlhttp.responseText;
}
}
}
xmlhttp.send();
//document.getElementById("center").innerHTML=Date();
}
</script>
<link rel="stylesheet" type="text/css" href="css/start.css" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Learning CSS</title>
</head>
<body>
<div id ="headerLink" class="HeaderLink" >
<button id="adminLink" class="AdminLink" href='DNE.jsp'>Home</button></div>
<button id="projectButton" class="ProjectButton" onclick="displayProject()" >Projects</button>
</div>
<div id="center" class ="Center"><p>Click Project</p></div>
</body>
</html>
The code for the servlet is shown below:
package excelExchange;
import java.io.IOException;
import java.io.PrintWriter;
import javaFiles.Dog;
import javaFiles.Person;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ReadProjectsInfo {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException{
//String var1= request.getParameter("var");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.write("Hello from servlet !!");
out.println("Hello");
}
}
This is what my web.xml configuration looks like:
<servlet>
<servlet-name>projectInfo</servlet-name>
<servlet-class>excelExchange.ReadProjectsInfo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>projectInfo</servlet-name>
<url-pattern>/ReadProjectsInfo</url-pattern>
</servlet-mapping>
Upon clicking the "Project" button, nothing seems to happen. I would appreciate any assistance in resolving this issue.