autocomplete list in input text box is not coming - javascript

So i am having problem on auto completion feature of jquery. My requirement is to show Json data in auto completion . Json data is coming from java class in jsp page .This code is working fine but nothing is coming in text box as i type something.
This is my jsp page
<%#page import="com.practise.autoComplete.AutoComplete"%>
<%#page import="net.sf.json.JSONObject"%>
<%#page import="net.sf.json.JSON"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# page import="java.util.*"%>
<%
AutoComplete autoComplete=new AutoComplete();
JSONObject jsonObject=autoComplete.autoComp();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<script src="./JS/jquery-1.4.2.min.js"></script>
<script src="/JS/jquery.autocomplete.js"></script>
<script>
function lookup(inputString) {
alert("inside lookup");
if (inputString.length == 0) {
$('#suggestions').hide();
} else {
var object=<%=jsonObject%>
var data= JSON.stringify(object);
if (data.length > 0) {
$("#inputauto").autocomplete({
source: data
});
}
}
}
</script>
<head>
<body>
<div>
<form>
<div>
<br /> Enter Name to see autocomplete <input
type="text" size="30" value="" id="inputauto"
onkeyup="lookup(this.value);" />
</div>
</form>
</div>
</body>
</html>
This is my java file
package com.practise.autoComplete;
import net.sf.json.JSONObject;
public class AutoComplete {
public JSONObject autoComp() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("1", "peter");
jsonObject.put("2", "nadia");
jsonObject.put("3", "jack");
jsonObject.put("4", "areena");
return jsonObject;
}
}

Related

retrive two values in separate textboxes from database using dropdown onchange event jsp without using table index

Following code will show only one value in a text box but what i need is if i select one drop down option it has to display all it's row values in each separate text boxes..retrive two or more values in separate textboxes from database using dropdown onchange event jsp without using table index.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#page import = "java.sql.*" %>
<%#page import = "java.sql.DriverManager.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
function populateCustomerId(){
var selectBox = document.getElementById('selectBox');
var selectedCustomerId = selectBox.options[selectBox.selectedIndex].value;
document.getElementById('customerId').value = selectedCustomerId;
}</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Select</title>
</head>
<body>
<%
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/ravi","root","root");
Statement st = con.createStatement();
ResultSet rs = null;
rs = st.executeQuery("select rid, rname, rmbl from r");
%>
<select id="selectBox" onchange="populateCustomerId();">
<%while(rs.next()){ %>
<option value="<%=rs.getString(2) %>"><%=rs.getString(1) %></option>
<%} %>
<%}
catch (Exception e){
e.printStackTrace();
} %>
</select>
<input id="customerId" type="text" value="" />
<input id="" type="text" value="" />
<input id="" type="text" value="" />
</td>
</body>
</html>
Simple pass data to your element's data attribute as below
<option value="<%=rs.getString(2) %>" data-name="<%=rs.getString(3)%>" data-xyz="value-which-you-want-to-get-in-change-event"><%=rs.getString(1) %></option>
Now you can get that value in your change event function
function populateCustomerId(){
var selectBox = document.getElementById('selectBox');
var selectedCustomerId = selectBox.options[selectBox.selectedIndex].value;
var xyz = selectBox.options[selectBox.selectedIndex].getAttribute('data-xyz');
var name = selectBox.options[selectBox.selectedIndex].getAttribute('data-name');
document.getElementById('customerId').value = selectedCustomerId;
document.getElementById('customerName').value = name;
document.getElementById('customerXyz').value = xyz;
}
This is just simple example how you can do it.
You can improve it as per your need.

How to get the result of the confirm popup in to a variable in spring MVC?

This is my jsp page
<%# 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>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script>
function getChoice(){
var k=confirm("Do you want me to overwrite the existing file ?");
return k;
}
</script>
</head>
<body style="background-color:powderblue;color:Tomato">
<center><form method="POST" action="saveagain">
<h3>${message} </h3>
<br> <br>
<input type="hidden" name="fname" value="${filename}">
<input type="hidden" name="fcontent" value="${filecontent}"> <br><br>
<input type="button" value="Overwrite ?" onclick="choice=getChoice()"/>
<input type="submit" value="Submit ?"></form></center>
<br>
<br>
<center><b>${m}</b></center>
<br><br>
<center>Click me to upload a file <br><br>
</center>
</body>
</html>
In the above code, i want to get the result of confirm popup in to a variable "choice". How to do that ?
Modified code :
bean class :
package com.fss;
public class Choice {
private String choice;
private String fname;
private String fcontent;
public String getChoice() {
return choice;
}
public void setChoice(String choice) {
this.choice = choice;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getFcontent() {
return fcontent;
}
public void setFcontent(String fcontent) {
this.fcontent = fcontent;
}
}
this is my jsp page :
<html>
<head>
<script>
function getUserChoice(){
var k=confirm("Do you want me to overwrite the existing file ?");
$('#choice').val(k);
return k;
}
</script>
</head>
<body>
<center><h3>${message} </h3></center>
<br>
<center><form method="POST" action="saveagain">
<input type="hidden" name="fname" value="${filename}">
<input type="hidden" name="fcontent" value="${filecontent}">
<input type="hidden" name="choice" id="choice"> <br><br>
<input type="submit" value="Overwrite ?" onclick="getUserChoice()"/></form></center>
<center>Click me to upload a file </center>
</body>
</html>
This is my Controller class:
#RequestMapping(value="/saveagain",method = RequestMethod.POST)
public ModelAndView fileUploading2(#ModelAttribute("ch") Choice ch) throws IOException{
String ch1;
String message="";
ch1=ch.getChoice();
System.out.println("popup result "+ch1);
return new ModelAndView("fileUpload","message",message);
}
fname and fcontent values are reflecting in controller. why is choice value not reflecting in "ch1" ?
With your approach, you can use javascript in the getChoice() function to put the return value(true/false) as an hidden element value and retrieve on the server side
function getChoice() {
var k=confirm("Do you want me to overwrite the existing file ?");
$('#hidden_choice').val(k);
}
<!-- HTML element -->
<input type="hidden" name="choice" id="hidden_choice" />
<input id="button_id" type="button" value="Overwrite ?" onclick="getChoice()"/>
On the server side, you can use #RequestParam to retrieve the value. Following is the Example code:
#RequestMapping(value="/saveagain",method = RequestMethod.POST)
public ModelAndView fileUploading2(#RequestParam("choice") Boolean ch) throws IOException {
// do your thing with choice variable here
System.out.println("Choice is: " + ch);
}
If the boolean value does not work, try with String and convert it to Boolean value manually.
You can create a variable such as cofirmstatus in the form.
<input type="hidden" name="cofirmstatus" id="cofirmstatus">
And set the value as below
function getChoice(){
var k=confirm("Do you want me to overwrite the existing file ?");
$('#cofirmstatus').val(k);
return k;
}
So, once the form is submitted, this value will be available on the server side
EDIT:
I created the JSP as follows. No changes required on server side.
<%# 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>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
function submitForm(){
var k=confirm("Do you want me to overwrite the existing file ?");
document.myform.choice.value=k;
document.myform.submit();
}
</script>
</head>
<body style="background-color:powderblue;color:Tomato">
<center><form method="POST" action="saveagain" name="myform">
<h3>${message} </h3>
<br> <br>
<input type="hidden" name="fname" value="${filename}">
<input type="hidden" name="fcontent" value="${filecontent}"> <br><br>
<input type="hidden" name="choice" id="choice"> <br><br>
<input type="button" value="Overwrite ?" onclick="javascript:submitForm()"/>
<input type="submit" value="Submit ?"></form></center>
<br>
<br>
<center><b>${m}</b></center>
<br><br>
<center>Click me to upload a file <br><br>
</center>
</body>
</html>
Output on server for both the actions:
popup result true
popup result false

Not getting value in <c:set var from scriptlet variable

I am defining a variable in scriptlet to get value from request headers, then we are using that variable c:set to set to variable to use in c:out .But i am not getting any value c:set variable, as a result c:out is giving result as ''.
please find the code snippet below and guide me if i am missing anything.
Code in jsp:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>DM</title>
<SCRIPT Language="JavaScript">
function submitform()
{
this.LoginForm.submit();
}
</SCRIPT>
</head>
<body onload="submitform()">
<%
String userName= request.getHeader("IV-USER");
System.out.println("name "+userName);
if (userName == null || userName.length()==0){
response.setHeader("IV-USER",userName);
response.sendRedirect("ULM.jsp");
}
%>
<c:set var="uName" value="<%=userName%>"/>
<p>Welcome1 ${uName}</p>
<form name="LoginForm" action="/ICDDMContent/STGDM.html" method="post">
<input type="hidden" name="UserId" value='<c:out value="${uName}"/>'/>
<!--
<input type="hidden" name="UserId" value="<%=userName%>" >
-->
<input type="hidden" name="Token" value="dummy">
<p>Welcome1 <c:out value="${uName}"/></p>
</form>
</body>
</html>
String userName= request.getHeader("IV-USER");
System.out.println("name "+userName);
if (userName == null || userName.length()==0){
response.setHeader("IV-USER",userName);
response.sendRedirect("ULM.jsp");
}
Is redundant piece of code, if userName is empty, you will just assign empty value to IV-USER header, if "ULM.jsp" is the same page as you've attached, and that redirect is just to set variable - here is your problem

Only File is uploading to database, all other fields are NULL

So as the title says, I'm inserting information in mysql database but only the file that I'm uploading is inserted into database. Everything else is NULL. I suspect it has something to do with the creation of temporary directory on the server to save the file but then again I can't find the solution.
Here is the table I'm inserting into
CREATE TABLE DOC(IDD INT NOT NULL AUTO_INCREMENT, DOCN VARCHAR(50), AUTHOR VARCHAR(50), CAT VARCHAR(50),CONTENT MEDIUMBLOB NOT NULL, CRITN INT DEFAULT 0, PRIMARY KEY(IDD));
Here is the html form I'm fiiling
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<form enctype="multipart/form-data" action="uploadfile.jsp" method="post" onsubmit="return verify()">
<table border='1'>
<tr>
<span style="color:black">Τίτλος</span> <input type="text" name="title"><p></p>
<span style="color:black">Συγγραφέας</span> <input type="text" name="author"><p></p>
<span style="color:black">Κατηγορία</span> <input type="text" name="cat"><p></p>
<td>
Επιλέξτε το άρθρο που θέλετε.
</td>
</tr>
<tr>
<td>
<input type="file" name="filename" id="filename"accept="application/pdf"/>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Upload" />
</td>
</tr>
</table>
</form>
</body>
</html>
And here's the code for the jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#page import="java.io.InputStream"%>
<%#page import="conPackage.MyConnection"%>
<%#page import="java.sql.PreparedStatement"%>
<%#page import="java.io.FileInputStream"%>
<%#page import="java.util.Enumeration"%>
<%#page import="com.oreilly.servlet.MultipartRequest"%>
<%#page import="java.io.File"%>
<%# page import="java.util.Properties.*" %>
<%# page import="java.security.MessageDigest;"%>
<%# page import="java.util.*"%>
<%#page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String rtempfile = File.createTempFile("temp","1").getParent();
MultipartRequest multi = new MultipartRequest(request,rtempfile, 15*1024*1024);
Enumeration files = multi.getFileNames();
String docn =request.getParameter("title");
String author = request.getParameter("author");
String cat= request.getParameter("cat");
String st="insert into doc(docn, author, cat, content) values (?,?,?,?)";
PreparedStatement psmt=MyConnection.getConnection().prepareStatement(st);
String name="";
String fileExtesion="";
File ff =null;
FileInputStream fin =null;
while (files.hasMoreElements())
{
name=(String)files.nextElement();
ff = multi.getFile(name);
fileExtesion = ff.getName().substring(ff.getName().lastIndexOf("."));
// check user has select the correct file or not
boolean fileAllowed = fileExtesion.equalsIgnoreCase(".pdf");
if((ff!=null)&&fileAllowed)
{
try
{
fin=new FileInputStream(ff);
psmt.setString(1, docn);
psmt.setString(2, author);
psmt.setString(3, cat);
psmt.setBinaryStream(4,(InputStream)fin, (int)(ff.length()));
boolean sss = psmt.execute();
out.print("uploaded successfully..");
out.print("<br/> Go to <a href='downloadfile.jsp'>Download</a> page");
}
catch(Exception e)
{
out.print("Failed due to " + e);
}
finally
{
fin.close();
ff.delete();
}
}
else
{
out.print("Please select the correct file...");
}// end of if and else
}// end of while
MyConnection.CloseConnection(); // close the connection
%>
</body>
</html>
When using enctype multipart/form-data you have to use request.getPart for the regular parameters too, as request.getParameter always returns null with multipart/form-data. The other option for processing file uploads is the Apache Commons File Upload library, but in that case as well, you have to get the regular parameters wit the same method as you get the file.

Call JavaScript in Pageload in VS2005

<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<script language="JavaScript" type="text/JavaScript">
CheckBrow();
</script>
function CheckBrow()
{
if((navigator.appName == "Microsoft Internet Explorer") ||(navigator.appName == "Netscape"))
{
HhdnBrowsertype.value=0;
}
else
{
alert("please open the application in IE or Fire fox browser")
HhdnBrowsertype.value=1;
}
}
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:HiddenField ID="HhdnBrowsertype" runat="server" />
</div>
</form>
</body>
</html>
Here is my JavaScript function. Now I need to execute this function on page load.
Here I will check the browser type based on the hidden field value 0 or 1.
I will check this hidden field value on page load
protected void Page_Load(object sender, EventArgs e)
{
// here i need to call my javscript function
// can any one tell me the syntax
If(HhdnBrowsertype.Value==”1”)
{
// here go my page load function
}
}
Can anyone tell me how I can call this JavaScript function on page load? I am using VS 2005.
try this
<body onload="CheckBrow()">
or you can use.
Page.ClientScript.RegisterStartupScript(typeof(string), "CheckBrow", "CheckBrow();", true);

Categories

Resources