从jquery调用servlet时出现问题。 [英] Problem while calling a servlet from jquery.

查看:84
本文介绍了从jquery调用servlet时出现问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在Test.JSP中编写这样的代码

< html>

< head>

 <   script  >  
$(document).ready(function(){
$('#country')。change(function (事件){
var $ country = $(select#country)。val();
$ .get('ActionServlet',{countryname:$ country},function(responseJson){
var $ select = $('#states');
$ select.find('option')。remove();
$ .each(responseJson,function(key,value){
$('< 选项 > ')。val(key).text(value).appendTo($ select);
});
});
});
}) ;
< / script >



 <   / head  >  
< body >
< < < span class =code-leadattribute> / h1 >
选择国家/地区:
< 选择 id = country & gt;
< 选项 已选择 = 已选择 > 选择国家< / option >
< option value = 印度 > 印度< / option >
< < span class =code-leadattribute>选项 = 美国 > 美国< / option >
< / select > ;
< br / >
< br / >
选择状态:
< 选择 id = 声明 >
< 选项 已选择 = 选中 > 选择州< / option >
< ; / select >



< / body>

< / html>





和servlet


------------------------------------- ---------------------



  public   class  ActionServlet扩展HttpServlet {
private static final long serialVersionUID = 1L;


public ActionServlet(){
// TODO自动生成的构造函数存根
}


protected void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException {

String country = request.getParameter( countryname);
Map< String,String> ind = new LinkedHashMap< String,String>();
ind.put( 1 新德里);
ind.put( 2 Tamil Nadu);
ind.put( 3 喀拉拉邦);
ind.put( 4 Andhra Pradesh);

Map< String,String> us = new LinkedHashMap< String,String>();
us.put( 1 华盛顿);
us.put( 2 California);
us.put( 3 佛罗里达州);
us.put( 4 纽约);

Map< String,String> reset = new LinkedHashMap< String,String>();
reset.put( 1 选择状态);

字符串 json = null ;
if (country.equals( India) )){
json = new Gson()。toJson(ind);
}
else if (country.equals( US)){
json = new Gson ().toJson(美国);
}
else if (country.equals( 选择国家))
{
json = new Gson()。toJson(重置);
}
response.setContentType( application / json);
response.setCharacterEncoding( UTF-8);
response.getWriter()。write(json);


}


受保护 void doPost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException {
// TODO Auto生成方法存根

}

}





< Web.xml中的
我写的是这样的,

 <   servlet  >  
< servlet-name > ActionServlet < / servlet-name >
< ; servlet-class > Test.Sales.ActionServlet < / servlet-class >
< / servlet >
< servlet-映射 >
< servlet-name > ActionServlet < / servlet-name >
< url-pattern > / ActionServlet / * < / url-pattern & gt;
< / servlet-mapping >







所以,我的问题是当我保留我的时候网页文件夹中的Test.Jsp文件,它是工作文件。

但是如果我添加一个文件夹并将这个Test.jsp文件放在那里,我就得不到结果了。



我正在使用netbeans。并且要求是根据所选国家获得状态。



请做必要的

解决方案

(document).ready(function(){


('#country')。change(function(event){
var


< BLOCKQUOTE>国=

Hi,

i'm Writing code like this, in Test.JSP
<html>
<head>

<script>
         $(document).ready(function() {
        $('#country').change(function(event) {
        var $country=$("select#country").val();
           $.get('ActionServlet',{countryname:$country},function(responseJson) {
               var $select = $('#states');
               $select.find('option').remove();
               $.each(responseJson, function(key, value) {
                   $('<option>').val(key).text(value).appendTo($select);
                    });
            });
        });
    });
</script>


</head>
    <body>
        <h1>AJAX calls to Servlet using JQuery and JSON</h1>
Select Country:
<select id="country">
<option selected="selected">Select Country</option>
<option value="India">India</option>
<option value="US">US</option>
</select>
<br/>
<br/>
Select State:
<select id="states">
<option selected="selected">Select State</option>
</select>


</body>
</html>


and in servlet

----------------------------------------------------------

public class ActionServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    public ActionServlet() {
        // TODO Auto-generated constructor stub
    }


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

        String country=request.getParameter("countryname");
        Map<String, String> ind = new LinkedHashMap<String, String>();
        ind.put("1", "New delhi");
        ind.put("2", "Tamil Nadu");
        ind.put("3", "Kerala");
        ind.put("4", "Andhra Pradesh");

        Map<String, String> us = new LinkedHashMap<String, String>();
        us.put("1", "Washington");
        us.put("2", "California");
        us.put("3", "Florida");
        us.put("4", "New York");

        Map<String, String> reset = new LinkedHashMap<String, String>();
        reset.put("1", "Select State");

        String json = null ;
        if(country.equals("India")){
            json= new Gson().toJson(ind);
        }
        else if(country.equals("US")){
            json=new Gson().toJson(us);
        }
        else if(country.equals("Select Country"))
        {
            json=new Gson().toJson(reset);
        }
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);


    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

    }

}




in Web.xml i'm writing like this,

<servlet>
    <servlet-name>ActionServlet</servlet-name>
    <servlet-class>Test.Sales.ActionServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ActionServlet</servlet-name>
    <url-pattern>/ActionServlet/*</url-pattern>
  </servlet-mapping>




So, My problem is when i keep my Test.Jsp File in Web Pages Folder, It is working file.
But if i add a folder and place this Test.jsp file there, I'm not getting the result.

I'm using netbeans. ANd the requirement is to get the state based on the selected Country.

Pls do the needful

解决方案

(document).ready(function() {


('#country').change(function(event) { var


country=


这篇关于从jquery调用servlet时出现问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