Can anyone make this code work?

Reply

Join Date: Oct 2007
Posts: 23
Reputation: dave_nithis is an unknown quantity at this point 
Solved Threads: 0
dave_nithis dave_nithis is offline Offline
Newbie Poster

Can anyone make this code work?

 
0
  #1
Jul 25th, 2008
This is my HTML File:

  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function validateCatalogId(){
  5.  
  6. var req=init();
  7.  
  8. function init(){
  9.  
  10. if (window.XMLHttpRequest) {
  11. return new XMLHttpRequest();
  12. } else if (window.ActiveXObject) {
  13.  
  14. return new ActiveXObject("Microsoft.XMLHTTP");
  15. }
  16.  
  17. }
  18. var catalogId=document.getElementById("catalogId");
  19. req.open("POST", "validateForm", true);
  20. req.onreadystatechange=processRequest;
  21. req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  22. req.send("catalogId="+ encodeURIComponent(catalogId.value));
  23.  
  24. function processRequest(){
  25.  
  26. if(req.readyState==4){
  27. if(req.status==200){
  28.  
  29. processResponse();
  30.  
  31. }
  32. }
  33. }
  34.  
  35. function processResponse(){
  36.  
  37. var xmlMessage=req.responseXML;
  38.  
  39. }
  40. }
  41.  
  42. </script>
  43.  
  44. </head>
  45. <body>
  46. <h1>Form for Catalog Entry</h1>
  47. <form name="validationForm" action="validateForm" method="post">
  48.  
  49. <div id="catalogId" name="catalogId"> </div>
  50. <table>
  51. <tr><td>Journal:</td><td><input type="text"
  52. size="20"
  53. id="journal"
  54. name="journal"></td>
  55. </tr>
  56.  
  57. <tr><td>Publisher:</td><td><input type="text"
  58. size="20"
  59. id="publisher"
  60. name="publisher"></td>
  61. </tr>
  62. <tr><td><input type="button" onclick="validateCatalogId();"
  63. value="Create Catalog"
  64. id="submitForm"
  65. name="submitForm"></td>
  66. </tr>
  67. </table>
  68.  
  69. </form>
  70.  
  71. </body>
  72. </html>

And this is my servlet:

  1. import java.io.*;
  2. import java.sql.*;
  3. import javax.naming.InitialContext;
  4. import javax.servlet.*;
  5. import javax.servlet.http.*;
  6.  
  7. public class FormServlet extends HttpServlet {
  8.  
  9. private String catalogId =null;
  10. private String journal=null;
  11. private String publisher= null;
  12.  
  13. public void setCatalogId(String catalogId)
  14. {
  15. this.catalogId= catalogId;
  16. }
  17. public String getCatalogId()
  18. {
  19. return catalogId;
  20. }
  21.  
  22. public void setJournal(String journal)
  23. {
  24. this.journal= journal;
  25. }
  26.  
  27. public String getJournal()
  28. {
  29. return journal;
  30. }
  31.  
  32. public void setPublisher(String publisher)
  33. {
  34. this.publisher= publisher;
  35. }
  36. public String getPublisher()
  37. {
  38. return publisher;
  39. }
  40.  
  41.  
  42. public void doPost(HttpServletRequest request, HttpServletResponse response)
  43. throws ServletException, IOException {
  44.  
  45. // Obtain value of Catalog Id field to ve validated.
  46. String catalogId = request.getParameter("catalogId");
  47. String journal = request.getParameter("journal");
  48. String publisher = request.getParameter("publisher");
  49.  
  50. // set headers before accessing the Writer
  51. response.setContentType("text/xml");
  52. response.setHeader("Cache-Control", "no-cache");
  53.  
  54. PrintWriter out = response.getWriter();
  55.  
  56.  
  57. if(request.getParameter("catalogId") != null)
  58. {
  59. setCatalogId(request.getParameter("catalogId"));
  60. }
  61. else {
  62. setCatalogId(" ");
  63. }
  64.  
  65. if(request.getParameter("journal") != null)
  66. {
  67. setJournal(request.getParameter("journal"));
  68. }
  69. else {
  70. setJournal(" ");
  71. }
  72.  
  73. if(request.getParameter("publisher") != null) {
  74. setPublisher(request.getParameter("publisher"));
  75. }
  76. else {
  77. setPublisher(" ");
  78. }
  79.  
  80. // then write the response
  81. // If result set is empty set valid element to true
  82.  
  83. response.getWriter().write("<catalog>" + "<catalogId>+getCatalogId()+</catalogId>" + "<journal>+getJournal()+</journal>" + "<publisher>+getPublisher()+</publisher>" + "</catalog>");
  84. }
  85. }
Without getter and setter methods the code is working fine..Can anyone find and solve the issue?

Expecting your Reply
Dave.
Last edited by peter_budo; Jul 25th, 2008 at 10:06 am. Reason: Keep It Organized - please use [code] tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,213
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 489
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Can anyone make this code work?

 
-1
  #2
Jul 25th, 2008
Are you running this on Tomcat server?
What are you trying to achieve?
Are there any errors?
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 15
Reputation: adnanahsan is an unknown quantity at this point 
Solved Threads: 0
adnanahsan adnanahsan is offline Offline
Newbie Poster

Re: Can anyone make this code work?

 
0
  #3
Aug 11th, 2008
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..
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 23
Reputation: dave_nithis is an unknown quantity at this point 
Solved Threads: 0
dave_nithis dave_nithis is offline Offline
Newbie Poster

Re: Can anyone make this code work?

 
0
  #4
Aug 25th, 2008
hyyyyyyyyyyyyyyyyyyyyyy
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC