如果我preSS提交正在进入到其他的URL在sevlet按钮其他的、按钮、preSS、sevlet

2023-09-10 15:52:55 作者:拱手江山讨你欢

这是我的javascript code发生了什么事的时​​候,我preSS提交按钮第二次是去到另一个URL,但在第一次被给予,结果罚款谁能帮助我PLZ

 函数validatenewcat(){
        。VAR类=的document.getElementById(猫」)的价值;
        如果(类别==){
            的setTimeout(的document.getElementById(验证)的innerHTML =PLZ输入类别名称!,2000。);
            返回false;
        }
    其他{
        VAR URL =catnamecheck.do?id =+范畴;
         xmlhttp.open(后,网址,真实);
         xmlhttp.send(空);
         xmlhttp.onreadystatechange =功能(){
          如果(xmlhttp.readyState == 4){
                 如果(xmlhttp.status == 200){
                    变种临时= xmlhttp.responseText;
                    物镜= JSON.parse(临时);
                  如果(obj.catgoryname ==类){
                     警报(obj.catgoryname);
                        警报(类);
                         。的document.getElementById(验证)的innerHTML =!PLZ输入唯一的类别名称;
                        返回false;
                     }
                 如果(obj.catgoryname!=类别){
                    警报(里真正的+ obj.catgoryname);
                    警报(里真正的+类别);
                       返回true;
                     }
                  }
              }
           }
        }
    }
 

这是我的Java code我打电话从阿贾克斯本code

 公开地图<字符串,字符串> catuniqecheck(字符串ID){
    连接C = NULL;
    preparedStatement PS1 = NULL;
    结果集RS1 = NULL;
    字符串SQL = NULL;
    尝试{
        C = JDBCHelper.getConnection();
        如果(C!= NULL)
        {
            地图<字符串,字符串>图=新的HashMap<字符串,字符串>();
            SQL =SELECT * FROM catgory哪里catgoryname =?;
            PS1 = C prepareStatement(SQL)。
            ps1.setString(1,ID);
            ps1.execute();
            RS1 = ps1.getResultSet();
            如果(rs1.next())
            {
                的System.out.println(下称catuniqecheck的insdide);
                map.put(catgoryname,rs1.getString(catgoryname));
            }
            返回地图;
        }
       其他
         {
               的System.out.println(DB连接建立);
               返回null;
         }
        }
        赶上(例外五){
            // TODO:处理异常
               返回null;
        }
最后{
            JDBCHelper.close(RS1);
            JDBCHelper.close(PS1);
            JDBCHelper.close(C);
       }
}
 
servlet 跳转问题,写了个servlet,点击提交报错404,以下详细

这是我sevlet code

 如果(uri.contains(/ catnamecheck.do)){
        项目P1 =新项目();
        字符串ID =的request.getParameter(ID);
        的System.out.println(里uniq的检查ID+ ID);
        地图<字符串,字符串>结果= p1.catuniqecheck(ID);
        如果(结果!= NULL)
        {
            的System.out.println(里成功);
            JSONObject的JSON =新的JSONObject();
            json.accumulateAll(结果);
            的System.out.println(里JSON+ json.toString());
            response.setContentType(应用/ JSON);
            。response.getWriter()写(json.toString());
        }
        如果(结果== NULL)
        {
            的System.out.println(里filure);
        }
    }
 

这是我的jsp code

 <表格名称=FRM行动=createnewcatgoryBean.jsp的onsubmit =返回validatenewcat()方法=邮报>
<表格样式=边距:5px的0px 0px加上了8px;>
&其中; TR>
百分位列跨度=2>
< D​​IV的风格=宽度:宽度:271px;颜色:红色; ID =验证>< / DIV>
< /第i个
< / TR>
&其中; TR>
<第i个类别名称和LT;跨度>:< / SPAN>< /第i;< TD><输入ID =猫的onblur =返回validatenewcat()类型=文本名称=类别 >
< / TD>
< / TR>
&其中; TR>
<第i个语录形式LT;跨度>:< / SPAN>< /第i;< TD><输入类型=复选框名称=语录>< / TD>
< / TR>
&其中; TR>
百分位>协议形式LT;跨度>:< / SPAN>< /第i;< TD><输入类型=复选框名称=协议和GT;< / TD>
< / TR>
&其中; TR>
百分位>支付形式LT;跨度>:< / SPAN>< /第i;< TD><输入类型=复选框名称=支付>< / TD>
< / TR>
&其中; TR>
百分位> ETI<跨度>:< / SPAN>< /第i;< TD><输入类型=复选框名称=ETI>< / TD>
< / TR>
&其中; TR>
< TD合并单元格=2的风格=浮动:权利;填充顶:15px的>
<输入类型=提交值=提交的风格=宽度:60PX;>
< / TD>
< / TR>
< /表>
< /形式GT;
 

解决方案

您已经设置形式行动=createnewcatgoryBean.jsp。如果验证成功,就会提交到该页面。为了避免提交,返回false从你的JavaScript。

 其他{
    VAR URL =catnamecheck.do?id=+范畴;
    xmlhttp.open(后,网址,真实);
    xmlhttp.send(空);
    xmlhttp.onreadystatechange =功能(){
        如果(xmlhttp.readyState == 4){
            如果(xmlhttp.status == 200){
                变种临时= xmlhttp.responseText;
                物镜= JSON.parse(临时);
                如果(obj.catgoryname ==类){
                    警报(obj.catgoryname);
                    警报(类);
                    。的document.getElementById(验证)的innerHTML =!PLZ输入唯一的类别名称;
                    返回false;
                }
                如果(obj.catgoryname!=类别){
                    警报(里真正的+ obj.catgoryname);
                    警报(里真正的+类别);
                    返回true;
                }
            }
        }
    }

    返回false; //返回false,以避免表单提交
}
 

This is my javascript code what is happening in when i press the submit button second time it is going to the other url but in the first time it is giving the result fine can anyone help me plz

 function validatenewcat(){
        var category = document.getElementById("cat").value;
        if(category==""){
            setTimeout(document.getElementById("validate").innerHTML="!PLz Enter The Category Name", 2000);
            return false;
        }
    else{
        var url="catnamecheck.do?id="+category;
         xmlhttp.open("post", url,true);
         xmlhttp.send(null);
         xmlhttp.onreadystatechange=function(){
          if(xmlhttp.readyState==4){      
                 if(xmlhttp.status==200){   
                    var temp = xmlhttp.responseText;
                    obj = JSON.parse(temp);
                  if(obj.catgoryname==category) {
                     alert(obj.catgoryname);
                        alert(category);
                         document.getElementById("validate").innerHTML="!PLz Enter The Unique Category Name";
                        return false; 
                     }
                 if(obj.catgoryname!=category){
                    alert("inside true"+obj.catgoryname);
                    alert("inside true"+category);
                       return true; 
                     }
                  }
              }
           }
        }
    }

This is my java code i'm calling this code from ajax

public Map<String, String> catuniqecheck(String id) {
    Connection c = null;
    PreparedStatement ps1 = null;
    ResultSet rs1 = null;
    String sql=null;
    try{
        c = JDBCHelper.getConnection();
        if(c!=null)
        {
            Map<String, String> map = new HashMap<String, String>();
            sql="select * from catgory where catgoryname=?";
            ps1=c.prepareStatement(sql);
            ps1.setString(1, id);
            ps1.execute();
            rs1=ps1.getResultSet();
            if(rs1.next())
            {
                System.out.println("insdide of the catuniqecheck");
                map.put("catgoryname",rs1.getString("catgoryname"));
            }
            return map;
        }   
       else
         {  
               System.out.println("DB connection Established");
               return null  ;
         }
        }
        catch (Exception e) {
            // TODO: handle exception
               return null  ;
        }
finally{
            JDBCHelper.close(rs1);
            JDBCHelper.close(ps1);
            JDBCHelper.close(c);
       }
}

This is my sevlet code

if (uri.contains("/catnamecheck.do")) {
        Project p1=new Project();
        String id=request.getParameter("id");
        System.out.println("inside uniq check id"+id);
        Map<String, String> result =p1.catuniqecheck(id);
        if(result!=null)
        {
            System.out.println("inside success");
            JSONObject json = new JSONObject();
            json.accumulateAll(result);
            System.out.println("inside json"+json.toString());
            response.setContentType("application/json");
            response.getWriter().write(json.toString());
        }
        if(result==null)
        {
            System.out.println("inside filure");
        }
    }

This is my jsp code

<form name="frm" action="createnewcatgoryBean.jsp" onsubmit="return validatenewcat()" method="post">
<table style="padding: 5px 0px 0px 8px;">
<tr>
<th colspan="2">
<div style="width: width:271px; color:red;" id="validate"></div>
</th>
</tr>
<tr>
<th>Category Name<span>:</span></th><td><input id="cat"  onblur="return validatenewcat()" type="text" name="category">
</td>
</tr>
<tr>
<th>Quotations form<span>:</span></th><td><input type="checkbox" name="quotations"></td>
</tr>
<tr>
<th>Agreement form<span>:</span></th><td><input type="checkbox" name="agreement"></td>
</tr>
<tr>
<th>Payment form<span>:</span></th><td><input type="checkbox" name="payment"></td>
</tr>
<tr>
<th>ETI<span>:</span></th><td><input type="checkbox" name="eti"></td>
</tr>
<tr>
<td colspan="2" style="float:right; padding-top:15px">
<input type="submit" value="Submit"  style="width: 60px;">
</td>
</tr>
</table>
</form>

解决方案

You have set form action="createnewcatgoryBean.jsp". If validation is successful, it will submit to this page. To avoid submission, return false from your javascript.

else {
    var url = "catnamecheck.do?id=" + category;
    xmlhttp.open("post", url, true);
    xmlhttp.send(null);
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
                var temp = xmlhttp.responseText;
                obj = JSON.parse(temp);
                if (obj.catgoryname == category) {
                    alert(obj.catgoryname);
                    alert(category);
                    document.getElementById("validate").innerHTML = "!PLz Enter The Unique Category Name";
                    return false;
                }
                if (obj.catgoryname != category) {
                    alert("inside true" + obj.catgoryname);
                    alert("inside true" + category);
                    return true;
                }
            }
        }
    }

    return false; //return false to avoid form submission
}

 
精彩推荐
图片推荐