Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JSP-Servlet Passing Parameter
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Ngeaw Kam Wah
Passing Parameter
2 Answer(s)      4 years and 8 months ago
Posted in : JSP-Servlet


Hi, it'me again. Below is the set of code that I get from your solution entitled droplist that read from database. It do read from database but in the textbox of the webpage, i need it to show something associated with the data i selected. Therefore, i've modify it into something like below which is from the selectbox.jsp. Notice that in the javascript part, there's var answer. I need to pass this parameter back into the jsp part, how should i do it which means the String answer in jsp part will equal to the var answer javascript part?

<%@ page language="java" import="java.util.*"%>
<%@ page import="java.sql.*,java.io.*,javax.servlet.*,javax.servlet.http.*,java.lang.String
"%>
<html>
<head>
<script type="text/javascript">
function change()
{
var answer = document.getElementById('selc').value;

<%

String connectionURL = "jdbc:mysql://localhost:3306/test";
Connection connection=null;
ResultSet rs;
response.setContentType("text/html");
String price = "0";
String answer = "amir";

try {
// Load the database driver
Class.forName("com.mysql.jdbc.Driver");
// Get a Connection to the database
connection = DriverManager.getConnection(connectionURL, "root", "123");
//Select the data from the database
String sql = "select * from message where message = '"+answer+"'";
Statement s = connection.createStatement();
s.executeQuery (sql);
rs = s.getResultSet();

while (rs.next ()){

price = rs.getString("price");
}

rs.close ();
s.close ();
}


catch(Exception e)
{
System.out.println("Exception is ;"+e);
}

%>
document.getElementById('textvalue').value= <%= price %>;
}
</script>
</head>
<body>
<h1>Use of Select Box in JSP</h1>
<table border="1" width="41%" height="53" cellspacing="0" cellpadding="3"
bgcolor="#000080" bordercolorlight="#FFFFFF">
<tr>
<td width="100%" height="18" colspan="2"><b>
<font color="#FF00FF">Select items from select box</font></b></td>
</tr>
<tr>
<td width="17%" height="23">
<select name="ActionSelect" onChange="change()" id="selc" >
<%Iterator itr;%>
<% List data= (List)request.getAttribute("data");
for (itr=data.iterator(); itr.hasNext(); )
{
String value=(String)itr.next();
%>
<option value=<%=value%>><%=value%></option>
<%}%>
</select>
</td>
<td width="83%" height="23"><input type="text" id="textvalue" />
</td>
</tr>
</table>
</body>
</html>

Advertisement
View Answers

April 17, 2010 at 12:24 PM


Hi Friend,

We have modified your code.Check it.

<%@ page language="java" import="java.util.*"%>
<%@ page import="java.sql.*,java.io.*,javax.servlet.*,javax.servlet.http.*,java.lang.String
"%>
<html>
<head>
<script type="text/javascript">
function change(){
var answer = document.getElementById('selc').value;
window.location.replace("mod1.jsp?name="+answer);
}
</script>
<%
String ans=request.getParameter("name");
String connectionURL = "jdbc:mysql://localhost:3306/register";;
Connection connection=null;
ResultSet rs;
response.setContentType("text/html");
String price = "";
String name = "";
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, "root", "root");
String sql = "select * from item where name = '"+ans+"'";
Statement s = connection.createStatement();
s.executeQuery (sql);
rs = s.getResultSet();
while (rs.next ()){
price = rs.getString("price");
name=rs.getString("name");
}
rs.close ();
s.close ();
}
catch(Exception e){
System.out.println("Exception is ;"+e);
}
System.out.println(price);
%>
</head>
<body>
<h1>Use of Select Box in JSP</h1>
<table border="1" width="41%" height="53" cellspacing="0" cellpadding="3"
bgcolor="#000080" bordercolorlight="#FFFFFF">
<tr>
<td width="100%" height="18" colspan="2"><b>
<font color="#FF00FF">Select items from select box</font></b></td>
</tr>
<tr>
<td width="17%" height="23">
<select name="ActionSelect" onChange="change()" id="selc" >
<%
String[] arr={"A","B","C","D","E"};
for(int i=0;i<arr.length;i++){
%>
<option value="<%=arr[i]%>"><%=arr[i]%></option>
<%
if(name.equals(arr[i])){
%>
<option value="<%=arr[i]%>" selected=selected><%=arr[i]%></option>
<%
}
}%>

</select>
</td>
<td width="83%" height="23"><input type="text" id="textvalue" value="<%= price %>" />
</td>
</tr>
</table>
</body>
</html>

Thanks


April 17, 2010 at 12:52 PM


We have performed action on same jsp page.Above jsp page has been saved as mod1.jsp.

thanks


