| | |
How to check 2 functionalities in one servlet likeadding uSer and verifying userin DB
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Nov 2008
Posts: 28
Reputation:
Solved Threads: 0
How to check 2 functionalities in one servlet likeadding uSer and verifying userin DB
0
#1 Nov 19th, 2008
Re: How to check 2 functionalities in one servlet likeadding uSer and verifying userin DB
0
#2 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.....?
'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
•
•
Join Date: Nov 2008
Posts: 28
Reputation:
Solved Threads: 0
Re: How to check 2 functionalities in one servlet likeadding uSer and verifying userin DB
0
#3 Nov 19th, 2008
java Syntax (Toggle Plain Text)
import java.io.*; import java.util.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class Validation extends HttpServlet{ private ServletConfig config; public void init(ServletConfig config) throws ServletException{ this.config=config; // PrintWriter out = response.getWriter(); String connectionURL = "jdbc:mysql://localhost/test"; Connection connection=null; ResultSet rs; String userName=request.getParameter("user"); String password = request.getParameter("pass"); // response.setContentType("text/html"); try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection(connectionURL, "root", "password"); String sql = "insert into user values('" + userName + "','" + password + "')"; Statement s = connection.createStatement(); int x = s.executeUpdate(sql); if(x==1) { System.out.println("<h1> Thanks For the Registration</h1>"); } else { System.out.println("<h1>+Your Registration is failed..Plase try again+</h1>"); } s.close(); } catch(Exception e) { System.out.println("Exception is ;"+e); } } public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException { PrintWriter out = response.getWriter(); String connectionURL = "jdbc:mysql://localhost/test"; Connection connection=null; ResultSet rs; String userName=new String(""); String password=new String(""); response.setContentType("text/html"); try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection(connectionURL, "root", "password"); String sql = "select user,password from user"; Statement s = connection.createStatement(); s.executeQuery (sql); rs = s.getResultSet(); while (rs.next ()){ userName=rs.getString("user"); password=rs.getString("password"); } rs.close (); s.close (); }catch(Exception e){ System.out.println("Exception is ;"+e); } if(userName.equals(request.getParameter("user")) && password.equals(request.getParameter("pass"))){ out.println("<h1>User is Valid</h1>"); } else { out.println("<h1>You are not a Valid User</h1>"); out.println("<a href='myvalidation.jsp'><br>Login again</a>"); } } }
Last edited by Narue; Nov 19th, 2008 at 9:30 am. Reason: added code tags
Re: How to check 2 functionalities in one servlet likeadding uSer and verifying userin DB
0
#4 Nov 19th, 2008
•
•
•
•
while (rs.next ()){
userName=rs.getString("user");
password=rs.getString("password");
}
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:
Java Syntax (Toggle Plain Text)
Vector userList = new Vector(); User foundUser = null; while (rs.next()){ foundUser = new User(); foundUser.setUserName(rs.getString("user"); foundUser.setPassword(rs.getString("password"); userList.add(foundUser); } // iterate over the vector, and perform next tests ... User testUser = (User)userList.getElementAt((location of the element you want to check)); if (testUser.getUserName().equalsIgnoreCase(request.getParameter("user")) && testUser.getPassword().equalsIgnoreCase(request.getParameter("password"))) return true; // end of iteration return false
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)
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
•
•
Join Date: Nov 2008
Posts: 28
Reputation:
Solved Threads: 0
Re: How to check 2 functionalities in one servlet likeadding uSer and verifying userin DB
0
#6 Nov 19th, 2008
•
•
•
•
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:
Java Syntax (Toggle Plain Text)
Vector userList = new Vector(); User foundUser = null; while (rs.next()){ foundUser = new User(); foundUser.setUserName(rs.getString("user"); foundUser.setPassword(rs.getString("password"); userList.add(foundUser); } // iterate over the vector, and perform next tests ... User testUser = (User)userList.getElementAt((location of the element you want to check)); if (testUser.getUserName().equalsIgnoreCase(request.getParameter("user")) && testUser.getPassword().equalsIgnoreCase(request.getParameter("password"))) return true; // end of iteration return false
Re: How to check 2 functionalities in one servlet likeadding uSer and verifying userin DB
0
#7 Nov 19th, 2008
![]() |
Other Threads in the JSP Forum
- Previous Thread: Print pdf or file from jsp page
- Next Thread: JSP and XML
Views: 902 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for JSP
apache array backbutton combobox comma connection csv database development directorystructure dropdownlist dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 mysql netbeans network parameters passing ping printinserverinsteadofclient project read redirect request.getparameter response seperated servlet servletdopost()readxml sessions software sql ssl state_saving_method stocks sun tomcat tutorial update values video web write






