title: how to pass values from one jsp to another jsp to update database
body: <p>I have LookupStudent.jsp to search and list student based on some condition.</p>
  
  <p>this jsp has a ref link to a another jsp, UpdateStudent.jsp.</p>
  
  <p>I want to </p>
  
  <ol>
  <li>display a student in the UpdateStudent.jsp based on a student id from LookupStudent.jsp</li>
  <li>Update table based on the changes made in step 1.</li>
  </ol>
  
  <p>Please can you help how to acheive this?</p>
  
  <p>LookupStudent.jsp</p>
  
  <pre><code>    &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%&gt;
      &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%&gt;
  
      &lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
      &lt;!DOCTYPE html&gt;
      &lt;html&gt;
      &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
      &lt;title&gt;Lookup Students&lt;/title&gt;
      &lt;/head&gt;
  
      &lt;body class="body"&gt;
  
          &lt;form method="get" action="LookupStudentServlet"&gt;
  
              &lt;table border="0"&gt;
                  &lt;tr align="left" valign="top"&gt;
                      &lt;td&gt;Student Name:&lt;/td&gt;
                      &lt;td&gt;&lt;select name="fnameOperator"&gt;
                              &lt;option value="Eq"&gt;Equals&lt;/option&gt;
                              &lt;option value="Sw"&gt;Starts With&lt;/option&gt;
                              &lt;option value="Ew"&gt;Ends With&lt;/option&gt;
                              &lt;option value="Co"&gt;Contains&lt;/option&gt;
                      &lt;/select&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;input type="text" name="fname" /&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr align="left" valign="top"&gt;
                      &lt;td&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;input type="submit" name="submit" value="submit" /&gt;&lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
  
          &lt;/form&gt;
  
          &lt;!-- List results --&gt;
  
          &lt;c:if test="${not empty studentList}"&gt;
              &lt;table border="1" cellspacing="0" cellpadding="0" :&gt;
                  &lt;tr&gt;
                      &lt;th&gt;ID&lt;/th&gt;
                      &lt;th&gt;Title&lt;/th&gt;
                      &lt;th&gt;First Name&lt;/th&gt;
                      &lt;th&gt;Last Name&lt;/th&gt;
                  &lt;/tr&gt;
                  &lt;c:forEach var="students" items="${studentList}"&gt;
                      &lt;tr&gt;
                          &lt;td&gt;${students.studentID}&lt;/td&gt;
                          &lt;td&gt;${students.title}&lt;/td&gt;
                          &lt;td&gt;${students.firstName}&lt;/td&gt;
                          &lt;td&gt;${students.lastName}&lt;/td&gt;
                          &lt;td&gt;&lt;c:url value="UpdateStudent.jsp" var="url"&gt;
                                  &lt;c:param name="StudentID" value="${students.studentID}" /&gt;
                              &lt;/c:url&gt; &lt;a href="${url}"&gt;edit&lt;/a&gt;
                      &lt;/tr&gt;
                  &lt;/c:forEach&gt;
              &lt;/table&gt;
          &lt;/c:if&gt;
  
          &lt;p&gt;There are ${fn:length(studentList)} results.&lt;/p&gt;
      &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>
  
  <p>LookupStudentServlet.java</p>
  
  <pre><code>    package org.cms.controller;
  
      import java.io.IOException;
      import java.sql.SQLException;
      import java.util.List;
  
      import javax.servlet.ServletException;
      import javax.servlet.annotation.WebServlet;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
  
      import org.cms.model.StudentDAO;
  
      /**
       * Servlet implementation class ListStudent
       */
      @WebServlet("/LookupStudentServlet")
      public class LookupStudentServlet extends HttpServlet {
          private static final long serialVersionUID = 1L;
  
  
          /**
           * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
           */
          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              try {
                  String fnameOperator = request.getParameter("fnameOperator");
  
                  //System.out.println(fnameOperator);
  
                  String fname = request.getParameter("fname");
                  String condition = "where 1=1 ";
  
                  if (fname!=null||fname.length()&gt;0) {
                      if (fnameOperator.equalsIgnoreCase("Eq")) {
                          condition =condition+ "and first_name = '"+fname+"'";
                      }
                      else if (fnameOperator.equalsIgnoreCase("Sw")) {
                          condition =condition+ "and first_name like '"+fname+"%'";
                      }
                      else if (fnameOperator.equalsIgnoreCase("Ew")) {
                          condition =condition+ "and first_name like '%"+fname+"'";
                      }
                      else if (fnameOperator.equalsIgnoreCase("Co")) {
                          condition =condition+ "and first_name like '%"+fname+"%'";
                      }
  
                  }       
  
                  //System.out.println(condition);
  
                  StudentDAO student = new StudentDAO();
                  List&lt;StudentDAO&gt; students = student.lookupStudent(condition);
                  request.setAttribute("studentList", students);
              } catch (SQLException sqle) {
                  request.setAttribute("error", "Retrieving Students failed.");
                  sqle.printStackTrace();
              }
           catch (Exception e) {
                  e.printStackTrace();
          }
              finally {}
              request.getRequestDispatcher("LookupStudent.jsp").forward(request, response);
          }
  
          }
  </code></pre>
  
  <p>UpdateStudent.jsp</p>
  
  <pre><code>    &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%&gt;
      &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%&gt;
  
      &lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
      &lt;!DOCTYPE html&gt;
      &lt;html&gt;
      &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
      &lt;title&gt;Lookup Students&lt;/title&gt;
      &lt;/head&gt;
      &lt;link rel="stylesheet" href="css/style.css" type="text/css"&gt;&lt;/link&gt;
  
      &lt;body class="body"&gt;
  
  
          &lt;form method="get" action="UpdateStudent" class="form"&gt;
  
  
              &lt;table border="0"&gt;
                  &lt;tr align="left" valign="top"&gt;
                      &lt;td&gt;Student ID:&lt;/td&gt;
                      &lt;td&gt;&lt;input type="text" name="StudentID" /&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr align="left" valign="top"&gt;
                      &lt;td&gt;Title:&lt;/td&gt;
                      &lt;td&gt;&lt;input type="text" name="Title" /&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr align="left" valign="top"&gt;
                      &lt;td&gt;First Name:&lt;/td&gt;
                      &lt;td&gt;&lt;input type="text" name="Fname"  /&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr align="left" valign="top"&gt;
                      &lt;td&gt;Last Name:&lt;/td&gt;
                      &lt;td&gt;&lt;input type="text" name="Lname"  /&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr align="left" valign="top"&gt;
                      &lt;td&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;input type="submit" name="submit" value="submit"
                          class="fb8" /&gt;&lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
  
  
  
          &lt;/form&gt;
  
  
      &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>
  
  <p>UpdateStudentServlet.java</p>
  
  <pre><code>    package org.cms.controller;
  
      import java.io.IOException;
      import java.sql.SQLException;
      import java.util.List;
  
      import javax.servlet.ServletException;
      import javax.servlet.annotation.WebServlet;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
  
      import org.cms.model.StudentDAO;
  
      /**
       * Servlet implementation class ListStudent
       */
      @WebServlet("/UpdateStudentServlet")
      public class UpdateStudentServlet extends HttpServlet {
          private static final long serialVersionUID = 1L;
  
  
          /**
           * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
           */
          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              try {
  
                  int studentID;
                  studentID = Integer.parseInt(request.getParameter("StudentID"));
                  //System.out.println(fnameOperator);
                  String condition = "where 1=1";
                  condition = condition+"and student_id = "+studentID;
                  System.out.println(condition);
                  StudentDAO student = new StudentDAO();
                  List&lt;StudentDAO&gt; students = student.lookupStudent(condition);
                  request.setAttribute("studentList", students);
              } catch (SQLException sqle) {
                  request.setAttribute("error", "Retrieving Students failed.");
                  sqle.printStackTrace();
              }
           catch (Exception e) {
                  e.printStackTrace();
          }
              finally {}
              request.getRequestDispatcher("UpdateStudent.jsp").forward(request, response);
          }
  
          }
  </code></pre>