title: passing form values submitted in a servlet to a java class and hence inserting into database
body: <p>The form values are submitted to the servlet and I need to retrieve those values into a class "RegisterDetails" and hence insert into the database "userdetails".. I used session to store the values and later retrieve it in the class file. Its a failure. How am I supposed to bring those values into the class file??? Later I need to pass the values into a jsp as well. Please instruct me. If there's any mistakes please help me...</p>
  
  <p><strong>Servlet: p1.java</strong></p>
  
  <pre><code>import java.io.IOException;
  import java.sql.Connection;
  
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletException;
  import javax.servlet.ServletRequest;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  
  //import reg.common.Co;
  //import agge.db.geg;
  //import agge.model.m_login;
  import reg.db.RegisterDetails;
  
  
  /**
   * Servlet implementation class p1
   */
  @WebServlet("/p1")
  public class p1 extends HttpServlet {
      private static final long serialVersionUID = 1L;
  
      /**
       * @see HttpServlet#HttpServlet()
       */
      public p1() {
          super();
          // TODO Auto-generated constructor stub
      }
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      }
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
          HttpSession session = request.getSession();
          String a=request.getParameter("username1");
          String b=request.getParameter("password1");
          String c = request.getParameter("confirmpassword1");
          String d = request.getParameter("email1");
          String ee = request.getParameter("mob1");
          String f =request.getParameter("address1");
  
          System.out.println("Hai User "+a);
          System.out.println("Password is "+b);
  
  
          session.setAttribute("de", a); 
          session.setAttribute("de1", b);
          session.setAttribute("de2", c);
          session.setAttribute("de3", d);
          session.setAttribute("de4", ee);
          session.setAttribute("de5",f);
          RegisterDetails details = new  RegisterDetails();
  
          session.setAttribute("details", details);
          getServletContext().getRequestDispatcher("/jsp/Welcome.jsp").forward(request, response);
      }
  }
  </code></pre>
  
  <p><strong>Class: RegisterDetails.java</strong></p>
  
  <pre><code>package reg.db;
  
  import javax.servlet.ServletRequest;
  import javax.servlet.http.HttpSession;
  
  import java.sql.*;
  public class RegisterDetails {
      String user13;
      String pass13;
      String confpassword13;
      String email13;
      String mobile13;
      String address13;
      private ServletRequest session;
  
  
      String n=(String)session.getAttribute("de");
      String n1=(String)session.getAttribute("de1");
      String n2=(String)session.getAttribute("de2");
      String n3=(String)session.getAttribute("de3");
      String n4=(String)session.getAttribute("de4");
      String n5=(String)session.getAttribute("de5");
  
  
  
      public boolean insertDB() throws Exception {        
  
          Connection conn=null;
          PreparedStatement st = null;
  
          st=conn.prepareStatement("insert into user_details(username, password, confpassword, email,mobile, address) values ('" + n + "','" + n1 + "','" + n2 + "','" + n3 + "','" + n4 + "',' "+n5+ "')");
  
          ResultSet rs =st.executeQuery();
          boolean l_status=false;
          while(rs.next()){
              user13          =   rs.getString("username");
              pass13          =   rs.getString("password");
              confpassword13  =   rs.getString("confpassword");
              email13         =   rs.getString("email");
              mobile13            =   rs.getString("mobile");
              address13           =   rs.getString("address");
          }
          return l_status=true;
  
      }
  }
  </code></pre>
  
  <p><strong>Database connectivity: Dbconnect.java</strong></p>
  
  <pre><code>package reg.common;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.SQLException;
  public class Dbconnect {
  
      Connection conn=null;
  
      public  Connection getConnection() { 
           try {
               String driverName = "org.postgresql.Driver";
               Class.forName(driverName).newInstance(); 
  
               conn=DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres","postgres", "postgres");
           } 
           catch (ClassNotFoundException e) {e.printStackTrace();
               System.out.println("db Error1");
           } 
           catch (SQLException e) {
               e.printStackTrace();
               System.out.println("DB Error 2");
           }
           catch (Exception e) {
  
          }
           return conn;
      }
  
      public void closeConnection(Connection connection) {
          try{
              connection.close();
          }
          catch (Exception e) {
  
          }
  
      }
  }
  </code></pre>
  
  <p><strong>jsp:Welcome.jsp</strong></p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
      &lt;%@ page import="reg.db.RegisterDetails" %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
    Hai
  &lt;% RegisterDetails details = (RegisterDetails)session.getAttribute("details");%&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>