943,853 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Unsolved
  • Views: 2540
  • JSP RSS
Nov 22nd, 2008
0

how to pass the parameters of jsp from one servlet to another

Expand Post »
Hi i created one jsp form with 2 fields..now in the first servlet i want check whether those 2 fields values are presented in the Database or not..if not then i want move those values to another servlet .. how can i do this?how to get the values (parameter values of jsp in second servlet... here is my code plz check it once.. and send me update ASAP very urgent...
1st Servlet..
java Syntax (Toggle Plain Text)
  1. ____________
  2. package com;
  3. import java.io.*;
  4. import java.util.*;
  5. import java.sql.*;
  6. import javax.servlet.*;
  7. import javax.servlet.http.*;
  8.  
  9. public class SearchServlet extends HttpServlet
  10. {
  11.  
  12. private ServletConfig config;
  13.  
  14. public void init(ServletConfig config)
  15. throws ServletException{
  16. this.config=config;
  17. }
  18. public void service(HttpServletRequest request, HttpServletResponse response)
  19. throws ServletException,IOException{
  20.  
  21. HttpSession ses=request.getSession();
  22. PrintWriter out = response.getWriter();
  23. String connectionURL = "jdbc:mysql://localhost/test";
  24. Connection connection=null;
  25. ResultSet rs;
  26. String mobileno=request.getParameter("mobileno");
  27. String password=request.getParameter("password");
  28. response.setContentType("text/html");
  29.  
  30.  
  31. try {
  32. // Load the database driver
  33. Class.forName("com.mysql.jdbc.Driver");
  34. // Get a Connection to the database
  35. connection = DriverManager.getConnection(connectionURL, "root", "DBpassword");
  36. //Add the data into the database
  37. String sql = "select mobileno,password from newuser";
  38. Statement s = connection.createStatement();
  39. s.executeQuery (sql);
  40. rs = s.getResultSet();
  41. while (rs.next ()){
  42. mobileno=rs.getString("mobileno");
  43. password=rs.getString("password");
  44. }
  45. rs.close ();
  46. s.close ();
  47. }catch(Exception e)
  48. {
  49. System.out.println("Exception is ;"+e);
  50. }
  51.  
  52. if(mobileno.equals(request.getParameter("mobileno"))
  53. && password.equals(request.getParameter("password"))){
  54. request.setAttribute("MOBILENO",mobileno);
  55. request.setAttribute("PASSWORD",password);
  56. mobileno = (String)ses.getAttribute("MOBILENO");
  57. out.print("<h1>You are Already Registerd </h1>");
  58. }
  59.  
  60. else
  61. {
  62.  
  63. // out.println("<h1>You are not a Valid User Please Register</h1>");
  64.  
  65. // response.sendRedirect("/ServletProject/ValidUser");
  66.  
  67.  
  68. request.setAttribute("MOBILENO",mobileno);
  69. request.setAttribute("PASSWORD",password);
  70. response.sendRedirect("LoginServlet");
  71.  
  72. //out.println("<a href='newregister.jsp'><br>Register Here!!</a>");
  73. }
  74. }
  75.  
  76. }
  77.  
  78. 2nd servlet...
  79. ___________
  80.  
  81. package com;
  82. import java.io.*;
  83. import java.util.*;
  84. import java.sql.*;
  85. import javax.servlet.*;
  86. import javax.servlet.http.*;
  87.  
  88. public class LoginServlet extends HttpServlet{
  89.  
  90. private ServletConfig config;
  91.  
  92. public void init(ServletConfig config)
  93. throws ServletException{
  94. this.config=config;
  95. }
  96. public void doGet(HttpServletRequest request, HttpServletResponse response)
  97. throws ServletException,IOException{
  98.  
  99. PrintWriter out = response.getWriter();
  100. String connectionURL = "jdbc:mysql://localhost/test";
  101. Connection connection=null;
  102.  
  103. HttpSession ses=request.getSession();
  104.  
  105. String mobileno = (string)ses.getAttribute(mobileno);
  106. String password = ses.getAttribute(password);
  107.  
  108. response.setContentType("text/html");
  109. try {
  110. // Load the database driver
  111. Class.forName("com.mysql.jdbc.Driver");
  112. // Get a Connection to the database
  113. connection = DriverManager.getConnection(connectionURL, "root", "DBpassword");
  114. //Add the data into the database
  115. String sql = "insert into newuser values(" + mobileno + ",'" + password + "')";
  116. Statement s = connection.createStatement();
  117.  
  118. int x = s.executeUpdate(sql);
  119. if(x==1)
  120. {
  121. out.println("<h1> Thanks For the Registration</h1>");
  122. out.println("<br><h1> Welcome to 4s Technologies</h1></br>");
  123. }
  124. else{
  125. out.println("<h1>+Your Registration is failed..Plase try again+</h1>");
  126. out.println("<a href='newregister.jsp'><br>Register Here!!</a>");
  127. }
  128. s.close();
  129.  
  130. }
  131. catch(Exception e)
  132. {
  133. System.out.println("Exception is ;"+e);
  134. }
  135. }
  136. }
Last edited by ~s.o.s~; Nov 22nd, 2008 at 2:14 am. Reason: Added code tags, learn to use them.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
mahaboob Basha is offline Offline
28 posts
since Nov 2008
Nov 22nd, 2008
-1

Re: how to pass the parameters of jsp from one servlet to another

here is my code plz check it once.. and send me update ASAP very urgent...
  1. Once again you posted in wrong section. In Web Development we have JSP section to which most of your posts been already moved and this one will be hopefully moved soon
  2. Your imperious demand for immediate results is shameful
  3. If you actually bother tried to search around you would find a solution, like this one
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: Separate JSP-Page and Content
Next Thread in JSP Forum Timeline: Reading the mail ids from other websites.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC