This is my HTML File:

<html>
<head>
<script type="text/javascript">
function validateCatalogId(){

var req=init();

  function init(){

if (window.XMLHttpRequest) {
           return new XMLHttpRequest();
       } else if (window.ActiveXObject) {
           
           return new ActiveXObject("Microsoft.XMLHTTP");
       }

}
var catalogId=document.getElementById("catalogId");
req.open("POST", "validateForm", true);
req.onreadystatechange=processRequest;
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.send("catalogId="+ encodeURIComponent(catalogId.value));

function processRequest(){

if(req.readyState==4){
   if(req.status==200){
    
      processResponse();

    }
  }
}

function processResponse(){

var xmlMessage=req.responseXML;

 }
}  

</script>

</head>
<body>
<h1>Form for Catalog Entry</h1>
<form name="validationForm" action="validateForm" method="post">

<div id="catalogId" name="catalogId"> </div>
<table>
<tr><td>Journal:</td><td><input    type="text"
            size="20"  
              id="journal"
            name="journal"></td>
</tr>

<tr><td>Publisher:</td><td><input    type="text"
            size="20"  
              id="publisher"
            name="publisher"></td>
</tr>
<tr><td><input    type="button" onclick="validateCatalogId();"
            value="Create Catalog"  
              id="submitForm"
            name="submitForm"></td>
</tr>
</table>

</form>

</body>
</html>

And this is my servlet:

import java.io.*;
import java.sql.*;
import javax.naming.InitialContext;
import javax.servlet.*;
import javax.servlet.http.*;

public class FormServlet extends HttpServlet {

			 private String catalogId =null;
			 private String journal=null;
			 private String publisher= null;

			 public void setCatalogId(String catalogId)
			{
				 this.catalogId= catalogId;
			}
			 public String getCatalogId()
			{
				return catalogId;
			}

			 public void setJournal(String journal)
			{
				 this.journal= journal;
			}

			 public String getJournal()
			{
				return journal;
			}

			 public void setPublisher(String publisher)
			{
				 this.publisher= publisher;
			}
		    public String getPublisher()
			{
				return publisher;
			}
			

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

			// Obtain value of Catalog Id field to ve validated.
			String catalogId = request.getParameter("catalogId");
			String journal = request.getParameter("journal");
			String publisher = request.getParameter("publisher");

			// set headers before accessing the Writer
			response.setContentType("text/xml");
			response.setHeader("Cache-Control", "no-cache");

			PrintWriter out = response.getWriter();


			if(request.getParameter("catalogId") != null) 
			{
				setCatalogId(request.getParameter("catalogId"));
			}
            else {
                     setCatalogId(" ");
            }

			if(request.getParameter("journal") != null) 
			{
				setJournal(request.getParameter("journal"));
			}
             else {
                     setJournal(" ");
             }
			
			if(request.getParameter("publisher") != null) {
				setPublisher(request.getParameter("publisher"));
			}
            else {
                   setPublisher(" ");
             }
			
			// then write the response
			// If result set is empty set valid element to true
		  
		   response.getWriter().write("<catalog>" + "<catalogId>+getCatalogId()+</catalogId>" + "<journal>+getJournal()+</journal>" + "<publisher>+getPublisher()+</publisher>" + "</catalog>");
		}
}

Without getter and setter methods the code is working fine..Can anyone find and solve the issue?

Expecting your Reply
Dave.

Recommended Answers

All 3 Replies

Are you running this on Tomcat server?
What are you trying to achieve?
Are there any errors?

i am not familiar with java servlets, but i doubt in following line:

req.send("catalogId="+ encodeURIComponent(catalogId.value));

i think it should be like this

req.send("catalogId="+ encodeURIComponent(catalogId.innerHTML));

please let me know if it works or not..:)

hyyyyyyyyyyyyyyyyyyyyyy

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.