How to check 2 functionalities in one servlet likeadding uSer and verifying userin DB

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 check 2 functionalities in one servlet likeadding uSer and verifying userin DB

 
0
  #1
Nov 19th, 2008
Hi
i want to implement one servlet where first i want add a user to the database and later when he logged again i need to check that whether he is already existed or not?if he is not existed i need to add those details to the database... can any one help me in this.....?
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: How to check 2 functionalities in one servlet likeadding uSer and verifying userin DB

 
0
  #2
Nov 19th, 2008
Originally Posted by mahaboob Basha View Post
Hi
i want to implement one servlet where first i want add a user to the database and later when he logged again i need to check that whether he is already existed or not?if he is not existed i need to add those details to the database... can any one help me in this.....?
help you with what? you haven't done anything yet.
'this' is also not very specific question.

I have no doubt you'll find plenty of examples on the net for what you're trying to do
Reply With Quote Quick reply to this message  
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

Re: How to check 2 functionalities in one servlet likeadding uSer and verifying userin DB

 
0
  #3
Nov 19th, 2008
  1. import java.io.*;
  2. import java.util.*;
  3. import java.sql.*;
  4. import javax.servlet.*;
  5. import javax.servlet.http.*;
  6.  
  7. public class Validation extends HttpServlet{
  8.  
  9. private ServletConfig config;
  10.  
  11. public void init(ServletConfig config)
  12. throws ServletException{
  13. this.config=config;
  14.  
  15. // PrintWriter out = response.getWriter();
  16. String connectionURL = "jdbc:mysql://localhost/test";
  17. Connection connection=null;
  18. ResultSet rs;
  19. String userName=request.getParameter("user");
  20. String password = request.getParameter("pass");
  21.  
  22. // response.setContentType("text/html");
  23. try {
  24. Class.forName("com.mysql.jdbc.Driver");
  25. connection = DriverManager.getConnection(connectionURL, "root", "password");
  26. String sql = "insert into user values('" + userName + "','" + password + "')";
  27. Statement s = connection.createStatement();
  28. int x = s.executeUpdate(sql);
  29. if(x==1)
  30. {
  31. System.out.println("<h1> Thanks For the Registration</h1>");
  32. }
  33. else
  34. {
  35. System.out.println("<h1>+Your Registration is failed..Plase try again+</h1>");
  36. }
  37. s.close();
  38.  
  39. }
  40. catch(Exception e)
  41. {
  42. System.out.println("Exception is ;"+e);
  43. }
  44.  
  45. }
  46.  
  47.  
  48. public void service(HttpServletRequest request, HttpServletResponse response)
  49. throws ServletException,IOException
  50. {
  51.  
  52. PrintWriter out = response.getWriter();
  53. String connectionURL = "jdbc:mysql://localhost/test";
  54. Connection connection=null;
  55. ResultSet rs;
  56. String userName=new String("");
  57. String password=new String("");
  58. response.setContentType("text/html");
  59.  
  60. try {
  61. Class.forName("com.mysql.jdbc.Driver");
  62. connection = DriverManager.getConnection(connectionURL, "root", "password");
  63. String sql = "select user,password from user";
  64. Statement s = connection.createStatement();
  65. s.executeQuery (sql);
  66. rs = s.getResultSet();
  67. while (rs.next ()){
  68. userName=rs.getString("user");
  69. password=rs.getString("password");
  70. }
  71. rs.close ();
  72. s.close ();
  73. }catch(Exception e){
  74. System.out.println("Exception is ;"+e);
  75. }
  76.  
  77. if(userName.equals(request.getParameter("user"))
  78. && password.equals(request.getParameter("pass"))){
  79. out.println("<h1>User is Valid</h1>");
  80.  
  81. }
  82.  
  83. else
  84. {
  85.  
  86. out.println("<h1>You are not a Valid User</h1>");
  87. out.println("<a href='myvalidation.jsp'><br>Login again</a>");
  88. }
  89. }
  90.  
  91. }
Last edited by Narue; Nov 19th, 2008 at 9:30 am. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: How to check 2 functionalities in one servlet likeadding uSer and verifying userin DB

 
0
  #4
Nov 19th, 2008
Originally Posted by mahaboob Basha View Post
while (rs.next ()){
userName=rs.getString("user");
password=rs.getString("password");
}
maybe it would be easier for us if you added what it does and what trouble you get in.
I can be wrong, but I think the above code will cause problems if you have more than 1 users with their passwords in your database.

what you can do, doesn't mean you have to do it this way, is to create a class User, which takes (at least) a username and a password as variables.
this way, you can create a Vector, ArrayList, ... and do the next:
  1. Vector userList = new Vector();
  2. User foundUser = null;
  3. while (rs.next()){
  4. foundUser = new User();
  5. foundUser.setUserName(rs.getString("user");
  6. foundUser.setPassword(rs.getString("password");
  7. userList.add(foundUser);
  8. }
  9. // iterate over the vector, and perform next tests ...
  10. User testUser = (User)userList.getElementAt((location of the element you want to check));
  11. if (testUser.getUserName().equalsIgnoreCase(request.getParameter("user")) && testUser.getPassword().equalsIgnoreCase(request.getParameter("password")))
  12. return true;
  13.  
  14. // end of iteration
  15. return false
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: How to check 2 functionalities in one servlet likeadding uSer and verifying userin DB

 
0
  #5
Nov 19th, 2008
I hate to break it to you mahaboob Basha but this is not the right way to do this thing.
Firstly you need to put DB functionality out of the Servlet.
Secondly Servlets are hardly used for generating GUI. (Use JSP)
Last edited by javaAddict; Nov 19th, 2008 at 6:55 am. Reason: Removed unnecessary comment
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
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

Re: How to check 2 functionalities in one servlet likeadding uSer and verifying userin DB

 
0
  #6
Nov 19th, 2008
Originally Posted by stultuske View Post
maybe it would be easier for us if you added what it does and what trouble you get in.
I can be wrong, but I think the above code will cause problems if you have more than 1 users with their passwords in your database.

what you can do, doesn't mean you have to do it this way, is to create a class User, which takes (at least) a username and a password as variables.
this way, you can create a Vector, ArrayList, ... and do the next:
  1. Vector userList = new Vector();
  2. User foundUser = null;
  3. while (rs.next()){
  4. foundUser = new User();
  5. foundUser.setUserName(rs.getString("user");
  6. foundUser.setPassword(rs.getString("password");
  7. userList.add(foundUser);
  8. }
  9. // iterate over the vector, and perform next tests ...
  10. User testUser = (User)userList.getElementAt((location of the element you want to check));
  11. if (testUser.getUserName().equalsIgnoreCase(request.getParameter("user")) && testUser.getPassword().equalsIgnoreCase(request.getParameter("password")))
  12. return true;
  13.  
  14. // end of iteration
  15. return false
can i use rquest and response objects in init method?.in the first reuest form jsp username and password have to be add to DB .from second time i need to check that whether those username and password are present or not?can u tell me how to execute multiple sql querries in one servlet like insert and select...
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: How to check 2 functionalities in one servlet likeadding uSer and verifying userin DB

 
0
  #7
Nov 19th, 2008
that's the same logic as adding several numbers op to each other. you don't create a new method, and sure not a new class, for every number you want to add, you just set the code in one place.

just execute several queries. how to do that? by coding it that way, it won't do it by itself
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the JSP Forum


Views: 902 | Replies: 6
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC