0

This is my jsp.. and I use Javascript-function and ajax-call to call 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>

Servlet code is as follows:

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");
        
    }

}

My web.xml look 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>

Nothing is happening on clicking on " Project " button. Please help me out in this.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mayur Sharma
  • 65
  • 2
  • 5
  • 17
  • Apparently the relative URL is plain wrong. Debug/alert `xmlhttp.status` to learn more. If it returns `404`, then the URL is definitely wrong. – BalusC Mar 20 '12 at 14:52

3 Answers3

0

You have to extend the HttpServlet in your class, like:

public class ReadProjectsInfo extend HttpServlet{
      ...
}

I think you could remove the line:

xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

It must work...

Vitor Braga
  • 2,173
  • 1
  • 23
  • 19
0

give the complete url of the servlet class as below and try once. It worked for me. xmlhttp.open("GET","http://localhost:8080/excelExchange/ReadProjectsInfo",true);

0

try commenting out the: xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); and the: if (xmlhttp.status == 200) { ... bits and see if it works

solvation
  • 21
  • 2
  • No. Still nothing is happening !! – Mayur Sharma Mar 20 '12 at 14:51
  • Have you tried setting a breakpoint in your servlet to see if it gets hit? – solvation Mar 20 '12 at 15:04
  • Thanks solvation for replying . @solvation : sorry , but what do you mean by setting a break point in servlet .. and howto do it .. Could you please explain it . – Mayur Sharma Mar 21 '12 at 04:51
  • What? You can't debug? How do you track code execution step by step while exploring the variables in the heap then? By `System.out.println()` lines printing ASCII art? Well, do that then. The point is to check if the servlet method is been called or not. – BalusC Mar 21 '12 at 04:58
  • On executing the jsp Exception that is comming is Allocate exception for servlet projectInfo java.lang.ClassCastException: excelExchange.ReadProjectsInfo cannot be cast to javax.servlet.Servlet at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1136) at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:857) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135) at org.apache.catalina.core.StandardContextValve.invoke (2-3 LINES ARE MORE THERE).. bUT I THINK ITS NOT GOING TO MY SERVLET !! – Mayur Sharma Mar 21 '12 at 05:23
  • One more thing .. If i uncomment alert(time); its not working !! Also if i replace xmlhttp.open("GET","ReadProjectsInfo",true); by xmlhttp.open("GET","displayForTesting.txt",true); everything is working fine !! – Mayur Sharma Mar 21 '12 at 05:30
  • I got my problem solved !! Silly mistake --- I forget to extend HttpServlet in servlet class ..!! thanks @solvation and BalusC . – Mayur Sharma Mar 21 '12 at 05:51