Click here to Skip to main content
15,887,822 members
Home / Discussions / JavaScript
   

JavaScript

 
Questionjson array is not getting the value from jsonOBject. i mentioned the line in index.jsp where json value is not getting Pin
LOKENDRA YADAV23-May-17 1:33
LOKENDRA YADAV23-May-17 1:33 
RantRe: json array is not getting the value from jsonOBject. i mentioned the line in index.jsp where json value is not getting Pin
Richard Deeming23-May-17 1:42
mveRichard Deeming23-May-17 1:42 
GeneralRe: json array is not getting the value from jsonOBject. i mentioned the line in index.jsp where json value is not getting Pin
LOKENDRA YADAV23-May-17 3:59
LOKENDRA YADAV23-May-17 3:59 
RantRe: json array is not getting the value from jsonOBject. i mentioned the line in index.jsp where json value is not getting Pin
Richard Deeming23-May-17 4:12
mveRichard Deeming23-May-17 4:12 
Questionjson object result not showing in jsp?? Pin
LOKENDRA YADAV22-May-17 17:15
LOKENDRA YADAV22-May-17 17:15 
Answeri also tried with this code Pin
LOKENDRA YADAV22-May-17 17:47
LOKENDRA YADAV22-May-17 17:47 
GeneralRe: i also tried with this code Pin
Richard MacCutchan22-May-17 21:57
mveRichard MacCutchan22-May-17 21:57 
Generaljson array is not getting the value from jsonOBject. i mentioned the line in index.jsp where json value is not getting Pin
LOKENDRA YADAV23-May-17 1:14
LOKENDRA YADAV23-May-17 1:14 
-------------------------------------index.jsp-----------------------------------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<script type="text/javascript">

var req;
function initReq(){

if(window.XMLHttpRequest){
req=new XMLHttpRequest;


}else if(window.ActiveXObject){
req=new ActiveXObject("Microsoft.XMLHTTP");

}

}//initReq

function verifyUname(){

initReq();

var sid=document.getElementById("uname").value;
var url="checkunm.jlc?uname="+escape(uname);
req.open("GET",url);
req.onreadystatechange=processResponse;
req.send();

}


function processResponse(){
var st=req.readyState;
var resSt=req.status;

if(st==4 && resSt==200){

displayResult();
}

}//processResponse

function displayResult(){
document.getElementById("error").innerHTML="";
document.getElementById("result").innerHTML="";
var array=req.responseText;

var data=eval('('+array+')');

if(data.length==0){

document.getElementById("error").innerHTML="<font color='red' size='5'>no student found</font>";


}//if
else
{
for(var i=0;i<data.length;i++){

var jsob=data[i]; <<<<<<<<<<<<<< ----here value is not coming it is getting 0 each time input----------------
document.write(jsob.msg);
}

}
}

</script>




<body>
<form action="register.jlc" method="post">
<div id="result"></div>
<div id="error"></div>
<table>
<tr>
<td align="center" colspan="2">REGISTER HERE</td>
</tr>

<tr>

<td>
ENTER USERNAME
</td>

<td>
<input type="text" name="uname" id="uname" onkeyup="verifyUname()"/>
</td>

</tr>

<tr>
<td>
ENTER PASSWORD
</td>

<td>
<input type="password" name="password" id="password" onkeyup="verifyUname()"/>
</td>

</tr>

<tr>
<td align="center" colspan="2">
<input type="submit" value="REGISTER"/>
</td>
</tr>
</table>
</form>
</body>
</html>
------------------------------CheckUnameServlet-------------------------------------------------

package com.jlcindia.ajax;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.JSONArray;

import java.io.IOException;
import java.util.*;
public class CheckUnameServlet extends HttpServlet {

/**
*
*/
private static final long serialVersionUID = 1L;
ArrayList<String> unames=new ArrayList<String>();

public void init(ServletConfig config) throws ServletException{
unames.add("sri");
unames.add("lucky");
unames.add("piyush");
unames.add("ujjawal");
super.init(config);
}

protected void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
String uname=req.getParameter("uname");
String msg="";
JSONArray array=new JSONArray();

if(uname!=null && uname.trim().length()>0){
JSONObject jobj=new JSONObject();

if(unames.contains(uname)){

try {
jobj.put(msg); <<<<<<<< -------------// here may be am doing wrong --------------

} catch (JSONException e) {

e.printStackTrace();
}
array.add(jobj);// <<<<<<<<< -------------// here may be am doing wrong --------------
System.out.println(array);
}

}


System.out.println(array.toJSONString());

res.setContentType("application/json");

res.getWriter().write(array.toString());
}
}

-----------------------web.xml------------------------

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Test2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<servlet>
<servlet-name>cuServlet</servlet-name>
<servlet-class>com.jlcindia.ajax.CheckUnameServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cuServlet</servlet-name>
<url-pattern>/checkunm.jlc</url-pattern>

</servlet-mapping>
</web-app>

-- modified 23-May-17 7:24am.
GeneralRe: i also tried with this code Pin
LOKENDRA YADAV23-May-17 1:36
LOKENDRA YADAV23-May-17 1:36 
GeneralRe: i also tried with this code Pin
Richard MacCutchan23-May-17 3:10
mveRichard MacCutchan23-May-17 3:10 
AnswerRe: json object result not showing in jsp?? Pin
Nathan Minier23-May-17 1:00
professionalNathan Minier23-May-17 1:00 
GeneralRe: json object result not showing in jsp?? Pin
LOKENDRA YADAV23-May-17 1:31
LOKENDRA YADAV23-May-17 1:31 
QuestionMerging 2 different JSON objects Pin
Farhad Eft21-May-17 21:43
Farhad Eft21-May-17 21:43 
AnswerRe: Merging 2 different JSON objects Pin
Richard MacCutchan21-May-17 22:51
mveRichard MacCutchan21-May-17 22:51 
QuestionAccording to you, the output of the console output will look like? Pin
Member 1320782917-May-17 21:25
Member 1320782917-May-17 21:25 
AnswerRe: According to you, the output of the console output will look like? Pin
Richard MacCutchan17-May-17 21:33
mveRichard MacCutchan17-May-17 21:33 
GeneralRe: According to you, the output of the console output will look like? Pin
Member 1320782917-May-17 22:09
Member 1320782917-May-17 22:09 
AnswerRe: According to you, the output of the console output will look like? Pin
Afzaal Ahmad Zeeshan17-May-17 21:48
professionalAfzaal Ahmad Zeeshan17-May-17 21:48 
GeneralRe: According to you, the output of the console output will look like? Pin
Member 1320782917-May-17 22:09
Member 1320782917-May-17 22:09 
QuestionHow can I write this button Pin
庭喵15-May-17 22:54
庭喵15-May-17 22:54 
QuestionWhen someone enters a wrong answer in an input text box in JavaScript I want the user to be placed in the same field instead of going to the next Pin
Member 1319885813-May-17 20:22
Member 1319885813-May-17 20:22 
AnswerRe: When someone enters a wrong answer in an input text box in JavaScript I want the user to be placed in the same field instead of going to the next Pin
Richard Deeming15-May-17 8:08
mveRichard Deeming15-May-17 8:08 
QuestionJSON not working in IE 6 Pin
AntoVasanth10-May-17 22:56
AntoVasanth10-May-17 22:56 
AnswerRe: JSON not working in IE 6 Pin
Richard Deeming11-May-17 1:24
mveRichard Deeming11-May-17 1:24 
QuestionJSON retrieved from server works mostly like string, not element Pin
Member 1103130410-May-17 20:36
Member 1103130410-May-17 20:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.