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

Reply

Join Date: Nov 2008
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 servlet

 
0
  #1
Nov 24th, 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.. .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..
_____________

  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
------------------

  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

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

 
0
  #2
Nov 24th, 2008
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:-
  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.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,653
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

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

 
0
  #3
Nov 26th, 2008
Originally Posted by mahaboob Basha View 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..
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.
The romantic image of an über-programmer is someone who fires up Emacs, types like a machine gun, and delivers a flawless final product from scratch. A more accurate image would be someone who stares quietly into space for a few minutes and then says “Hmm. I think I’ve seen something like this before.” - John D
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3557 | Replies: 2
Thread Tools Search this Thread



Tag cloud for JSP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC