943,823 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Unsolved
  • Views: 10979
  • JSP RSS
Nov 24th, 2008
0

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

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.. .ie; in first servlet those values will be comapred with database and bcoz of send redirect in else the values will be stored if they not present in DB here is my code plz check it once..
1st Servlet..
_____________

JSP Syntax (Toggle Plain Text)
  1. package com;
  2. import java.io.*;
  3. import java.util.*;
  4. import java.sql.*;
  5. import javax.servlet.*;
  6. import javax.servlet.http.*;
  7.  
  8. public class SearchServlet extends HttpServlet
  9. {
  10.  
  11. private ServletConfig config;
  12.  
  13. public void init(ServletConfig config)
  14. throws ServletException{
  15. this.config=config;
  16. }
  17. public void service(HttpServletRequest request, HttpServletResponse response)
  18. throws ServletException,IOException{
  19.  
  20. PrintWriter out = response.getWriter();
  21. String connectionURL = "jdbc:mysql://localhost/test";
  22. Connection connection=null;
  23. ResultSet rs;
  24.  
  25. String mobileno=request.getParameter("mno");
  26. String password=request.getParameter("pwd");
  27. response.setContentType("text/html");
  28. HttpSession ses=request.getSession(true);
  29.  
  30. try {
  31. // Load the database driver
  32. Class.forName("com.mysql.jdbc.Driver");
  33. // Get a Connection to the database
  34. connection = DriverManager.getConnection(connectionURL, "root", "password");
  35. //Add the data into the database
  36. String sql = "select mobileno,password from newuser where mobileno='"+ mobileno + "'and password='"+ password + "' " ;
  37.  
  38.  
  39. Statement s = connection.createStatement();
  40. rs=s.executeQuery (sql);
  41. rs = s.getResultSet();
  42.  
  43.  
  44. if(rs.next ())
  45. {
  46. mobileno=rs.getString("mobileno");
  47. password=rs.getString("password");
  48. out.println("MOBILENO=" +mobileno );
  49. out.println("PASSWORD=" +password );
  50.  
  51.  
  52. if(mobileno.equals(request.getParameter("mno")) && password.equals(request.getParameter("pwd")))
  53. {
  54.  
  55. out.print("<h1>You are Already Registerd </h1>");
  56. }
  57. }
  58.  
  59. else
  60. {
  61. request.setAttribute("MOBILENO",mobileno);
  62. request.setAttribute("PASSWORD",password);
  63. RequestDispatcher dispatcher =
  64. getServletConfig ( ) .getServletContext ().getRequestDispatcher("/LoginServlet");
  65. dispatcher.forward (request, response) ;
  66.  
  67. }
  68. rs.close ();
  69. s.close ();
  70.  
  71.  
  72.  
  73. }
  74. catch(Exception e)
  75. {
  76. System.out.println("Exception is ;"+e);
  77. }
  78.  
  79.  
  80. }
  81.  
  82. }

2nd servlet
------------------

JSP Syntax (Toggle Plain Text)
  1. package com;
  2. import java.io.*;
  3. import java.util.*;
  4. import java.sql.*;
  5. import javax.servlet.*;
  6. import javax.servlet.http.*;
  7.  
  8. public class LoginServlet extends HttpServlet{
  9.  
  10. private ServletConfig config;
  11.  
  12. public void init(ServletConfig config)
  13. throws ServletException{
  14. this.config=config;
  15. }
  16. public void doGet(HttpServletRequest request, HttpServletResponse response)
  17. throws ServletException,IOException{
  18.  
  19. PrintWriter out = response.getWriter();
  20. String connectionURL = "jdbc:mysql://localhost/test";
  21. Connection connection=null;
  22. String mno=null;
  23. String pwd=null;
  24. String mobileno = (String)request.getAttribute(MOBILENO);
  25. String password = request.getAttribute(PASSWORD);
  26.  
  27. response.setContentType("text/html");
  28. try {
  29. // Load the database driver
  30. Class.forName("com.mysql.jdbc.Driver");
  31. // Get a Connection to the database
  32. connection = DriverManager.getConnection(connectionURL, "root", "password");
  33. //Add the data into the database
  34. String sql = "insert into newuser values(" + mobileno + ",'" + password + "')";
  35. Statement s = connection.createStatement();
  36.  
  37. HttpSession ses=request.getSession();
  38. int x = s.executeUpdate(sql);
  39. if(x==1)
  40. {
  41. out.println("<h1> Thanks For the Registration</h1>");
  42. out.println("<br><h1> Welcome to 3P Technologies</h1></br>");
  43. }
  44. else{
  45. out.println("<h1>+Your Registration is failed..Plase try again+</h1>");
  46. out.println("<a href='newregister.jsp'><br>Register Here!!</a>");
  47. }
  48. s.close();
  49.  
  50. }
  51. catch(Exception e){
  52. System.out.println("Exception is ;"+e);
  53. }
  54. }
  55. }
Last edited by peter_budo; Nov 24th, 2008 at 6:42 pm. Reason: Removing the [inlinecode] tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
mahaboob Basha is offline Offline
28 posts
since Nov 2008
Nov 24th, 2008
0

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

So where are you encountering a problem ??
We are not Psychics to know what output you are getting on your computer screen.

Tell us the problem in clear words and we will see what is going wrong.

Also in the first servlet:-
java Syntax (Toggle Plain Text)
  1. if(rs.next()) {
  2. mobileno=rs.getString("mobileno");
  3. password=rs.getString("password");
why do you simply want to get the mobile no. and password from the database when you know that the mobile no. and password that you have is correct (cause it has already been validated by your SQL query).
Last edited by stephen84s; Nov 24th, 2008 at 9:55 am.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Nov 26th, 2008
0

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

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.. .ie; in first servlet those values will be comapred with database and bcoz of send redirect in else the values will be stored if they not present in DB here is my code plz check it once..
The very reason that you require to pass user submitted data from one servlet to another reeks of bad design. Seldom would you require two servlets in a single application. Just create a single controller servlet which is responsible for routing requests to proper request handlers.

IMO, what you require are multiple request handlers instead of multiple servlets. Also, you entire business logic is placed in the servlet which is pretty bad. Create two classes called LoginController and SearchController which would be instantiated by the servlet or the request handler.

You are actually spitting out HTML in your servlet which was pretty cool a few years back, but now isn't. Use JSP along with JSTL for presentation purposes. The J2EE 5 guide is a free download. Read and be enlightened.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006

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: Jstl number formatting
Next Thread in JSP Forum Timeline: Book about Struts2





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


Follow us on Twitter


© 2011 DaniWeb® LLC