|
|
| struts(DWR) |
Expert:dileep
i want to pass a combo box value from my jsp page to servlet . where i had to done this by using DWR.. It is getting null value into servlet.
i.e, i am giving my sample code here! plz help me ,
my.jsp::::: jsp page
<html> <head> <script type="text/javascript"> var xhr;
function modifyPage() {
try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); //alert("Msxml2.XMLHTTP ActiveXObject created"); } catch (e) { try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); //alert("Microsoft.XMLHTTP ActiveXObject created"); } catch (E) { xhr = false; } }
if (!xhr && typeof XMLHttpRequest != 'undefined') { xhr = new XMLHttpRequest(); //alert("XMLHttpRequest Object created"); }
xhr.open("GET", "../message", "true"); //alert("Open method called"); xhr.setRequestHeader("User-Agent", "my browser"); //alert("Request header setted");
//here is the actual problem
var queryString ="name=value&anothername=othervalue&so=on"; xhr.send(queryString);
// the values of name and others are getting as "null" in servlet
xhr.onreadystatechange=function() { //alert("State : " + xhr.readyState + "" ); if (xhr.readyState != 4) return; if(xhr.status==200) { alert("The document will change now" ); var r=xhr.responseText; t=r.split(','); //alert(t); //document.getElementById("message").innerHTML = xhr.responseText; //some work on retrieved message. it able to retrieve message here. }//if else { document.getElementById("items").innerHTML = "ur browser doestnot support ajax"; } }
xhr.send(null);
} </script> </head> <body> <form name="form1"> <b>Classes</b> <input type="text" name="item1" size="45"> <br> <select name="classes" Id="classes" onChange="modifyPage();"> <option>we</option> <option>are</option> <option>here</option> <option>fru</option> </select> <div ID="items"> <input type="hidden" id="add"/> </div> <input type="button" value="create"/> </form> </body> </html>
myservlet.java:
import java.io.*; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.servlet.*; import javax.servlet.http.*;
public class MessageServlet extends HttpServlet {
private ServletContext context; private String sub_classes=null;
public void init(ServletConfig config) throws ServletException { this.context = config.getServletContext();
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/plain"); response.setHeader("Cache-Control", "no-cache");
String DriverName = "com.mysql.jdbc.Driver"; String ConnectionURL = "jdbc:mysql://localhost:3306/test"; String UserName = "root"; String Password = "root";
//actual probleh is here String s=(String)request.getParameter("name"); System.out.println("here see me "+s); //the above string "s" is printing as null on console of server. // how to get the value of the name or other variables.here
String Classes_Sale_Display="SELECT * FROM customers where username='"+username+"'"; PreparedStatement pstmt = null;
try { Class.forName(DriverName); Connection con = DriverManager.getConnection(ConnectionURL,UserName,Password); pstmt = con.prepareStatement(Classes_Sale_Display); ResultSet rs = pstmt.executeQuery ();
while (rs.next()) { //price=0; sub_classes =rs.getString("availTraining"); System.out.println(sub_classes); } response.getWriter().write(sub_classes);
}//try catch (Exception e) { System.out.println(e); }
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/plain"); response.setHeader("Cache-Control", "no-cache"); response.getWriter().write(sub_classes);
} }
I am able to pass the control to the servlet through the above code , my only problem here is to get the values from jsp to servlet using xhr.send(); please correct the code and help me
i have been working on it for the last 3 days,
thanks in advance.
|
| Answers |
| More Questions |
|
|
Post Answers
Ask Question
Facing Programming Problem?
|
|
|
|
|