注册 登录
编程论坛 J2EE论坛

ajax异步:jsp无法传输数据到Servlet????

渐渐鱼 发布于 2018-10-11 16:50, 7609 次点击
jsp代码
程序代码:
<%@ page pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>省会查询</title>
</head>
<body>
<script>
function ajaxProcess(obj)
{
    var xhr = new XMLHttpRequest();
    var text=obj.value;
    var url="ajax/ajaxServlet?text="+text;

    xhr.onreadystatechange=function()
    {
        if(xhr.readyState==4&&xhr.status==200)
        {
        doucment.getElementById("provincial_captial").value=xhr.responseText;
        }
    };
   
    xhr.open("GET",url,true);
    xhr.send();
   
   
}
</script>
<h1>获取省份对应的省会信息</h1>
<label>输入省份</label><input type="text" id="province" onblur="ajaxProcess(this)">
<br><br>
<label>对应省会</label><input type="text" id="provincial_capital">

</body>
</html>


ajaxServlet.java
程序代码:
package ajax;

import import import java.util.HashMap;
import java.util.Map;

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

public class ajaxServlet extends HttpServlet
{
    private static final long serialVersionUID = 1L;
      
   
    public ajaxServlet()
    {
        super();
     
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        Map<String,String> map = new HashMap<String,String>();
        map.put("上海市","上海");
        map.put("湖北省","武汉");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        
        String text = request.getParameter("text");
        out.print(text);
        out.print("<br>");
        String provincial_captial = map.get(text);
        
        if(provincial_captial==null)
        {
            out.print("Error");
        }
        else
        {
            out.print(provincial_captial);
        }   
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
   
        doGet(request, response);
    }

}


确认过了,input的值在没有传过去?是url有问题吗??
12 回复
#2
林月儿2018-10-11 17:00
我猜你是没配置请求的路径吧web.xml写了啥
#3
渐渐鱼2018-10-12 12:10
回复 2楼 林月儿
跟以前一样配置的啊
贴出来给你

程序代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www. xmlns:web="http://xmlns. xsi:schemaLocation="http://xmlns. http://java. <servlet-name>ajaxServlet</servlet-name>
    <servlet-class>ajax.ajaxServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ajaxServlet</servlet-name>
    <url-pattern>/ajax/ajaxServlet</url-pattern>
  </servlet-mapping>

 
</web-app>
#4
林月儿2018-10-12 15:24
浏览器地址写了啥
#5
渐渐鱼2018-10-12 15:27
回复 4楼 林月儿
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录

页面都挺正常,但是数据没传过去。。
#6
wlrjgzs2018-10-12 17:46
问题在ajaxServlet.java上。
#7
渐渐鱼2018-10-12 17:50
回复 6楼 wlrjgzs
嗯??代码编译没问题啊?语义上的问题??
#8
林月儿2018-10-12 18:44

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        Map<String,String> map = new HashMap<String,String>();
        map.put("上海市","上海");
        map.put("湖北省","武汉");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        
        String text = request.getParameter("text");
        out.print(text);
        out.print("<br>");
        String provincial_captial = map.get(text);
        
        if(provincial_captial==null)
        {
            out.print("Error");
        }
        else
        {
            out.print(provincial_captial);
        }   
    }
#9
渐渐鱼2018-10-12 22:29
回复 8楼 林月儿
??眼盲,没看出来有问题啊。
#10
wlrjgzs2018-10-13 13:16
以下是引用渐渐鱼在2018-10-12 17:50:18的发言:

嗯??代码编译没问题啊?语义上的问题??

不是语法问题,而是逻辑上存在问题。
#11
渐渐鱼2018-10-14 13:22
回复 10楼 wlrjgzs
老铁,可以来点实际的不?怎么改啊??多谢

[此贴子已经被作者于2018-10-14 13:48编辑过]

#12
林月儿2018-10-14 19:14
function ajaxProcess(obj)
{
    var xhr = new XMLHttpRequest();
    var text=obj.value;

这里加个弹窗看看结果
alert(text);
#13
渐渐鱼2018-10-14 21:08
回复 12楼 林月儿
鬼使神差的今天就好了
我怀疑这个tomcat有毒。。。。

1