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

Recommended Answers

All 6 Replies

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

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>");
              }
   } 

  }

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)

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:

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

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:

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.