【问题标题】:Send values from jsp to Servlet将值从 jsp 发送到 Servlet
【发布时间】:2013-04-27 18:01:14
【问题描述】:

我有一个 jsp,其中我有选择标签,我想得到 以及从我的 Servlet 中的 jsp 中选择的值

<select id="listoffood" name="dropdown" onchange="foodname();">
<option value="bg">Burger</option>
<option value="pas">pasta</option>
<option value="pi">pizza</option>
</select>
<div id='content'></div>

这里是javascript代码

function foodname()
{

  var xmlHttpReq = false;
    var self = this;
    document.getElementById('content').innerHtml='';
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }

    self.xmlHttpReq.open('GET', "InformationServlet", true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.send(null);

    self.xmlHttpReq.onreadystatechange= function ()
    {
        //alert(document.getElementById('content'));
        if (self.xmlHttpReq.readyState==4)
        {
        if (self.xmlHttpReq.status == 200)
        {

        document.getElementById('content').innerHTML=self.xmlHttpReq.responseText;
        }
        }
    };

}

我所做的是使用这样的获取属性,但它无法正常显示为空

protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException 

    {
    // TODO Auto-generated method stub

    String coun = request.getParameter("dropdown");
    PrintWriter out=response.getWriter();
    System.out.println("here : "+coun);
}

提前致谢,我们非常感谢您提供任何代码。

【问题讨论】:

  • 它会转到同一个 servlet 吗?发布您的完整 html 表单
  • 方法foodName()是做什么的?您的选择在form 内吗?
  • #kshitj 食物名称转到 javascript 和 js,而不是通过 ajax 调用 servlet。 @Baadshah 是的,它在 onChange 方法上使用相同的 servlet
  • 你能在你的servlet中从这个JSP页面获取其他参数吗?
  • 我没有任何 html 表单,我的 jsp 上只有一个选择标签 @AnkitSingla 我的 jsp 上没有其他参数,只有选择标签

标签: java jsp servlets


【解决方案1】:

使用方法getParameterValues(String).
这是因为&lt;select&gt;标签可以有多个选择值(例如select multiple

String[] coun = request.getParameterValues("dropdown");

【讨论】:

  • 我知道有人会发布这个。 multiple 不适用于 select
【解决方案2】:

查看这篇文章:

How to use onClick() or onSelect() on option tag in a JSP page?

您似乎在使用 select 标记而没有任何操作,例如 onchange

【讨论】:

  • onchange=foodname(); 我想你错过了!
  • @AnishSharma 在select 上使用事件处理程序是相当普遍的,而不是这里的问题。你不需要改变它。
【解决方案3】:

您可以使用

从列表框中获取选定的项目文本
var e = document.getElementById("dropdown");
var selectedValue = e.options[selectBox.selectedIndex].value

在您的 JS 方法中并使用 servlet 的链接发送此值。

【讨论】:

  • 如何通过 servlet 链接发送值
  • InformationServlet?dropdown=selectedValue 并使用 request.getParameter("dropdown");在你的 servlet 中,就像你已经做的那样。
  • Distributorindex.jspInformationServletrequestData.InformationServletInformationServlet/InformationServlet
  • @AnishSharma 你检查我的答案了吗?
  • @AnishSharma :您是否为 参数提供了正确的包路径??
【解决方案4】:

从选择标签中获取值

var e = document.getElementById("dropdown");
var selectedValue = e.options[selectBox.selectedIndex].value;

编辑js函数

self.xmlHttpReq.open('GET', "InformationServlet?dropdown="selectedValue , true);

最后你应该看起来像

function foodname()
{

    var e = document.getElementById("dropdown");
    var selectedValue = e.options[selectBox.selectedIndex].value;

  var xmlHttpReq = false;
    var self = this;
    document.getElementById('content').innerHtml='';
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }

    self.xmlHttpReq.open('GET', "InformationServlet?dropdown="selectedValue , true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.send(null);

    self.xmlHttpReq.onreadystatechange= function ()
    {
        //alert(document.getElementById('content'));
        if (self.xmlHttpReq.readyState==4)
        {
        if (self.xmlHttpReq.status == 200)
        {

        document.getElementById('content').innerHTML=self.xmlHttpReq.responseText;
        }
        }
    };
}

【讨论】:

  • 你能分享你的 web.xml 文件吗
【解决方案5】:

只需将您的 AJAX open() 请求更改为

var select = document.getElementById("listoffood");
self.xmlHttpReq.open('GET', "InformationServlet?dropdown=" + select.options[select.selectedIndex].value, true);

【讨论】:

  • 它的localhost:8080/Distributor/InformationServlet 并在你给出的代码中显示错误.. 错误是无法读取未定义的属性“未定义”
  • 在 js 中显示错误的 url 没有任何问题。我在之前的评论中提到了该错误
  • @AnishSharma 你试过var select吗?
  • 是的,我做的比显示无法读取未定义的属性“选项”
  • @AnishSharma 我刚刚测试了代码并成功地为http://www.google.com/search?q=pasta 执行了 GET。你能做一个alert(document.getElementById("listoffood"));吗?根据你的HTML,这不应该是undefined。请核实。 (这里有两个“f”)
猜你喜欢
  • 2013-03-29
  • 1970-01-01
  • 2015-07-26
  • 2021-08-03
  • 2013-08-13
  • 2012-09-06
  • 2012-11-14
  • 1970-01-01
相关资源
最近更新 更多