Related Tutorials/Questions & Answers:
Java parameter passing
Java parameter passing  What type of parameter passing does Java support
Passing Parameter - JSP-Servlet
Passing Parameter  I would like to ask how to pass a parameter from javascript section into the jsp section.It will be something like...; answer<%; <---I'm stuck here passing the var
Advertisements
Passing Parameter - JSP-Servlet
Passing Parameter  Hi, it'me again. Below is the set of code that I get from your solution entitled droplist that read from database. It do read from..., there's var answer. I need to pass this parameter back into the jsp part, how should i do
Passing a parameter using URL string
Passing a parameter using URL string  How to pass a parameter using the URL string in a JSF application
passing parameter from html to applet
passing parameter from html to applet  how to pass the following from HTML to applet in Java code - font size,font style,font size?give me suitable code
passing .properties file as a parameter to another function
passing .properties file as a parameter to another function  passing .properties file as a parameter to another function in an jsp file
passing .properties file as a parameter to another function
passing .properties file as a parameter to another function  passing .properties file as a parameter to another function in an jsp file
passing .properties file as a parameter to another function
passing .properties file as a parameter to another function  passing .properties file as a parameter to another function in an jsp file
passing .properties file as a parameter to another function
passing .properties file as a parameter to another function  passing .properties file as a parameter to another function in an jsp file
Applet - Passing Parameter in Java Applet
Applet - Passing Parameter in Java Applet   ... and passing parameter in applet, a example is given below. In this example, we... case applet should display "Welcome in Passing parameter in java applet
JSP parameter passing throught out web app
JSP parameter passing throught out web app  i want to pass one param from page1.jsp page to page4.jsp page but in between this i need to navigate page3.jsp and page2.jsp.Please help me to understand how could i transfer param
Passing Parameter Values to another jsp in Query Strings
Passing Parameter Values to another jsp in Query Strings  HI ALL, I m trying to pass a variable value to another JSP using query string... response.sendRedirect("http://localhost:8080/SWIFT/index.jsp?loginid='"+loginid
passing file parameter through ajax - Ajax
passing file parameter through ajax  I have file parameter in jsp file, i need to pass it to server side through ajax. how i can i do that.   Hi friend, file1.jsp passing file using jsp
JSP:Passing query as parameter - Java Beginners
JSP:Passing query as parameter  Hi Friends, I have a real tough..."; String qry3="select age from table13"; string q=Insert into emp values(?,?,?); int update=prep.executeUpdate(q); prep.setString(1,qry1); prep.setString
JSP:Passing query as parameter - Java Beginners
JSP:Passing query as parameter  Hi Friends, I have a real tough..."; String qry3="select age from table13"; string q=Insert into emp values(?,?,?); int update=prep.executeUpdate(q); prep.setString(1,qry1); prep.setString
Passing parameter over Form to BackingBean - Java Server Faces Questions
Passing parameter over Form to BackingBean  Dear User, i have problems concerning passing parametes from form to my backing bean. I have... here, my Bean Code. This is the form
Parameter passing from jsp page to jsp page - JSP-Servlet
Parameter passing from jsp page to jsp page  Hi I intends to pass the parameters that I received from previous page through query String. I get all parameter in respective string variables. I have to passed the parameters
passing parameter from Java code to CSS file - Design concepts & design patterns
passing parameter from Java code to CSS file  hi, i have to pass a string variable from by java code to a CSS file so that i can use that variable in my CSS file. Plz help me as soon as possible
Passing Parameter with <jsp: include>
Passing Parameter with <jsp: include>      In this example we are going to use <jsp:include> action tag. This action tag has one attribute page which is used to include
Passing variable
Passing variable  How to pass variable to HTTP object in HTMl or PHP
Init Parameter
Init Parameter  How set Init Parameter in servlet
Argument Passing
Passing Argument in Java  hi this is my code class Test { void meth(int i, int j) { i *= 2; j /= 2... to the parameter of the subroutine have no effect on the argument plz clear my
Argument Passing
); print a=30 & b=10 As changes made to the parameter of the subroutine have
Argument Passing
); print a=30 & b=10 As changes made to the parameter of the subroutine have
Argument Passing
); print a=30 & b=10 As changes made to the parameter of the subroutine have
Argument Passing
); print a=30 & b=10 As changes made to the parameter of the subroutine have
Argument Passing
); print a=30 & b=10 As changes made to the parameter of the subroutine have
Argument Passing
); print a=30 & b=10 As changes made to the parameter of the subroutine have
Passing Parameter Using Html Form
Passing Parameter Using Html Form       This is a very simple example in which we are going to display the name on the browser which we have entered from the Html page
Passing Parameters - JSP-Servlet
Passing Parameters  Hi I have to pass parameter like such scenario. Please help me on emergent bases. I will be grateful. First Page First Name /**SECOND PAGER upto this page I passed the parameter through query
validate parameter before using it
validate parameter before using it   How to validate the parameter before using it in JSP
 

 

 

DMCA.com