一个简单的ajax+servlert+jsp的完整实例

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">今天写了人生第一个ajax,绝对简单通俗,一看就懂。</span>

因为路径问题,还搞了半个小时,所以这是一个菜鸟的进阶。不过想想,ajax这种事情半年前就该学会的。半年前在那个接触各种代码的公司实习的时候就该会这个了,可是对新生事物有种畏惧的感觉,这种心理实在是不该。想想实习也有一年左右了,但是学的东西还是很少。毕设更是还没有开始,总之,继续吧,开始忙毕设了,感觉要炸了。下面是今天做得ajax完整的简单的实例,希望可以对和我一样新人学习有帮助。

随意命名的servlet

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class SelectCityServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		 response.setContentType("text/xml");
         response.setHeader("Cache-Control", "no-cache");
         String a = "Hello Man!";
//         String state = request.getParameter("state");
//         StringBuffer sb=new StringBuffer("<state>");
//         if ("zj".equals(state)){
//                 sb.append("<city>hangzhou</city><city>huzhou</city>");
//         } else if("zs".equals(state)){
//                 sb.append("<city>nanjing</city><city>yangzhou</city><city>suzhou</city>");
//         }
//         sb.append("</state>");
         PrintWriter out=response.getWriter();
         out.write(a);
         out.flush();
         out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
	}

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>SelectCityServlet</servlet-name>
    <servlet-class>SelectCityServlet</servlet-class>
  </servlet>

  <servlet-mapping>
 <servlet-name>SelectCityServlet</servlet-name>
 <url-pattern>/servlet/SelectCityServlet</url-pattern>
 </servlet-mapping> 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

jsp页面,注意url的写法

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	 <script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
var str=1;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {  
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }  
xmlhttp.open("GET","servlet/SelectCityServlet",true);
xmlhttp.send();
}
 </script>
  </head>
  
  <body>
<!--    <select name="state" onChange="getResult(this.value)">-->
<!--         <option value="">Select</option>>-->
<!--         <option value="zj">ZEHJIANG</option>>-->
<!--         <option value="zs">JIANGSU</option>>-->
<!-- </select>-->
<!-- <select id="city">-->
<!--     <option value="">CITY</option>-->
<!-- </select>-->
<div id="myDiv"><h2>使用 AJAX 修改该文本内容</h2></div>
<button type="button" οnclick="loadXMLDoc()">修改内容</button>

  </body>
</html>





  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值