RSS Forums RSS

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

Please support our JSP advertiser: Programming Forums
Reply
Posts: 28
Reputation: mahaboob Basha is an unknown quantity at this point 
Solved Threads: 0
mahaboob Basha mahaboob Basha is offline Offline
Light Poster

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

  #1  
Nov 22nd, 2008
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..
  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 1:14 am. Reason: Added code tags, learn to use them.
AddThis Social Bookmark Button
Reply With Quote  
Posts: 3,459
Reputation: peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold 
Solved Threads: 412
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

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

  #2  
Nov 22nd, 2008
Originally Posted by mahaboob Basha View Post
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
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 1017 | Replies: 1 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:00 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC