Ajax的get请求

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 id="WebApp_ID" version="2.5">
 <!-- 容器对于web.xml的加载过程是context-param、listener、filter、servlet-->
 
 <!-- 配置Struts2的核心Filter -->
 <filter>
  <filter-name>struts</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 </filter>
 <!-- 配置Struts2拦截的请求    /* 表示所有请求 -->
 <filter-mapping>
  <filter-name>struts</filter-name>
  <url-pattern>*.action</url-pattern>
  <url-pattern>*.jsp</url-pattern>
  <url-pattern>*.do</url-pattern>
 </filter-mapping>
 
 <!-- 配置错误页面 -->
 <error-page>
  <error-code>500</error-code>
  <location>/pages/login/error.jsp</location>
 </error-page>
 <error-page>
  <error-code>404</error-code>
  <location>/pages/login/error.jsp</location>
 </error-page>
 <error-page>
  <exception-type>java.lang.Exception</exception-type>
  <location>/pages/login/error.jsp</location>
 </error-page>
 
 <!-- 配置Web应用的首页列表 -->
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>

struts.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 <package name="user" extends="struts-default">
  <action name="ajaxLogin" class="com.huawei.mun.AjaxAction" method="ajaxLogin">   
   <result>index.jsp</result>
  </action>
 </package>
</struts>

AjaxAction.java:

package com.huawei.mun;

import java.io.PrintWriter;
import java.net.URLDecoder;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class AjaxAction extends ActionSupport {
 /** 处理AJAX请求:异步请求 **/
 public String ajaxLogin() {
  try {
   // 读取请求的参数
   HttpServletRequest request = ServletActionContext.getRequest();
   String responseText = request.getParameter("responseText");
   System.out.println("后台得到的数据:"+responseText);
   responseText = "网页不刷新,我变了";
   // 获取response对象
   HttpServletResponse response = ServletActionContext.getResponse();
   // 设置字符集
   response.setContentType("text/html;charset=GBK");
   PrintWriter out = response.getWriter();
   // 直接输出响应的内容
   out.print(responseText);

   //out.write(responseText);
   out.flush();
   out.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  return null;// 不需要跳转某个视图 因为上面已经有了直接输出的响应结果
 }
}

index.jsp:

<%@ page language="java" import="java.util.*" contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme() + "://"
   + request.getServerName() + ":" + request.getServerPort()
   + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>123</title>
<script language="javascript">  
   //定义一个变量用于存放XMLHttpRequest对象   
   var xmlHttp;
   //改函数用于创建一个XMLHttpRequest对象   
   function createXMLHttpRequest(){   
       if(window.ActiveXObject){   
           xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");   
       }else if(window.XMLHttpRequest){   
           xmlHttp = new XMLHttpRequest();   
       }   
   }

   //这是一个启动AJAX异步通信的方法   
   function ajaxLogin(){   
       var ln = document.getElementById("returnMes").value; 
       //创建一个XMLHttpRequest对象
       createXMLHttpRequest();
       //将状态绑定到一个函数   
       xmlHttp.onreadystatechange=processAjaxLogin;   
       //通过GET方法向指定的URL建立服务器的调用   
       var url="ajaxLogin.action?responseText="+encodeURI(ln);
       xmlHttp.open("GET",url,true);
       //发送请求   
       xmlHttp.send(null);   
   }   
   //这是一个用来处理状态改变的函数   
   function processAjaxLogin(){   
       //定义一个变量用于存放 从服务器返回的响应结果   
       var responseContext="";   
       if(xmlHttp.readyState==4){
           if(xmlHttp.status==200){
               responseContext = xmlHttp.responseText;
               document.getElementById("returnMes").value=responseContext;
           }   
       }
   }   
</script>
</head>
<body>
<input type="text" id="returnMes">       
<input type="button" value="异步请求" οnclick="ajaxLogin();">
<table>
<tr>
<td>我是不刷新的</td>
</tr>
<tr>
<td>我是不刷新的</td>
</tr>
<tr>
<td>我是不刷新的</td>
</tr>
<tr>
<td>我是不刷新的</td>
</tr>
<tr>
<td>我是不刷新的</td>
</tr>
<tr>
<td>我是不刷新的</td>
</tr>
</table>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
AjaxGET请求是一种向服务器获取数据的方式。它可以通过XMLHttpRequest对象进行原生底层写法,也可以使用jQuery进行封装后的使用。 在原生底层写法中,首先需要创建一个XMLHttpRequest对象,并通过open函数指定请求方式和URL地址。然后调用send函数发起Ajax请求。同时,还需要监听XMLHttpRequest对象的onreadystatechange事件,在事件处理函数中判断readyState和status属性的值,以确定服务器响应完成且响应成功。如果满足条件,可以通过responseText属性获取服务器响应的数据。这是一个典型的GET请求的底层写法。 而在使用jQuery进行封装后的写法中,可以直接使用$.ajax或$.get等函数进行GET请求。这些函数已经对XMLHttpRequest对象进行了封装,极大地降低了Ajax的使用难度。可以通过传递URL地址和一些可选参数来发起GET请求,并通过回调函数处理服务器响应的数据。 总之,AjaxGET请求可以通过原生底层写法或使用jQuery进行封装后的写法来实现。无论哪种方式,都可以实现向服务器获取数据的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Ajax-GET请求](https://blog.csdn.net/qq_43551801/article/details/120048178)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值