工作需要自己寫了個例子調用SERVLET的,可以運行,
很簡單就是一個index.jsp頁面,一個GetAndPostExample servlet後台,和WEB.XML設定檔
index.jsp頁面
-------------------------------------------------------------------------------------------------------
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%><br /><%request.setCharacterEncoding("GB2312");%><br /><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /><head><br /><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><br /><title>AJAX測試</title><br /><mce:script language="javascript"><!--<br />var xmlHttp;<br />//建立xmlHttp<br />function createXMLHttpRequest()<br />{<br /> if(window.ActiveXObject)<br /> {<br /> xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");<br /> }<br /> else if(window.XMLHttpRequest)<br /> {<br /> xmlHttp=new XMLHttpRequest();<br /> }<br />}</p><p>//拼出要發送的姓名資料<br />function createQueryString()<br />{<br /> var firstName=document.getElementById("firstname").value;<br /> var middleName=document.getElementById("middleName").value;<br /> var birthday=document.getElementById("birthday").value;</p><p> var queryString="firstName=" + firstName + "&middleName=" + middleName + "&birthday=" + birthday;<br /> return queryString;<br />}</p><p>//使用get方式發送<br />function doRequestUsingGET()<br />{<br /> createXMLHttpRequest();<br /> var queryString="./GetAndPostExample?";<br /> queryString=queryString+createQueryString() + "&timeStamp=" + new Date().getTime();<br /> xmlHttp.onreadystatechange=handleStateChange;<br /> xmlHttp.open("GET",queryString,true);<br /> xmlHttp.send(null);<br />}</p><p>//使用post方式發送<br />function doRequestUsingPost()<br />{<br /> createXMLHttpRequest();<br /> var url="./GetAndPostExample?timeStamp=" + new Date().getTime();<br /> var queryString=createQueryString();<br /> xmlHttp.open("POST",url,true);<br /> xmlHttp.onreadystatechange=handleStateChange;<br /> xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");<br /> xmlHttp.send(queryString);<br />}</p><p>function handleStateChange()<br />{<br /> if(xmlHttp.readyState==4)<br /> {<br /> if(xmlHttp.status==200)<br /> {<br /> parseResults();<br /> }<br /> }<br />}</p><p>//解析傳回值<br />function parseResults()<br />{<br /> var responseDiv=document.getElementById("serverResponse");<br /> if(responseDiv.hasChildNodes())<br /> {<br /> responseDiv.removeChild(responseDiv.childNodes[0]);<br /> }<br /> var responseText=document.createTextNode(xmlHttp.responseText);<br /> alert("後台返回的傳回值: "+xmlHttp.responseText);<br /> responseDiv.appendChild(responseText);<br />}<br />// --></mce:script><br /></head></p><p><body><br /><form id="form1" name="form1" method="post" action="#"><br /> <p><br /><br /> <br /><br /> 姓:<input name="firstName" type="text" id="firstName" /><br /></p><br /> <p><br /> <label><br /> 名:<input type="text" name="middleName" id="middleName" /><br /> </label><br /></p><br /> <p><br /> 生日:<input name="birthday" type="text" id="birthday" /><br /> </p><br /> <p> </p><br /> <p><br /> <input type="button" name="Submit" value="GET" onclick="doRequestUsingGET();"/><br /> <br /> <input type="button" name="Submit2" value="POST" onclick="doRequestUsingPost();"/><br /> </p></p><p> <div id="serverResponse"></div><br /></form></p><p></body><br /></html></p><p>
-------------------------------------------------------------------------------------------------------
GetAndPostExample
-------------------------------------------------------------------------------------------------------
package temp;</p><p>import java.io.IOException;<br />import java.io.PrintWriter;</p><p>import javax.servlet.ServletException;<br />import javax.servlet.http.HttpServlet;<br />import javax.servlet.http.HttpServletRequest;<br />import javax.servlet.http.HttpServletResponse;</p><p>public class GetAndPostExample extends HttpServlet {</p><p>/**<br /> * Constructor of the object.<br /> */<br />public GetAndPostExample() {<br />super();<br />}</p><p>/**<br /> * Destruction of the servlet. <br><br /> */<br />public void destroy() {<br />super.destroy(); // Just puts "destroy" string in log<br />// Put your code here<br />}</p><p>/**<br /> * The doGet method of the servlet. <br><br /> *<br /> * This method is called when a form has its tag value method equals to get.<br /> *<br /> * @param request<br /> * the request send by the client to the server<br /> * @param response<br /> * the response send by the server to the client<br /> * @throws ServletException<br /> * if an error occurred<br /> * @throws IOException<br /> * if an error occurred<br /> */<br />public void doGet(HttpServletRequest request, HttpServletResponse response)<br />throws ServletException, IOException {</p><p>doPost(request, response);<br />}</p><p>/**<br /> * The doPost method of the servlet. <br><br /> *<br /> * This method is called when a form has its tag value method equals to<br /> * post.<br /> *<br /> * @param request<br /> * the request send by the client to the server<br /> * @param response<br /> * the response send by the server to the client<br /> * @throws ServletException<br /> * if an error occurred<br /> * @throws IOException<br /> * if an error occurred<br /> */<br />public void doPost(HttpServletRequest request, HttpServletResponse response)<br />throws ServletException, IOException {</p><p>String data = "";<br />String temp = "";</p><p>temp = (String) request.getParameter("firstName");<br />data = data + "第一個名字" + temp;<br />temp = (String) request.getParameter("middleName");<br />data = data + " 中間的名字" + temp;<br />temp = (String) request.getParameter("birthday");<br />data = data + " 生日" + temp;<br />temp = (String) request.getParameter("timeStamp");<br />data = data + " 調用時間" + temp;</p><p>System.out.println("獲得的資料 " + data);</p><p>response.setContentType("text/html;charset=gb2312");</p><p>PrintWriter out = response.getWriter();</p><p>out.println(data);<br />out.flush();<br />out.close();<br />}</p><p>/**<br /> * Initialization of the servlet. <br><br /> *<br /> * @throws ServletException<br /> * if an error occurs<br /> */<br />public void init() throws ServletException {<br />// Put your code here<br />}</p><p>}<br />
-------------------------------------------------------------------------------------------------------
web.xml
-------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?><br /><web-app version="2.4"<br />xmlns="http://java.sun.com/xml/ns/j2ee"<br />xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br />xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee<br />http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><br /> <servlet><br /> <description>This is the description of my J2EE component</description><br /> <display-name>This is the display name of my J2EE component</display-name><br /> <servlet-name>GetAndPostExample</servlet-name><br /> <servlet-class>temp.GetAndPostExample</servlet-class><br /> </servlet></p><p> <servlet-mapping><br /> <servlet-name>GetAndPostExample</servlet-name><br /> <url-pattern>/GetAndPostExample</url-pattern><br /> </servlet-mapping><br /> <welcome-file-list><br /> <welcome-file>index.jsp</welcome-file><br /> </welcome-file-list><br /></web-app><br />
-------------------------------------------------------------------------------------------------------