如何将值从一个jsp传递到另一个jsp以更新数据库

如何将值从一个jsp传递到另一个jsp以更新数据库,jsp,servlets,jstl,Jsp,Servlets,Jstl,我有LookupStudent.jsp根据某些条件搜索并列出学生 这个jsp有一个指向另一个jsp UpdateStudent.jsp的ref链接 我想 根据LookupStudent.jsp中的学生id在UpdateStudent.jsp中显示学生 根据步骤1中所做的更改更新表 你能帮我怎么做吗 LookupStudent.jsp <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <

我有LookupStudent.jsp根据某些条件搜索并列出学生

这个jsp有一个指向另一个jsp UpdateStudent.jsp的ref链接

我想

  • 根据LookupStudent.jsp中的学生id在UpdateStudent.jsp中显示学生
  • 根据步骤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>
    
        <%@ 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>
    
    
    查找学生
    学生姓名:
    等于
    开始于
    以
    包含
    身份证件
    标题
    名字
    姓
    ${students.studentID}
    ${students.title}
    ${students.firstName}
    ${students.lastName}
    有${fn:length(studentList)}个结果

    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);
            }
    
            }
    
        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);
            }
    
            }
    
    package org.cms.controller;
    导入java.io.IOException;
    导入java.sql.SQLException;
    导入java.util.List;
    导入javax.servlet.ServletException;
    导入javax.servlet.annotation.WebServlet;
    导入javax.servlet.http.HttpServlet;
    导入javax.servlet.http.HttpServletRequest;
    导入javax.servlet.http.HttpServletResponse;
    导入org.cms.model.StudentDAO;
    /**
    *Servlet实现类ListStudent
    */
    @WebServlet(“/LookupStudentServlet”)
    公共类LookupStudentServlet扩展了HttpServlet{
    私有静态最终长serialVersionUID=1L;
    /**
    *@请参阅HttpServlet#doPost(HttpServletRequest请求,HttpServletResponse响应)
    */
    受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
    试一试{
    字符串fnameOperator=request.getParameter(“fnameOperator”);
    //System.out.println(fnameOperator);
    字符串fname=request.getParameter(“fname”);
    字符串条件=“其中1=1”;
    如果(fname!=null | | fname.length()>0){
    if(fnameOperator.equalsIgnoreCase(“Eq”)){
    条件=条件+”,名字=“+fname+”;
    }
    else if(fnameOperator.equalsIgnoreCase(“Sw”)){
    条件=条件+”和第一个名称,如“+fname+”%”;
    }
    else if(fnameOperator.equalsIgnoreCase(“Ew”)){
    条件=条件+”和第一个名称,如“%”“+fname+”;
    }
    else if(fnameOperator.equalsIgnoreCase(“Co”)){
    条件=条件+”和第一个_名称,如“%”+fname+“%”;
    }
    }       
    //系统输出打印项次(条件);
    StudentDAO student=新StudentDAO();
    List student=student.lookupStudent(条件);
    setAttribute(“学生列表”,学生);
    }捕获(SQLException sqle){
    setAttribute(“错误”,“检索学生失败”);
    printStackTrace();
    }
    捕获(例外e){
    e、 printStackTrace();
    }
    最后{}
    getRequestDispatcher(“LookupStudent.jsp”).forward(请求,响应);
    }
    }
    
    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>
    
        <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>
    
        <%@ 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("/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);
            }
    
            }
    
        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);
            }
    
            }
    
    package org.cms.controller;
    导入java.io.IOException;
    导入java.sql.SQLException;
    导入java.util.List;
    导入javax.servlet.ServletException;
    导入javax.servlet.annotation.WebServlet;
    导入javax.servlet.http.HttpServlet;
    导入javax.servlet.http.HttpServletRequest;
    导入javax.servlet.http.HttpServletResponse;
    导入org.cms.model.StudentDAO;
    /**
    *Servlet实现类ListStudent
    */
    @WebServlet(“/UpdateStudentServlet”)
    公共类UpdateStudentServlet扩展了HttpServlet{
    私有静态最终长serialVersionUID=1L;
    /**
    *@请参阅HttpServlet#doPost(HttpServletRequest请求,HttpServletResponse响应)
    */
    受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
    试一试{
    国际学生;
    studentID=Integer.parseInt(request.getParameter(“studentID”);
    //System.out.println(fnameOperator);
    字符串条件=“其中1=1”;
    条件=条件+”和学生id=“+studentID”;
    系统输出打印项次(条件);
    StudentDAO student=新StudentDAO();
    
    session.setAttribute("name", object)
    
    (String)session.getAttribute("name")