title: Insert update and delete in servlet and jsp
body: <p>i am very new to servlet and jsp so i am trying to achieve a operation like view,add , update and delete but i dont have an idea to achieve this operation like how to forward a page and maintain session and pass value to jsp so kindly some could  help in right way.
  i had gone many tutorial but no idea and here tried so for  </p>
  
  <p>Controller.java</p>
  
  <pre><code>    package servlet;
  
      import java.io.IOException;
      import java.io.PrintWriter;
      import java.sql.Connection;
      import java.sql.DriverManager;
      import java.sql.PreparedStatement;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.sql.Statement;
  
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
  
      import db.Dbconnect;
  
  
      public class Controller extends HttpServlet 
      {
          private static final long serialVersionUID = 1L;
  
  
          public Controller() 
          {
              super();
          }
  
  
          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
          {
          }
  
  
          protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
          {
              response.setContentType("text/html");
              PrintWriter out=response.getWriter();
  
               String nam = request.getParameter("name"); 
               String pass = request.getParameter("pass"); 
               String email = request.getParameter("emailid"); 
               String phono = request.getParameter("phone");
  
  
          }
  
  
          public  void view() throws ClassNotFoundException
          {
  
              try
              {
                  String sql="select * from Reg";
                  Class.forName("com.mysql.jdbc.Driver");
                  Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mine", "root", "android");
                  Statement stmt=con.createStatement();
                  ResultSet rs=stmt.executeQuery(sql);
  
                  while(rs.next())
                  {
                      rs.getString("name");
                      rs.getString("pass");
                      rs.getString("emailid");
                      rs.getString("phoneno");
                     System.out.println(rs.getString("name"));
  
                  }
  
              } 
              catch (SQLException e)
              {
                  e.printStackTrace();
              }
              return;
  
          }
  
          public  String insertuser() throws SQLException
          {
  
              String sql = "insert into Reg(name,pass,emailid,phoneno) values(?,?,?,?)";
              Connection con = null;
              PreparedStatement prep = null;
  
              try
              {
                  Class.forName("com.mysql.jdbc.Driver");
                  con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mine", "root", "android");
                  prep = con.prepareStatement(sql);
                  prep.setString(1, "nam");
                  prep.setString(2, "pass");
                  prep.setString(3, "email");
                  prep.setString(4, "phono");
                  prep.executeUpdate();
                  prep.close();
  
              } 
              catch (ClassNotFoundException e)
              {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
  
              }
              return "successfully added";
          }
          public  String update() throws SQLException
          {
  
              String sql = "update Reg set pass=?,emailid=?,phoneno=? where name=?";
              Connection con = null;
              PreparedStatement prep = null;
  
              try
              {
                  Class.forName("com.mysql.jdbc.Driver");
                  con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mine", "root", "android");
                  prep = con.prepareStatement(sql);
                  prep.setString(1, "nam");
                  prep.setString(2, "pass");
                  prep.setString(3, "email");
                  prep.setString(4, "phono");
                  prep.executeUpdate();
                  prep.close();
  
              } 
              catch (ClassNotFoundException e)
              {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
  
              }
              return "successfully update";
          }
  
  public  String delete() throws SQLException
      {
  
          String sql = "delete form Reg where name=?";
          Connection con = null;
          PreparedStatement prep = null;
  
          try
          {
              Class.forName("com.mysql.jdbc.Driver");
              con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mine", "root", "android");
              prep = con.prepareStatement(sql);
  
          } 
          catch (ClassNotFoundException e)
          {
              // TODO Auto-generated catch block
              e.printStackTrace();
  
          }
          return "successfully delete";
      }
  
  
      }
  </code></pre>
  
  <p>Add.jsp::</p>
  
  <pre><code>&lt;form action="/Controller" method="post"&gt;
  User name:&lt;input type="text" name="name" &gt;
  Password:&lt;input type="password" name="pass"&gt;
  Email-id&lt;input type="text" name="emailid"&gt;
  phone number:&lt;input type="text" name="phone"&gt;
  &lt;input type="submit" value="submit"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Update.jsp:</p>
  
  <pre><code>&lt;form action="/Controller" method="post"&gt;
  User name:&lt;input type="text" name="name" &gt;
  Password:&lt;input type="password" name="pass"&gt;
  Email-id&lt;input type="text" name="emailid"&gt;
  phone number:&lt;input type="text" name="phone"&gt;
  
  &lt;input type="submit" value="Update"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"&gt;
    &lt;display-name&gt;Vweb2&lt;/display-name&gt;
    &lt;welcome-file-list&gt;
  
      &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
  
    &lt;/welcome-file-list&gt;
    &lt;servlet&gt;
      &lt;description&gt;&lt;/description&gt;
      &lt;display-name&gt;Controller&lt;/display-name&gt;
      &lt;servlet-name&gt;Controller&lt;/servlet-name&gt;
      &lt;servlet-class&gt;servlet.Controller&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;Controller&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/Controller&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p><code>View.jsp</code> and <code>delete.jsp</code> for this two page i dono how to retrieve information servlet to jsp</p>