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... here is my code plz check it once.. and send me update ASAP very urgent...
1st Servlet..

____________
package com;
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SearchServlet extends HttpServlet
{

 private ServletConfig config;
  
  public void init(ServletConfig config)
    throws ServletException{
     this.config=config;
     }
  public void service(HttpServletRequest request, HttpServletResponse response) 
              throws ServletException,IOException{
      
    HttpSession ses=request.getSession();
     PrintWriter out = response.getWriter();
    String connectionURL = "jdbc:mysql://localhost/test";
    Connection connection=null;
    ResultSet rs;
    String mobileno=request.getParameter("mobileno");
    String password=request.getParameter("password");
    response.setContentType("text/html");
    
 
    try {
       // Load the database driver
      Class.forName("com.mysql.jdbc.Driver");
      // Get a Connection to the database
      connection = DriverManager.getConnection(connectionURL, "root", "DBpassword"); 
      //Add the data into the database
      String sql = "select mobileno,password from newuser";
      Statement s = connection.createStatement();
      s.executeQuery (sql);
      rs = s.getResultSet();
      while (rs.next ()){
        mobileno=rs.getString("mobileno");
        password=rs.getString("password");
      }
      rs.close ();
      s.close ();
      }catch(Exception e)
       {
      System.out.println("Exception is ;"+e);
      }
    
     if(mobileno.equals(request.getParameter("mobileno")) 
          && password.equals(request.getParameter("password"))){
        request.setAttribute("MOBILENO",mobileno);
        request.setAttribute("PASSWORD",password);
        mobileno = (String)ses.getAttribute("MOBILENO");
        out.print("<h1>You are Already Registerd </h1>");
  }
 
             else 
             {
          
          //    out.println("<h1>You are not a Valid User Please Register</h1>");
            
          //       response.sendRedirect("/ServletProject/ValidUser");

            
        request.setAttribute("MOBILENO",mobileno);
        request.setAttribute("PASSWORD",password);
         response.sendRedirect("LoginServlet");
                            
           //out.println("<a href='newregister.jsp'><br>Register Here!!</a>");
         }
   } 

  }

 2nd servlet...
___________

package com;
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class LoginServlet extends HttpServlet{

  private ServletConfig config;
  
  public void init(ServletConfig config)
    throws ServletException{
     this.config=config;
     }
  public void doGet(HttpServletRequest request, HttpServletResponse response) 
              throws ServletException,IOException{
      
    PrintWriter out = response.getWriter();
    String connectionURL = "jdbc:mysql://localhost/test";
    Connection connection=null;
    
      HttpSession ses=request.getSession();

    String mobileno = (string)ses.getAttribute(mobileno); 
     String password = ses.getAttribute(password); 

    response.setContentType("text/html");
    try {
       // Load the database driver
      Class.forName("com.mysql.jdbc.Driver");
      // Get a Connection to the database
      connection = DriverManager.getConnection(connectionURL, "root", "DBpassword"); 
      //Add the data into the database
      String sql = "insert into  newuser values(" + mobileno  + ",'" + password  + "')";
      Statement s = connection.createStatement();
              
          int x = s.executeUpdate(sql);
        if(x==1)
        {
                out.println("<h1> Thanks For the Registration</h1>"); 
        out.println("<br><h1> Welcome to 4s Technologies</h1></br>"); 
        }
        else{
        out.println("<h1>+Your Registration is failed..Plase try again+</h1>");  
        out.println("<a href='newregister.jsp'><br>Register Here!!</a>");     
         } 
     s.close();
  
      }
      catch(Exception e)
    {
      System.out.println("Exception is ;"+e);
      }
    }  
}

Dani AI

Generated

Notes for : core problems in the posted code are (1) request attributes are lost when using response.sendRedirect, (2) the SELECT logic reads all rows and overwrites variables instead of checking for a matching row, and (3) the second servlet uses incorrect attribute retrieval/casing and has syntax errors (use String, pass the attribute name as a literal). was correct to note the forum placement, but the code needs concrete fixes.

Server-side forward (preserves request attributes)

request.setAttribute("mobileno", mobileInput);
RequestDispatcher rd = request.getRequestDispatcher("/LoginServlet");
rd.forward(request, response);

Then in LoginServlet:

String mobileno = (String) request.getAttribute("mobileno");

Client redirect (new request) — two safe options

  • Session: store values before redirect and remove them after use:
    HttpSession session = request.getSession();
    session.setAttribute("mobileno", mobileInput);
    response.sendRedirect("LoginServlet");
    ...
    String mobileno = (String) session.getAttribute("mobileno");
    session.removeAttribute("mobileno");
  • Query string: use URLEncoder.encode(...) if absolutely necessary, but never pass raw passwords in URLs.

Database & flow fixes

  • Query by mobile (don’t loop all rows): use PreparedStatement to avoid SQL injection and to check existence:
    PreparedStatement pst = conn.prepareStatement(
    "SELECT password FROM newuser WHERE mobileno = ?");
    pst.setString(1, mobileInput);
    ResultSet rs = pst.executeQuery();
    if (rs.next()) { /* compare password safely */ } else { /* forward/redirect to register */ }
  • Insert with explicit columns:
    PreparedStatement ps = conn.prepareStatement(
    "INSERT INTO newuser (mobileno, password) VALUES (?, ?)");

Quick checklist: implement doPost/doGet to match form method, always check for nulls before calling equals to avoid NPEs, close JDBC resources (try-with-resources), never store plain-text passwords (use hashing) and avoid sending credentials in URLs. These changes will fix parameter passing and make the flow reliable and secure.

here is my code plz check it once.. and send me update ASAP very urgent...

  1. Once again you posted in wrong section. In Web Development we have JSP section to which most of your posts been already moved and this one will be hopefully moved soon
  2. Your imperious demand for immediate results is shameful
  3. If you actually bother tried to search around you would find a solution, like this one
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.