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

前端之家收集整理的这篇文章主要介绍了一个简单的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,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" onclick="loadXMLDoc()">修改内容</button>

  </body>
</html>
原文链接:https://www.f2er.com/ajax/162373.html

猜你在找的Ajax相关文章