how to get value in combo for updation?



how to get value in combo for updation?..

Answer / raj

1. DataServlet.java
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DataServlet extends HttpServlet{

private ServletConfig config;
//Setting JSP page
String page="selectbox.jsp";

public void init(ServletConfig config)
throws ServletException{
this.config=config;
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

PrintWriter out = response.getWriter();
//Establish connection to MySQL database
String connectionURL =
"jdbc:mysql://192.168.10.59/messagepaging";
Connection connection=null;
ResultSet rs;
response.setContentType("text/html");
List dataList=new ArrayList();
try {
// Load the database driver
Class.forName("com.mysql.jdbc.Driver");
// Get a Connection to the database
connection =
DriverManager.getConnection(connectionURL, "root", "root");
//Select the data from the database
String sql = "select message from message";
Statement s = connection.createStatement();
s.executeQuery (sql);
rs = s.getResultSet();
while (rs.next ()){
//Add records into data list
dataList.add(rs.getString("message"));
}
rs.close ();
s.close ();
}catch(Exception e){
System.out.println("Exception is ;"+e);
}
request.setAttribute("data",dataList);
//Disptching request
RequestDispatcher dispatcher =
request.getRequestDispatcher(page);
if (dispatcher != null){
dispatcher.forward(request, response);
}
}
}

JSP page "selectbox.jsp" is getting data in following way
<% List data= (List) request.getAttribute("data");

Full code of JSP page is given as below:

2. selectbox.jsp
<%@ page language="java" import="java.util.*"%>
<html>
<head>
<script type="text/javascript">
function change()
{
var answer = document.getElementById('selc').value;
document.getElementById('textvalue').value=answer;
}
</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>

For servlet to run and execute we have to do servlet entry
into "web.xml"
<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app>
<display-name>Welcome to Tomcat</display-name>
<description>Welcome to Tomcat</description>
<servlet>
<servlet-name>DataServlet</servlet-name>
<servlet-class>DataServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DataServlet</servlet-name>
<url-pattern>/DataServlet</url-pattern>
</servlet-mapping>
</web-app>

Is This Answer Correct ?    6 Yes 2 No

Post New Answer

More Advanced Java Interview Questions

what is difference between checked exception & unchecked exception in java?

3 Answers   Ness Technologies,


what is activation monitor and what is its job?

1 Answers  


Why is main purpose of XML?

5 Answers  


In RMI, inorder to sent the stub reference to the client, is we have to load the server object first into the memory or can we directly sent reference to the client?

0 Answers  


how the action can be map from jsp page to bean class in mvc1

1 Answers   Photon,






which book is better for jdbc ,servlets and jsp

0 Answers   TCS,


hi, All are saying that now software field position is not good and in 5-10yrs total software field ll be saturated . is it true? i am unable decide ii continue in this field or not please tell me?

19 Answers  


Explain bind(), rebind(), unbind() and lookup() methods?

1 Answers  


What are the JSP implicit objects ?

1 Answers   eMids,


i have a 1000 objects in data base i need to display those in jsp's how can i retrive those objects in jsp. (consider the performance issue)

2 Answers  


If i learn Java, what kind of applications can i create that will help Banking, Retail, Hotel, Logistics industry.

2 Answers  


What happens when we invoke a thread?s interrupt method while it is in sleeping or waiting condition?

1 Answers  


Categories