<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>etechGuide . . . &#187; Web Technologies</title>
	<atom:link href="http://etechGuide.in/category/web/feed/" rel="self" type="application/rss+xml" />
	<link>http://etechGuide.in</link>
	<description>Know more , share more</description>
	<lastBuildDate>Fri, 25 Jun 2010 13:58:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Developing GWT-RPC application with a Database support</title>
		<link>http://etechGuide.in/web/gwt-rpc-with-database/</link>
		<comments>http://etechGuide.in/web/gwt-rpc-with-database/#comments</comments>
		<pubDate>Fri, 25 Jun 2010 13:49:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[GWT]]></category>
		<category><![CDATA[Web Technologies]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[rpc]]></category>

		<guid isPermaLink="false">http://etechGuide.in/?p=587</guid>
		<description><![CDATA[This post is a step-by-step guide to build an application using GWT-RPC . Just to make it more realistic , I have used a back-end database to store the records. I have used <a href="http://mysql.com/downloads/">MySql</a> as a database and <a href="http://tomcat.apache.org/download-60.cgi">Tomcat 6.0</a> as a deployment web server. If you are interested to have a look at the code and then the post , it can be downloaded from <a href="http://etechGuide.in/wp-content/uploads/2010/06/GwtRpcDB.zip">here</a>. Let me start with a small but specific description of GWT-RPC.]]></description>
			<content:encoded><![CDATA[<p>This post is a step-by-step guide to build an application using GWT-RPC . Just to make it more realistic , I have used a back-end database to store the records. I have used <a href="http://mysql.com/downloads/">MySql</a> as a database and <a href="http://tomcat.apache.org/download-60.cgi">Tomcat 6.0</a> as a deployment web server. If you are interested to have a look at the code and then the post , it can be downloaded from <a href="http://etechGuide.in/wp-content/uploads/2010/06/GwtRpcDB.zip">here</a>. Let me start with a small but specific description of GWT-RPC.</p>
<h4> <u>What is GWT-RPC ?</u> </h4>
<p>Google Web Tool-kit (<a href="http://code.google.com/webtoolkit/gettingstarted.html">GWT</a>) provides couple of different ways to communicate with the server using <a href="http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol">HTTP</a>. GWT-RPC is a framework to make a transparent server-side call where GWT takes care of all other low level details , like , object serialization etc.<br />
      When your application running in the browser , it needs to interact with the server using a Remote Procedure Call (RPC).GWT provides an RPC mechanism based on Java Servlets to provide access to server side. GWT uses <a href="http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsDeferred.html">deferred binding</a> to serialize objects across the network. You can read more on GWT-RPC  from <a href="http://code.google.com/webtoolkit/doc/latest/DevGuideServerCommunication.html#DevGuideRemoteProcedureCalls">here</a>.</p>
<h4> <u>The application : User Creation Application</u> </h4>
<p>The funny thing is , My application creates User alone. Yes , it doesn&#8217;t do anything else other than creating user and storing them to the database. But , the important thing is , it uses GWT-RPC and gives enough scope of learning to build an application using GWT-RPC or GWT. My create user screen look like ,</p>
<div id="create_user" class="wp-caption alignleft" style="width: 610px"><br />
<img class="size-full wp-image-38" title="Create User Screen" src="http://etechGuide.in/wp-content/uploads/2010/06/create_user.png" alt="Create User Screen" width="350" height="275" /><br />
<p class="wp-caption-text">Create User Screen</p></div>
<p><Strong> <u>Setup details :</u> </Strong></p>
<ul>
<li>Launch your favorite Java IDE. I have used eclipse which can be downloaded from <a href="http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/galileor">here</a>. If you are not so comfortable with eclipse , you can learn it from <a href="http://etechguide.in/eclipse/eclipse_shortcuts/">here</a>.</li>
<li>Configure the GWT plug-in for eclipse. Learn how , <a href="http://code.google.com/eclipse/docs/download.html">here</a>. </li>
<li>Download the project from <a href="http://etechGuide.in/wp-content/uploads/2010/06/GwtRpcDB.zip">here</a> and extract into a folder of your choice.</li>
<li><a href="http://dev.mysql.com/downloads/connector/j/5.0.html">Download</a> the MySQL Connector/J and copy the jar to the /war/WEB-INF/lib directory.</li>
<li>Import the extracted project to your eclipse. </li>
</ul>
<p>Once the project is imported to eclipse , the project structure should be like following :<br />
<div id="directory_structure" class="wp-caption alignleft" style="width: 610px"><br />
<img class="size-full wp-image-38" title="Project Directory Structure" src="http://etechGuide.in/wp-content/uploads/2010/06/project_hier.png" alt="Project Directory Structure" width="330" height="820" /><br />
<p class="wp-caption-text">Project Directory Structure</p></div></p>
<p>Now we are all set to go.  GWT-RPC uses servlets to communicate with the server-side. GWT provides a servlet called , RemoteServiceServlet. This is a base servlet class for your RPC service implementations. It helps you to automatically deserializes incoming requests from the client and serializes outgoing responses for client/server RPCs. </p>
<p>You should do the following to set your GWT-RPC interface ,</p>
<ul>
<li>Create a service interface &#8211; Create an interface for your service that extends RemoteService and lists all RPC methods. </li>
<li>Create a service implementation &#8211; Create a class to implement the server-side logic. The class should extends RemoteServiceServlet and implements the interface you created in the above step.</li>
<li>Create an asynchronous interface to your service to be called from the client-side code.</li>
</ul>
<p><Strong><u>Create a service interface : </u></Strong><br />
Our <i>Create User</i> program requires at least two service methods . One is to check if the user is already exist in the database and other one is to Save the User into the databse. </p>
<pre class="brush: java">
import com.etechguide.project.gwtrpcdb.client.exception.UserException;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;

/**
 * The client side stub for the RPC service.
 */
@RemoteServiceRelativePath(&quot;user&quot;)
public interface UserService extends RemoteService {
	 String createUser(User user) throws UserException;
	 boolean checkUserExist(String userName) throws UserException;

}
</pre>
<p>I have defined both the methods in my service interface. The service interface is annotated with the annotation , @RemoteServiceRelativePath(&#8221;user&#8221;). This annotation associates a RemoteService with the relative path(/user). we will use this relative path(/user) in the web.xml file while defining the servlet.</p>
<p><Strong><u>Create a service implementation : </u></Strong><br />
This class should extends RemoteServiceServlet and implements the UserService interface . As this implementation class extends RemoteServiceServlet , it is a servlet class by itself.</p>
<pre class="brush: java">
/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings(&quot;serial&quot;)
public class UserServiceImpl extends RemoteServiceServlet implements
		UserService {

	UserDao userDao = new UserDao();
	@Override
	public String createUser(User user) throws UserException {
		String retVal = &quot;true&quot;;
		if (!Validator.isValidName(user.getUserName())) {
			throw new UserException(
					&quot;User name must be at least 5 characters long&quot;);
		}
		try {
			userDao.createUser(user);
		} catch (ClassNotFoundException e) {
			retVal = &quot;false&quot;;
			throw new UserException(e.getLocalizedMessage());
		} catch (SQLException e) {
			retVal = &quot;false&quot;;
			throw new UserException(e.getLocalizedMessage());
		}

		return retVal;
	}

	@Override
	public boolean checkUserExist(String userName) throws UserException {
		return userDao.checkUserExist(userName);
	}
}
</pre>
<p>The implementation class , UserServiceImpl uses the Data Access Object (DAO) to call the data base related methods. The DAO (UserDao.java) would look like ,</p>
<pre class="brush: java">
public class UserDao {

	Connection connection = null;
	Statement stmt = null;

	public void createUser(User user) throws ClassNotFoundException,
                     SQLException{
		String query = null;
		StringBuilder queryBuilder = null;
		queryBuilder = new StringBuilder();
		queryBuilder.append(&quot;INSERT INTO user(userName,password,&quot;);
		queryBuilder.append(&quot;emailId,address)&quot;);
		queryBuilder.append(&quot; VALUES( &quot;);
		queryBuilder.append(&quot;&#039;&quot;+user.getUserName()+&quot;&#039;&quot;);
		queryBuilder.append(&quot;,&quot;);
		queryBuilder.append(&quot;&#039;&quot;+user.getPassword()+&quot;&#039;&quot;);
		queryBuilder.append(&quot;,&quot;);
		queryBuilder.append(&quot;&#039;&quot;+user.getEmailId()+&quot;&#039;&quot;);
		queryBuilder.append(&quot;,&quot;);
		queryBuilder.append(&quot;&#039;&quot;+user.getAddress()+&quot;&#039;&quot;);
		queryBuilder.append(&quot; )&quot;);

		query = queryBuilder.toString();
		System.out.println(&quot;query from createUser = &quot; + query);
		System.out.println(&quot;user to be created ... &quot; + user.toString());
		connection = DbConnection.createConnection();
		stmt = connection.createStatement();
		stmt.executeUpdate(query);
		DbConnection.closeConnection(connection, stmt);
	}

	public boolean checkUserExist(String userName) throws UserException {
		boolean isExist = false;
		String query = null;
		StringBuilder queryBuilder = null;
		ResultSet rs = null;
	    int rowCount = -1;
		queryBuilder = new StringBuilder();
		queryBuilder.append(&quot;SELECT count(*) FROM user WHERE userName= &#039;&quot;);
		queryBuilder.append(userName);
		queryBuilder.append(&quot;&#039;&quot;);
		query = queryBuilder.toString();
		System.out.println(&quot;query from checkUserExist = &quot; + query);
		try {
			connection = DbConnection.createConnection();
			stmt = connection.createStatement();
			rs = stmt.executeQuery(query);
			rs.next();
			rowCount = rs.getInt(1);
			if(rowCount &gt; 0){
				isExist = true;
			}
		} catch (ClassNotFoundException e) {
			throw new UserException(e.getLocalizedMessage());
		} catch (SQLException e) {
			throw new UserException(e.getLocalizedMessage());
		}finally{
			try {
				DbConnection.closeConnection(connection, stmt,rs);
			} catch (SQLException e) {
				throw new UserException(e.getLocalizedMessage());
			}
		}
		return isExist;
	}

}
</pre>
<p>So , as of now the story is ,  you have  User Service and the Implementation of that. The Implementation is nothing but a servlet. We call all the database related methods from the implementation class. As it is a servlet and we are building a web application , we must declare this servlet in the web.xml file which is the main deployment descriptor file for any web application. The web.xml file is like ,</p>
<pre class="brush: xml">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;!DOCTYPE web-app
    PUBLIC &quot;-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN&quot;
    &quot;http://java.sun.com/dtd/web-app_2_3.dtd&quot;&gt;

&lt;web-app&gt;

  &lt;!-- Servlets --&gt;
  &lt;servlet&gt;
    &lt;servlet-name&gt;userServlet&lt;/servlet-name&gt;
    &lt;servlet-class&gt;
    	com.etechguide.project.gwtrpcdb.server.UserServiceImpl
    &lt;/servlet-class&gt;
  &lt;/servlet&gt;

  &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;userServlet&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/gwtrpcdb/user&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;

  &lt;!-- Default page to serve --&gt;
  &lt;welcome-file-list&gt;
    &lt;welcome-file&gt;GwtRpcDB.html&lt;/welcome-file&gt;
  &lt;/welcome-file-list&gt;

&lt;/web-app&gt;
</pre>
<p>Look at the url-pattern , it&#8217;s the MODULE_NAME/user which we have already specified with RemoteServiceRelativePath in the User Service. Now it is the time to write the asynchronous interface so that the client can use the services created by us.</p>
<p><Strong><u>Create an asynchronous interface :</u></Strong></p>
<pre class="brush: java">
import com.google.gwt.user.client.rpc.AsyncCallback;

public interface UserServiceAsync {
	void createUser(User user, AsyncCallback&lt;String&gt; callback)
			throws IllegalArgumentException;

	void checkUserExist(String userName, AsyncCallback&lt;Boolean&gt; callback);
}
</pre>
<p>You should follow certain rules while creating the asynchronous interface.</p>
<ul>
<li>The interface name should be the actual service name suffix with the word &#8216;Async&#8217;</li>
<li>The interface should have the similar methods that the actual service has with some exceptions.<br />
              1. The method should not have any return type other than void.<br />
              2. The methods should have an extra parameter at the end( AsyncCallback&lt;String&gt; callback). Here the &lt;String&gt; is the return type of the method in the actual service(User Service).</li>
<li>The methods in the async interface are allowed to throw Run-time Exceptions only. </li>
</ul>
<p>Lets call out services from a client side code. Have a look at the following code.</p>
<pre class="brush: java">
/**
   * Create a remote service proxy to talk to the server-side User service.
   */
	private final UserServiceAsync userService = GWT
			.create(UserService.class);

userService.createUser(user,
						new AsyncCallback&lt;String&gt;() {
							@Override
							public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
								Window.alert(caught.getLocalizedMessage());

							}
							@Override
							public void onSuccess(String result) {

								if(result.equals(&quot;true&quot;)){
									Window.alert(&quot;User Created Successfully&quot;);
								}else{
									Window.alert(&quot;User Creation failed&quot;);
								}
								clearAll();

							}

						});

nameField.addBlurHandler(new BlurHandler() {
			@Override
			public void onBlur(BlurEvent event) {
				final String userName = nameField.getText();
				if(userName != null
                               &amp;&amp; userName.length() &gt;0){
					userService.checkUserExist(userName,
                                                new AsyncCallback&lt;Boolean&gt;() {
						@Override
						public void onFailure(Throwable caught) {
							Window.alert(caught.getLocalizedMessage());

						}
						@Override
						public void onSuccess(Boolean result) {
							if(result.booleanValue()){
								errorLabel.setText(&quot;&quot;);
								errorLabel.setText(userName+&quot; taken&quot;);
								errorLabel.setStyleName(&quot;errorLabel&quot;);
							}else{
								errorLabel.setText(&quot;&quot;);
								errorLabel.setText(userName+&quot; available&quot;);
								errorLabel.setStyleName(&quot;successLabel&quot;);
							}
						}
					});
				}
			}
		});
</pre>
<p>First we create a remote service proxy to talk to the server side user service. Once the proxy is created we give a call to the service methods. We create an anonymous callback class instance and pass to the service methods. AsyncCallback interface has two methods , onSuccess() and inFailure(). Once a request is success and completed , the onSuccess() method gets called. If there is a failure , onFailure() method gets called. One thing to note here , onSuccess() method&#8217;s parameter&#8217;s type should be same of the service&#8217;s called method&#8217;s parameter type.</p>
<p>Thats all !! <a href="http://etechGuide.in/wp-content/uploads/2010/06/GwtRpcDB.zip">Download</a> the project and start exploring more. Please feel free to write comments if you like it or would like to see any improvement on this post.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?&amp;linkurl=http%3A%2F%2FetechGuide.in%2Fweb%2Fgwt-rpc-with-database%2F&amp;linkname=Developing%20GWT-RPC%20application%20with%20a%20Database%20support"><img src="http://etechGuide.in/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Save/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://etechGuide.in/web/gwt-rpc-with-database/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>GWT &#8211; How to create a HTML Label</title>
		<link>http://etechGuide.in/web/gwt/gwt-how-to-create-html-label/</link>
		<comments>http://etechGuide.in/web/gwt/gwt-how-to-create-html-label/#comments</comments>
		<pubDate>Mon, 14 Jun 2010 09:35:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[GWT]]></category>
		<category><![CDATA[html label]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[label]]></category>

		<guid isPermaLink="false">http://etechGuide.in/?p=573</guid>
		<description><![CDATA[GWT provides a text-type label by default. You can set a text to a label but it is not possible to set a HTML to a Gwt label unless you do few changes. 

Label myLabel = new Label();
myLabel.setText(&#34;This is my label&#34;);
myLabel.setHtml(&#34;This&#60;br&#62;is&#60;br&#62;my&#60;br&#62;label&#34;); // compilation error

To achieve this , you can create your own customized label which [...]]]></description>
			<content:encoded><![CDATA[<p>GWT provides a text-type label by default. You can set a text to a label but it is not possible to set a HTML to a Gwt label unless you do few changes. </p>
<pre class="brush: java">
Label myLabel = new Label();
myLabel.setText(&quot;This is my label&quot;);
myLabel.setHtml(&quot;This&lt;br&gt;is&lt;br&gt;my&lt;br&gt;label&quot;); // compilation error
</pre>
<p>To achieve this , you can create your own customized label which extends com.google.gwt.user.client.ui.Widget. Have a look at the following code.</p>
<pre class="brush: java">
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Widget;

public class HTMLLabel extends Widget {

	public HTMLLabel(){
		this(null);
	}

	public HTMLLabel(String text)
	{
		setElement(DOM.createLabel());
		if(text != null)
			DOM.setInnerText(getElement(), text);
	}

	public String getText()
	{
		return DOM.getInnerText(getElement());
	}

	public void setHTML(String html){
		DOM.setInnerHTML(getElement(), html);
	}

	public String getHTML(){
		return DOM.getInnerHTML(getElement());
	}
}
</pre>
<p>We have created a class (HtmlLabel) extending the Widget class provided by GWT. At the line 12 ,we created a label by using the DOM class inside the parametrized constructor . Now we should expose a method so that consumer of this class can set a HTML to the created Label element. At the line 22 , we exposed a method where we set the inner HTML of the created label element. </p>
<p>A consumer of the HTMLLabel can create the HTML label in the following way,</p>
<pre class="brush: java">
HTMLLabel htmlLabel = new HTMLLabel();
htmlLabel.setHTML(&quot;This&lt;br&gt;is&lt;br&gt;my&lt;br&gt;label&quot;);
</pre>
<p>The output is a HTML label. As we have extended the Widget class , you can easily associate any event handlers to the HTML label that supported with normal GWT label. You can also create a SPAN element in GWT by following the above methodology of extending Widget class. I will discuss the SPAN element creation in my future post.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?&amp;linkurl=http%3A%2F%2FetechGuide.in%2Fweb%2Fgwt%2Fgwt-how-to-create-html-label%2F&amp;linkname=GWT%20%26%238211%3B%20How%20to%20create%20a%20HTML%20Label"><img src="http://etechGuide.in/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Save/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://etechGuide.in/web/gwt/gwt-how-to-create-html-label/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Protecting web application from Path Traversal Attack</title>
		<link>http://etechGuide.in/web/protecting_path_traversal_attack/</link>
		<comments>http://etechGuide.in/web/protecting_path_traversal_attack/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 06:27:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web Technologies]]></category>

		<guid isPermaLink="false">http://etechGuide.in/?p=534</guid>
		<description><![CDATA[<p>A Path Traversal attack targets the files and directories which are saved outside the root folder. An attacker aims for a files path stored in the web server. An attacker use the combination of dot-dot-slash(../) to browse to arbitrary directories and files. The dot-dot-slash sequence helps an attacker to move up in any directory location. The attacker not only can access any arbitrary files/directories from your web server , it is possible to upload some malicious files to some location in your web server , if the proper care is not taken.</p>]]></description>
			<content:encoded><![CDATA[<p><strong>Path Traversal Attack , What it is ?</strong></p>
<p>A Path Traversal attack targets the files and directories which are saved outside the root folder. An attacker aims for a files path stored in the web server. An attacker use the combination of dot-dot-slash(../) to browse to arbitrary directories and files. The dot-dot-slash sequence helps an attacker to move up in any directory location. The attacker not only can access any arbitrary files/directories from your web server , it is possible to upload some malicious files to some location in your web server , if the proper care is not taken.</p>
<p>The attack can be made in several different ways. An attacker might not use the dot-dot-slash patter directly in the url , rather can use the Encoding and double encoding value of dot(.) and slash(/).<br />
..%2f represents ../<br />
..%255c represents ..\<br />
%2e%2e\ represents ..\ and so on.<br />
Hence an attacker can use the following type of urls to play with your web server files and directories,<br />
http://web_site.com/toGetFiles?file=../../../../dir/file<br />
http://web_site.com/toGetFiles?file=&#8230;%255cdir/file<br />
depending on your operating systems.</p>
<p><strong>How to protect the web application from this attack ?</strong></P></p>
<p>Anything from an web application can be accessed using a request to the application. Hence we need a strict sanity check for all the possible dot-dot-slash patterns on each request coming to the web application.<br />
The check should be performed well before a request reaches in a state to have response for it. It means , a web application pre-filter should be written to achieve this.</p>
<p>You need to do modification in the web.xml of your web application. Add the following tag in your web application :</p>
<h4><span style="text-decoration: underline;">Listing 1 : Add filter tag to web.xml file</span></h4>
<pre class="brush: xml">
&lt;filter&gt;
    &lt;filter-name&gt;PTA&lt;/filter-name&gt;
        &lt;display-name&gt;PTA&lt;/display-name&gt;
        &lt;description&gt;This is a Pre-filter for PTA Prevention&lt;/description&gt;
    &lt;filter-class&gt;com.etechGuide.fw.filter.PathTraversalFilter&lt;/filter-class&gt;
&lt;/filter&gt;
&lt;filter-mapping&gt;
         &lt;filter-name&gt; PTA&lt;/filter-name&gt;
         &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;
</pre>
<p>As we are looking for filtering all the request and we would like to pass all the request through the filter. Hence we will be writing a Request Wrapper to code our filter logic. Look at the following filter definition.</p>
<h4><span style="text-decoration: underline;">Listing 2 : The PathTraversalFilter code</span></h4>
<pre class="brush: java">
public class PathTraversalFilter implements Filter {
	private FilterConfig filterConfig;
	public void init(FilterConfig filterConfig)
                              throws ServletException {
		this.filterConfig = filterConfig;
	}

	public void destroy() {
		this.filterConfig = null;
	}

	public void doFilter(ServletRequest request,
                 ServletResponse response,FilterChain chain)
                           throws IOException, ServletException {

		chain.doFilter(new RequestWrapper((HttpServletRequest) request),
				response);

	}
}
</pre>
<p> At the line number 16 we wrap the request to filter out all the malicious attacks for Path Traversals. have a look at the RequestWrapper.java file where the actual filtering bypass takes place.</p>
<h4><span style="text-decoration: underline;">Listing 3 : The RequestWrapper code</span></h4>
<pre class="brush: java">
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public final class RequestWrapper extends HttpServletRequestWrapper {

	public RequestWrapper(HttpServletRequest servletRequest) {
		super(servletRequest);
	}

	public String[] getParameterValues(String parameter) {

	  String[] values = super.getParameterValues(parameter);
	  if (values==null)  {
                  return null;
          }
	  int count = values.length;
	  String[] encodedValues = new String[count];
	  for (int i = 0; i &lt; count; i++) {
                 encodedValues[i] = cleanPTA(values[i]);
	   }
	  return encodedValues;
	}

	public String getParameter(String parameter) {
		  String value = super.getParameter(parameter);
		  if (value == null) {
		         return null;
                  }
		  return cleanPTA(value);
	}

	public String getHeader(String name) {
	    String value = super.getHeader(name);
	    if (value == null)
	        return null;
	    return cleanPTA(value);

	}

	private String cleanPTA(String value) {
		// Bypassing all possible dot-dot-slash
                value = value.replaceAll(&quot;%2e&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%2f&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%5c&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%25u002e&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%25u002f&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%25u005c&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%c0&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%ae&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%af&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%c1&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%9c&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%e0&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%80&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%f0&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%81&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%f8&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%fc&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%25uff3C&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%25u002E&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%5C&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%2F&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%25uff0E&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%25uff0F&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%25ufe68&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%00&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%2A&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;%25&quot;, &quot;&quot;);
		value = value.replaceAll(&quot;\\|&quot;, &quot;&quot;); 

                /* File.separator is different from one OS to another.
                    Hence the additional check is done below
                */
		String sFileName = value
				.substring(value.lastIndexOf(File.separator) + 1);

		if (sFileName.indexOf(&quot;..&quot;) &gt;= 0) {
			sFileName = value.substring(value.lastIndexOf(&quot;..&quot;) + 2);
		}
		if (sFileName.indexOf(&quot;\\&quot;) &gt;= 0) {
			sFileName = value.substring(value.lastIndexOf(&quot;\\&quot;) + 1);
		} if (sFileName.indexOf(&quot;/&quot;) &gt;= 0) {
			sFileName = value.substring(value.lastIndexOf(&quot;/&quot;) + 1);
		}
		return sFileName;
	}
}
</pre>
<p> cleanPTA(String value) method of above class does all the bypassing. This method is open for modification to the reader of this post. The logic can be changed as per the need but the basic by passing is all about what is done above.</p>
<p> Once a request reaches your web application , it has to go through the filter. The filter uses a wrapper on each request and filter out all the malicious attacks.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?&amp;linkurl=http%3A%2F%2FetechGuide.in%2Fweb%2Fprotecting_path_traversal_attack%2F&amp;linkname=Protecting%20web%20application%20from%20Path%20Traversal%20Attack"><img src="http://etechGuide.in/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Save/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://etechGuide.in/web/protecting_path_traversal_attack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Know more about Servlet Mapping</title>
		<link>http://etechGuide.in/web/servlets/how_servlet_mapping_works/</link>
		<comments>http://etechGuide.in/web/servlets/how_servlet_mapping_works/#comments</comments>
		<pubDate>Fri, 11 Sep 2009 08:55:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Servlets]]></category>
		<category><![CDATA[servlet]]></category>
		<category><![CDATA[Web Technologies]]></category>

		<guid isPermaLink="false">http://etechGuide.in/?p=418</guid>
		<description><![CDATA[A web server takes a request from the client and send the response back to the client. A servlet is a web application component that can talk to web server through the container it&#8217;s deployed into. We know , we need to do servlet mapping into web.xml to define the url pattern to access the [...]]]></description>
			<content:encoded><![CDATA[<p>A web server takes a request from the client and send the response back to the client. A servlet is a web application component that can talk to web server through the container it&#8217;s deployed into. We know , we need to do servlet mapping into web.xml to define the url pattern to access the servlet. The servlet is the place where a service is processed. So it&#8217;s mandatory to define the URL pattern correctly into web.xml to access the servlet correctly.<br />
We define a simple servlet mapping in the following way into web.xml file ,</p>
<h4><strong><span style="text-decoration: underline;">Listing 1 : Servlet Mapping</span> </strong></h4>
<pre class="brush: xml">
&lt;web-app xmlns=”http://java.sun.com/xml/ns/j2ee”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd”
version=”2.4”&gt;
&lt;servlet&gt;
&lt;servlet-name&gt;CustomerServlet&lt;/servlet-name&gt;
&lt;servlet-class&gt;com.etechGuide.CustomerDetailsServlet&lt;/servlet-class&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
&lt;servlet-name&gt;CustomerServlet&lt;/servlet-name&gt;
&lt;url-pattern&gt;/getCustomer&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
&lt;/web-app&gt;
</pre>
<p>The URL to access the CustomerServlet should be something like this,<br />
http://YOUR_SERVER_IP:SERVER_PORT/CONTEXT_ROOT/getCustomer<br />
(example : http://localhost:8080/customerdeal/getCustomer) .<br />
Have a look at the above URL to access the servlet. We call the servlet by it&#8217;s url-pattern defined into the web.xml , not by it&#8217;s servlet-name. So what is the use of the  tag? This is for use in other part of the web.xml. A client request a servlet.Requesting a servlet to get the servlet instantiated is done in the following way,</p>
<p><strong>1.</strong> Request come to the web server in the form of a URL(http://localhost:8080/customerdeal/getCustomer)<br />
<strong>2.</strong> Container look at the URL and find the possible <strong>url-pattern</strong> (in this case it is , getCustomer) in the URL.<br />
<strong>3.</strong> Container look for the <strong>url-pattern</strong> inside any <strong>servlet-mapping</strong> tag into the web.xml file.<br />
<strong>4.</strong> Once the <strong>url-pattern</strong> found , container locates the <strong>servlet-name</strong> tag , inside <strong>servlet-mapping</strong> tag(servlet name is  CustomerServlet in the Listing 1).<br />
<strong>5.</strong> Once the <strong>servlet-name</strong> is located , container search for the same <strong>servlet-name</strong> under a <strong>servlet</strong> tag.<br />
<strong>6.</strong> If found , container locates the <strong>servlet-class</strong> tag element associated with the <strong>servlet-name</strong> tag it found in the above step.<br />
<strong>7.</strong> If the servlet class (it is com.etechGuide.CustomerDetailsServlet in the Listing 1) is found , container instantiate the servlet.</p>
<p>So , now we know the importance of the servlet-name in servlet mapping. Servlet name is something client should not bother about but a web developer should map it carefully.<br />
Lets have a look at the <strong>url-mapping</strong> tag closely. In the above example, url-pattern is getCustomer. Question is , is getCustomer is a resource in the server ? What does the client to do with url-pattern ? Does he really need to bother about it ? No. It is not a resource and client does not have to bother about the meaning of the url-pattern. The URL should have url-pattern in any of the following forms ,<br />
<strong><span style="text-decoration: underline;">EXACT Match URL Pattern :</span> </strong></p>
<pre class="brush: xml">&lt;url-pattern&gt;/Customer/getCustomer.do&lt;/url-pattern&gt;</pre>
<p>This type of pattern must begin with a slash(/) and might have or not have an extension(.do).</p>
<p><strong><span style="text-decoration: underline;">EXTENSION Match URL Pattern :</span> </strong></p>
<pre class="brush: xml">&lt;url-pattern&gt;*.do&lt;/url-pattern&gt;</pre>
<p>This type of pattern must not begin with a slash(/) but must begin with a asterik(*). There must be an extension(do , com etc) specified after the period(.).</p>
<p><strong><span style="text-decoration: underline;">DIRECTORY Match URL Pattern :</span> </strong></p>
<pre class="brush: xml">&lt;url-pattern&gt;/Customer/*&lt;/url-pattern&gt;</pre>
<p>This kind of pattern must start with a slash(/) followed by a real or virtual directory name. It must end with a slash(/) and an asterik(*).</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?&amp;linkurl=http%3A%2F%2FetechGuide.in%2Fweb%2Fservlets%2Fhow_servlet_mapping_works%2F&amp;linkname=Know%20more%20about%20Servlet%20Mapping"><img src="http://etechGuide.in/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Save/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://etechGuide.in/web/servlets/how_servlet_mapping_works/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Refresh GUI on XML update without restarting Tomcat(Server)</title>
		<link>http://etechGuide.in/web/servlets/refresh_ui_tomcat_restart_xml_update/</link>
		<comments>http://etechGuide.in/web/servlets/refresh_ui_tomcat_restart_xml_update/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 12:44:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Servlets]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Tomcat]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://etechGuide.in/?p=354</guid>
		<description><![CDATA[I am writing this post on the basis of my learning on refreshing UI(User interface) without restarting server(Tomcat , in my case) on update of an XML which resides inside the container. Often we follow the design where we write our application configuration into an XML file and read - write the XML based on the requirements.]]></description>
			<content:encoded><![CDATA[<p>I am writing this post on the basis of my learning on refreshing UI(User interface) without restarting server(Tomcat , in my case) on update of an XML which resides inside the container. Often we follow the design where we write our application configuration into an XML file and read &#8211; write the XML based on the requirements.</p>
<p>lets take an example of an application which supports user management and keeps track of the users accessed by Admin in a History.Requirement is to persist the history information in such a manner so that it should be accessible well after application restart or server restart.So XML could be one of the option where you can write the history info and read it back whenever required.If the XML is to be placed in your war file , it has to be under web/ or web/<some directory or sub-directory> where web is your web content directory. The problem with modifying any of the resources under web content directory is , you need to restart your server(tomcat) to see the effect.</p>
<p>Just think about a situation where an admin had accessed 100 users and your code to write to XML executed perfectly to write to the XML file under web content directory.If admin wish to see the history browsing to the history page , he would not be able to see the latest history without restarting the server and the application.You would have written to the XML file successfully but to have the effect of getting the updated(recently written) value , server must be restarted. This is time consuming and none of web application can wait for it.</p>
<p>Solution of this problem is to write the history details in a Collection(it can be a list , map , array) or in a string. populate your GUI(History page) from the collection (of history) or the String. Write the collection to the XML file on log-out from the application.</p>
<p> Lets discuss in a step by step manner.<br />
<strong>Step 1 :</strong> Put(or insert) all the History object in a collection<br />
<strong>Step 2 :</strong> For each action listener , if the history is modified ( added or deleted) , modify the History collection.<br />
<strong>Step 3 :</strong> Make your GUI to read the details(History details) from the collection , not from the history directly.<br />
<strong>Step 4 :</strong> Write to the XML file , on log-out from the application<br />
<strong>Step 5 :</strong> Have mechanism to read the XML and populate the Collection on log-in to the application.</p>
<p>Lets have a look at the following picture.<br />
<div id="refreshUI" class="wp-caption alignleft" style="width: 610px"><br />
<img class="size-full wp-image-38" title="Flow Diagram" src="http://etechGuide.in/wp-content/uploads/2009/07/refresh_ui_tomcat_restart_xml_update/Read_Write_XML_web_app.jpg" alt="Flow Diagram" title="Object Serialization" width="600" height="500" /><br />
<p class="wp-caption-text">Flow Diagram</p></div><br />
The each step can be described as,<br />
1 . The application GUI uses a program logic on log-in to the application.<br />
2 . The logic reads the XML file<br />
3 . Populate the collection for future use of populating the GUI.<br />
4.  Read the History from the collection not from the UI directly.<br />
5.  If there is any event triggered from the GUI (say , add to history) , do not add to the XML directly.<br />
6. Add to the Collection using the logic.<br />
7. Add to the XML file while log-out.</p>
<p>This way , you can have your GUI rendering the recent data and can sync the XML file on log-out. There must be different ways to deal with this. Reader of this post is expected to share if any other ways are known.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?&amp;linkurl=http%3A%2F%2FetechGuide.in%2Fweb%2Fservlets%2Frefresh_ui_tomcat_restart_xml_update%2F&amp;linkname=Refresh%20GUI%20on%20XML%20update%20without%20restarting%20Tomcat%28Server%29"><img src="http://etechGuide.in/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Save/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://etechGuide.in/web/servlets/refresh_ui_tomcat_restart_xml_update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Refresh Parent page using JavaScript</title>
		<link>http://etechGuide.in/web/java-script/refresh-parent-page-using-javascript/</link>
		<comments>http://etechGuide.in/web/java-script/refresh-parent-page-using-javascript/#comments</comments>
		<pubDate>Thu, 25 Jun 2009 09:40:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java Script]]></category>

		<guid isPermaLink="false">http://etechGuide.in/?p=317</guid>
		<description><![CDATA[Window.open() method opens a new window . If we try to refresh the parent on change of something in the opened window , what are the ways to do it ? One possible way is to call reload(true) on the opener. It reloads the whole document for you.]]></description>
			<content:encoded><![CDATA[<p>Many times we use javaScript Window.open() method to open a new window. Often we do some kind of form processing in the new window opened by Window.open(). If the requirement is to refresh the parent page based on some action happen in the new window use the javaScript reload method to do that. The method would reload the full document for you.<br />
The syntax to refresh the parent window is ,</p>
<pre class="brush: jscript">window.opener.location.reload(true);</pre>
<p>This small code is very useful when you try to reflect your changes in parent window on change of anything in your child(opened) window.</p>
<p>I would like to request you all to share other mechanisms to refresh the window using javaScript.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?&amp;linkurl=http%3A%2F%2FetechGuide.in%2Fweb%2Fjava-script%2Frefresh-parent-page-using-javascript%2F&amp;linkname=Refresh%20Parent%20page%20using%20JavaScript"><img src="http://etechGuide.in/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Save/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://etechGuide.in/web/java-script/refresh-parent-page-using-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Json &#8211; JavaScript Standard Object Notation</title>
		<link>http://etechGuide.in/web/java-script/json-javascript-standard-object-notation/</link>
		<comments>http://etechGuide.in/web/java-script/json-javascript-standard-object-notation/#comments</comments>
		<pubDate>Sun, 07 Jun 2009 07:35:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java Script]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://etechGuide.in/?p=251</guid>
		<description><![CDATA[When we send a request to the server , we expect a response. When we make an Ajax call to server with the help of XMLHttpRequest ,we have an option to get the response in many different formats. It can be a plain text , CSV(comma separated) , Xml etc. If the return data is in complex form or in a form of an object you need something called JSON. This post would give you a start up confidence to use JSON.]]></description>
			<content:encoded><![CDATA[<p>When we send a request to the server , we expect a response. When we make an Ajax call to server with the help of <a href="http://etechguide.in/web/ajax/ajax-xmlhttprequest-object/">XMLHttpRequest</a> ,we have an option to get the response in many different formats. It can be a plain text , CSV(comma separated) , Xml etc. If the return data from the server is smple text , the way to retrieve is as simple as doing
<pre class="brush: jscript">responseVal = response.responseText</pre>
<p> If response is in the form of Xml ,
<pre class="brush: jscript">responseVal = response.responseXML</pre>
<p> or an object then? Manipulating response always takes some extra effort .<br />
<div id="imagedetailsobj" class="wp-caption alignleft" style="width: 610px"><br />
<img class="size-full wp-image-38" title="Image Details Object" src="http://etechGuide.in/wp-content/uploads/2009/06/imagedetailsobj.jpg" alt="Image Details Object" title="Image Details Object" width="525" height="278" /><br />
<p class="wp-caption-text">Image Details object</p></div><br />
Refer to the image above . Lets assume we have an ImageDetails Object which has the properties lke ,image id,image description and the resource urls to get the resources(images in this case).When we send a request to the server , saying getImageDetails , we get an object in respose which has all the details of the image (day , id , description and the url to get it).<br />
If the response object is in CSV format , JavaScript have to use a string manipulation method like split()<br />
to return the string into individual data.
<pre class="brush: jscript">imageDetails = response.split(&quot;,&quot;);</pre>
<p>With XML response , we would get a DOM representation of the text using the request object&#8217;s responseXML property.Then we have to use all the DOM mechanisms to work with the object , instead of actual property names.<br />
Here we ca think of something called JSON , i.e  JavaScript Standard Object otation.If you need to represent object in your JavaScript, then you should look at Json.When you get Json data from server, you are just getting text which can be easily converted into a JavaScript Object. then you can use dot notation to access the propertirs(fields) of an object like, var imageId = imageDetailsObj.id; When server sends a Json data , the data comes to you as a text , so , it is easy to get the response data using responseText propery of the request object. var jsonata = request.responseText; Lets see , how a Json data look like , </p>
<pre class="brush: css">
imageDetails = {&quot;id&quot; : &quot;roseImage&quot;,
				&quot;description&quot; : &quot;Rose is red&quot;,
				&quot;resources&quot;:[&quot;http://www.etechGuide.in/&quot;,
							&quot;http://www.roseValie.com&quot;]}
</pre>
<p>Json data is enclosed within curly braces : {json data}. The above imageDetails is an Json object(enclosed in curly braces) consists of two Json String , &#8220;id&#8221;:&#8221;roseImage&#8221; and &#8220;description&#8221;:&#8221;Rose is red&#8221;. The imageDetails json object has an array which is enclosed in quare braces:[json array]. Each json string is a key-value pair , like , id : roseImage etc.<br />
Once we get the Json object in response text , we have to evaluate the Json text to convert into a java script object. JavaScript provides a very strong method called eval(); which helps us to convert the Json text into a object form. If you pass a text to the eval method , JavaScript would run the statement and give back the result. Hence , </p>
<pre class="brush: jscript">
var imageDetails = request.responseText;
imageDetails = eval(&#039;(&#039; + imageDetails + &#039;)&#039;);
</pre>
<p>would convert the servers response into an object. Once the eval() executed , you can access the object property directly as , imageDetails.id , imageDetails.description , imageDetails.resources. You can get all the image resource urls as,</p>
<pre class="brush: jscript">
for(var i=0; i&lt;imageDetails.resources.length; i++) {
  var resource = itemDetails.resources[i];
}
</pre>
<p>Calling eval(); does a lot to evaluate the Json data but we need some more steps to make sure the following things are takes care.<br />
1. A malicious script is not there in our Json data coming from the server.<br />
2. The Json data coming from the server is well-json-formatted.<br />
A simple eval(); can not do all the above. We need a parser to do so. Fortunately http://www.json.org provides a Json parser that does all the above.You can download the script from json.org named json2.js and then use the following command to parse Json-formatted data ,</p>
<pre class="brush: jscript">var imageDetails = JSON.parse(request.responseText);</pre>
<p>The JSON object is created when json2.js is loaded by the web browser.parse() method takes the string as an input and return the an object if the input string is a valid Json-formatted data. So it&#8217;s always good to use JSON.parse(String) instead of eval().</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?&amp;linkurl=http%3A%2F%2FetechGuide.in%2Fweb%2Fjava-script%2Fjson-javascript-standard-object-notation%2F&amp;linkname=Json%20%26%238211%3B%20JavaScript%20Standard%20Object%20Notation"><img src="http://etechGuide.in/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Save/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://etechGuide.in/web/java-script/json-javascript-standard-object-notation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ajax &#8211; XMLHttpRequest Object</title>
		<link>http://etechGuide.in/web/ajax/ajax-xmlhttprequest-object/</link>
		<comments>http://etechGuide.in/web/ajax/ajax-xmlhttprequest-object/#comments</comments>
		<pubDate>Mon, 04 May 2009 11:16:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[XmlHttpRequest]]></category>

		<guid isPermaLink="false">http://etechGuide.in/?p=119</guid>
		<description><![CDATA[Ajax is nothing but getting the XMLHttpRequest and work on it to get or post data. Forming an XMLHttpRequest varies form browser to browser. This post shows , how to construct XMLHttpRequest object seamlessly.]]></description>
			<content:encoded><![CDATA[<p><strong>XMLHttpRequest</strong> Object can be created in the following way :</p>
<pre class="brush: jscript">

function newXMLHttpRequest() {
var xmlreq = false;
if (window.XMLHttpRequest) {
// Create XMLHttpRequest object in non-Microsoft browsers
xmlreq = new XMLHttpRequest();
} else if (window.ActiveXObject) {
// Create XMLHttpRequest via MS ActiveX
try {
// Try to create XMLHttpRequest in later versions of Internet Explorer
xmlreq = new ActiveXObject(&quot;Msxml2.XMLHTTP&quot;);
} catch (e1) {
// Failed to create required ActiveXObject
try {
// Try version supported by older versions of Internet Explorer
xmlreq = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);
} catch (e2) {
// Unable to create an XMLHttpRequest with ActiveX
}
}
}
return xmlreq;
}
</pre>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?&amp;linkurl=http%3A%2F%2FetechGuide.in%2Fweb%2Fajax%2Fajax-xmlhttprequest-object%2F&amp;linkname=Ajax%20%26%238211%3B%20XMLHttpRequest%20Object"><img src="http://etechGuide.in/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Save/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://etechGuide.in/web/ajax/ajax-xmlhttprequest-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
