advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Partners & Affiliates
advertisement
advertisement
Tip of the Day
Rate this item | 0 users have rated this item.
Expertise: Intermediate
Language: Java
January 11, 2005
Update an HTML Form Using an XML ActiveX Object and a Servlet
The minimum requirement for this tip to work is to have Internet Explorer with MSXML objects installed when Windows is installed. The DOMDocument is also available in the new versions of Netscape. This example displays user information based on the login id without submitting the form.

Create a servlet that will submit an .xml document based on the request parameter "login" value:


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class UserInfoServlet
    extends HttpServlet {
  private static final String CONTENT_TYPE = "text/xml";

  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    doPost(request, response);
  }

  //Process the HTTP Post request
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    String login = request.getParameter("login");
    String info = null;
    if (login != null && "guest".equalsIgnoreCase(login)) {
      info = "<username>Guest</username>";
    }
    else {
      info = "<username>Unknown</username>";
    }
    response.setContentType(CONTENT_TYPE);
    PrintWriter out = response.getWriter();
    out.println("<?xml version=\"1.0\"?>");
    out.println("<output>");
    out.println(info);
    out.println("</output>");
  }
}
Add servlet mapping for this servlet in the web.xml file as follows:

<servlet>
  <servlet-name>userinfo</servlet-name>
  <servlet-class>UserInfoServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>userinfo</servlet-name>
  <url-pattern>/ui</url-pattern>
</servlet-mapping>
Following is the HTML page:

<html>
  <head><title>User Information</title></head>
  <body>
    <form name="userinfo">
      Enter Login Id:
      <input type="text" name="login" onblur="javascript:doLoadData(this.value)" />
      User Information: (Readonly)
      <input type="text" name="username" readonly="readonly" />
    </form>
  </body>

  <script language="JavaScript1.4" type="text/javascript">
  function doLoadData(login) {
    var dd = new ActiveXObject("MSXML.DOMDocument");
    dd.async = false;
    dd.load('/context/ui?login=' + login);
    var node = dd.documentElement.selectSingleNode("/output/username");
    document.forms['userinfo'].elements['username'].value = node.text;
  }
  </script>

</html>
Now, if you type "guest" in the login input, "Guest" or otherwise Unknown will be displayed automatically.
Farukh Ijaz
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible. Submit your tip here.
Please rate this item (5=best)
 1  2  3  4  5
advertisement
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs