I have been having this problem for the whole day today, so even after i googled for the solution i was not able to get one, so i had to post to this forum. I had checked the solutions suggested on this forum but its not working for me still.
Ok I would start from the beginning:
Plastform:
---------------
MAC OS X 10.5.2
Netbeans IDE 6.0.1
Glassfish V2UR1
MySQL 5.0
Firefox
Code:
1> I have an enterprise application in the NetBeans and basically i am trying to create a web 2.0 based project in which the user when he/she inputs the username they are immediately informed if the userid is available or not.
JSP code:
---------------
Expand|Select|Wrap|Line Numbers
- <%--
- Document : UserSignOn
- Created on : Mar 13, 2008, 4:07:08 PM
- Author : ankittoshniwal
- --%>
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
- "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>User Sign On</title>
- <script language="javascript">
- var xmlHttp;
- function createXmlHttpRequest()
- {
- try
- {
- xmlHttp = new XMLHttpRequest();
- } catch (tryMicrosoft)
- {
- try {
- xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
- }
- catch (otherMicrosoft)
- {
- try
- {
- request = new ActiveXObject("Microsoft.XMLHTTP");
- }catch (failed)
- {
- xmlHttp = false;
- }
- }
- }
- if (!xmlHttp)
- alert("error initializing XMlHttpRequest");
- }
- function callServer()
- {
- createXmlHttpRequest();
- // var nameValue = document.getElementById("n");
- var userNameValue = document.getElementsByName("user").value;
- var url = "/TestProject-war/data?tempUserName=" + escape(userNameValue);
- xmlHttp.open("GET", url, true);
- xmlHttp.onreadystatechange = confirmUpdate;
- xmlHttp.send(null);
- }
- function confirmUpdate()
- {
- if (xmlHttp.readyState == 4)
- {
- if (xmlHttp.status == 200)
- {
- var message =
- xmlHttp.responseXML
- .getElementsByTagName("valid")[0]
- .childNodes[0].nodeValue;
- // Basically this is the point which is giving me errors
- document.getElementById("results").innerHTML = message;
- }
- else
- {
- alert("Error loading page" + xmlHttp.status );
- }
- }
- }
- </script>
- </head>
- <body>
- <p> We are trying to read</p>
- <input type="text" onkeyup="callServer();"/>
- <td width="31%" id=”results”> </td>
- </body>
- </html>
--------------------
So what i was planning to do was get the input from the JSP page and transfer it to the Servlet. In the servlet i was trying to connect to database and get the username approved, but i was getting the same errors, and hence i deleted the whole code and i just wrote a simple code in which whatever might be the input by the user in the JSP page the servlet would return "true" from the servlet meaning the username is true and available.
Code is as follows:
Expand|Select|Wrap|Line Numbers
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package org.web;
- import java.io.*;
- import java.net.*;
- import java.math.*;
- import java.util.List;
- import java.util.ListIterator;
- import javax.ejb.EJB;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import org.ejb.UserDetailsFacadeLocal;
- import org.ejb.UserDetails;
- /**
- *
- * @author ankittoshniwal
- */
- public class data extends HttpServlet {
- @EJB
- private UserDetailsFacadeLocal userDetailsFacade;
- String tempUsr;
- String tempUsername;
- String tempId;
- /**
- * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
- * @param request servlet request
- * @param response servlet response
- */
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter();
- try {
- // TODO output your page here
- out.println("<html>");
- out.println("<head>");
- out.println("<title>Servlet data</title>");
- out.println("</head>");
- out.println("<body>");
- out.println("<h1>Servlet data at " + request.getContextPath () + "</h1>");
- UserDetails UserDetailsObj = new UserDetails();
- out.println("</body>");
- out.println("</html>");
- } finally {
- out.close();
- }
- }
- // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
- /**
- * Handles the HTTP <code>GET</code> method.
- * @param request servlet request
- * @param response servlet response
- */
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- PrintWriter out = response.getWriter();
- UserDetails userObj = new UserDetails();
- // String checkName = request.getParameter("name");
- // String checkUserName = request.getParameter("user");
- // String temp;
- response.setContentType("application/xml");
- response.setHeader("Cache-Control", "no-cache");
- response.getWriter().write("<valid>true</valid>");
- }
- /**
- * Handles the HTTP <code>POST</code> method.
- * @param request servlet request
- * @param response servlet response
- */
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- PrintWriter out = response.getWriter();
- doGet(request, response);
- // processRequest(request, response);
- }
- /**
- * Returns a short description of the servlet.
- */
- public String getServletInfo() {
- return "Short description";
- }
- // </editor-fold>
- }
can somebody explain me where i am going wrong, i have been following the IBM's AJAX tutorial but its not helping out.
Any help would be greatly appreciated.
Thanks,
Ankit.