2012-09-08 36 views
0

我有LookupStudent.jsp根據某些條件搜索和列出學生。如何將值從一個jsp傳遞給另一個jsp以更新數據庫

這個jsp有一個ref鏈接到另一個jsp UpdateStudent.jsp。

我想

  1. 顯示在UpdateStudent.jsp學生基於從LookupStudent.jsp學生證
  2. 基於在步驟1中

所做的更改

  • 更新表請你幫助如何實現這一目標?

    LookupStudent.jsp

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
        <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> 
    
        <%@page contentType="text/html" pageEncoding="UTF-8"%> 
        <!DOCTYPE html> 
        <html> 
        <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
        <title>Lookup Students</title> 
        </head> 
    
        <body class="body"> 
    
         <form method="get" action="LookupStudentServlet"> 
    
          <table border="0"> 
           <tr align="left" valign="top"> 
            <td>Student Name:</td> 
            <td><select name="fnameOperator"> 
              <option value="Eq">Equals</option> 
              <option value="Sw">Starts With</option> 
              <option value="Ew">Ends With</option> 
              <option value="Co">Contains</option> 
            </select></td> 
            <td><input type="text" name="fname" /></td> 
           </tr> 
           <tr align="left" valign="top"> 
            <td></td> 
            <td><input type="submit" name="submit" value="submit" /></td> 
           </tr> 
          </table> 
    
         </form> 
    
         <!-- List results --> 
    
         <c:if test="${not empty studentList}"> 
          <table border="1" cellspacing="0" cellpadding="0" :> 
           <tr> 
            <th>ID</th> 
            <th>Title</th> 
            <th>First Name</th> 
            <th>Last Name</th> 
           </tr> 
           <c:forEach var="students" items="${studentList}"> 
            <tr> 
             <td>${students.studentID}</td> 
             <td>${students.title}</td> 
             <td>${students.firstName}</td> 
             <td>${students.lastName}</td> 
             <td><c:url value="UpdateStudent.jsp" var="url"> 
               <c:param name="StudentID" value="${students.studentID}" /> 
              </c:url> <a href="${url}">edit</a> 
            </tr> 
           </c:forEach> 
          </table> 
         </c:if> 
    
         <p>There are ${fn:length(studentList)} results.</p> 
        </body> 
        </html> 
    

    LookupStudentServlet.java

    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()>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<StudentDAO> 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); 
         } 
    
         } 
    

    UpdateStudent.jsp

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
        <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> 
    
        <%@page contentType="text/html" pageEncoding="UTF-8"%> 
        <!DOCTYPE html> 
        <html> 
        <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
        <title>Lookup Students</title> 
        </head> 
        <link rel="stylesheet" href="css/style.css" type="text/css"></link> 
    
        <body class="body"> 
    
    
         <form method="get" action="UpdateStudent" class="form"> 
    
    
          <table border="0"> 
           <tr align="left" valign="top"> 
            <td>Student ID:</td> 
            <td><input type="text" name="StudentID" /></td> 
           </tr> 
           <tr align="left" valign="top"> 
            <td>Title:</td> 
            <td><input type="text" name="Title" /></td> 
           </tr> 
           <tr align="left" valign="top"> 
            <td>First Name:</td> 
            <td><input type="text" name="Fname" /></td> 
           </tr> 
           <tr align="left" valign="top"> 
            <td>Last Name:</td> 
            <td><input type="text" name="Lname" /></td> 
           </tr> 
           <tr align="left" valign="top"> 
            <td></td> 
            <td><input type="submit" name="submit" value="submit" 
             class="fb8" /></td> 
           </tr> 
          </table> 
    
    
    
         </form> 
    
    
        </body> 
        </html> 
    

    UpdateStudentServlet.java

    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<StudentDAO> 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); 
         } 
    
         } 
    
  • 回答

    2

    如果問題僅涉及從一個jsp傳遞參數到另一個。 你有多種方式。您可以使用類似這樣

    request.setAttribute("parameterName") 
    

    OR 您可以使用類似和標籤。這樣你可以通過 傳遞參數。即使你的代碼看起來像傳遞參數,你正在得到什麼樣的異常 ? 謝謝, 本

    0

    如果你想從頁面發送參數,你可以使用會話也頁:

    session.setAttribute("name", object) 
    

    和其他頁面:

    session.getAttribute("name") 
    
    0

    我當然相信我們可以將這些參數放入範圍中,但要確保非常謹慎地使用會話,因爲太多會話屬性可能會減慢你的表現。 HTH, 本

    1

    可以使用

    <jsp:forward page = "UpdateStudent.jsp"> 
    <jsp:param name = "id" value = "15" /> 
    </jsp:forward> 
    
    0

    如果你想通過字符串,整數....

    session.setAttribute("name", object) 
    

    和其他頁面

    (String)session.getAttribute("name") 
    
    相關問題