【问题标题】:How to redirect the user to the intended page with the details he want to modify in jsp servlet?如何将用户重定向到想要在 jsp servlet 中修改的详细信息的页面?
【发布时间】:2017-02-06 12:09:50
【问题描述】:

我的项目的主要功能是赋予用户修改他想要的事件的权限。 所以第一页是 modify-event.jsp,他将在其中获取系统中存在的事件列表,最后一列有修改按钮

修改事件.jsp

<%@page import="java.util.ArrayList"%>
<jsp:include page="includes/header.jsp" />

<div>

    <%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>


<%

    HttpSession session1 = request.getSession();

//String id = request.getParameter("userId");
String driverName = "com.mysql.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost:3306/";
String dbName = "technovision";
String userId = "root";
String password = "root";

try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;


%>
<h2 align="center"><font><strong>Retrieve data from database in jsp</strong></font></h2>
<table align="center" cellpadding="5" cellspacing="5" border="1">
<tr>

</tr>
<tr bgcolor="#A52A2A">
<td><b>Event name</b></td>
<td><b>Registration Amount</b></td>
<td><b>EventHead name</b></td>
<td><b>EventHead contact</b></td>
<td><b>Event Description</b></td>
<td><b>Action</b></td>
</tr>
<%
try{ 
connection = DriverManager.getConnection(connectionUrl+dbName, userId, password);
statement=connection.createStatement();
String sql ="SELECT * FROM events";

resultSet = statement.executeQuery(sql);


while(resultSet.next()){
session1.setAttribute("eventid",resultSet.getString(1));  
session1.setAttribute("eventname",resultSet.getString(2));  
session1.setAttribute("registrationamount",resultSet.getString(3)); 
session1.setAttribute("eventheadname",resultSet.getString(4)); 
session1.setAttribute("eventheadcontact",resultSet.getString(5)); 
session1.setAttribute("eventdescription",resultSet.getString(6)); 
//ArrayList a = new ArrayList();
//a.add(resultSet.getString(1));
 //a.add(resultSet.getString(2));
//a.add(resultSet.getString(3));
//a.add(resultSet.getString(4));
//a.add(resultSet.getString(5));
//a.add(resultSet.getString(6));

%>
<tr bgcolor="#DEB887">

<td><%=resultSet.getString("event_name") %></td>
<td><%=resultSet.getString("registration_amount") %></td>
<td><%=resultSet.getString("eventhead_name") %></td>
<td><%=resultSet.getString("eventhead_contact") %></td>
<td><%=resultSet.getString("event_description") %></td>
<td><a href="modify-page.jsp">Modify</a></td>

</tr>

<% 
}


} catch (Exception e) {
e.printStackTrace();
}
%>
</table>



</div>


<%@ include file="includes/footer.jsp" %>
<%@include file="includes/scripts.jsp" %>
</body>
</html>

现在,当用户单击任何修改按钮时,他应该重定向到将获取单击事件详细信息的页面,以便他可以编辑相同的内容,但这里的情况是它在修改中打开上表中的最后一个数据-event.jsp 因为我使用了 httpsession 但我猜错了。 因此,当用户单击修改按钮时,我将他重定向到页面名称 modify-page.jsp

修改-page.jsp

<%@page import="java.util.ArrayList"%>
<jsp:include page="includes/header.jsp" />
<%--<%@ page import="com.event"%>--%>
<style>

    input.eventdescription{
        padding-top : 20px;
  padding-bottom : 100px;
    }


    </style>

   <%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.*"%>
<%@page import="java.sql.Connection"%>
<%

//    ArrayList[] eventname = new ArrayList[10];
//    ArrayList[] registrationamount = new ArrayList[10];
//    ArrayList[] eventheadname = new ArrayList[10];
//    ArrayList[] eventheadcontact = new ArrayList[10];
//    ArrayList[] eventdescription = new ArrayList[10];
//    ArrayList[] eventid = new ArrayList[10];
//    
//HttpSession session1 = request.getSession();
// eventname = (ArrayList[])session1.getAttribute("eventname");
// registrationamount = (ArrayList[])session1.getAttribute("registrationamount");
// eventheadname = (ArrayList[])session1.getAttribute("eventheadname");
// eventheadcontact = (ArrayList[])session1.getAttribute("eventheadcontact");
// eventdescription = (ArrayList[])session1.getAttribute("eventdescription");
// eventid = (ArrayList[])session1.getAttribute("eventid");



%>



    <div class="addevent-content" style="background-color:black">

     <div class="pattern height-resize">
<div class="container">


    <form class="form-horizontal" action="UpdateEvent" method="post"
        id="addevent_form" >


        <!-- Form Name -->

        <center>
                    <h2  id="registerheading">
                        <b style="color: gold">Modify Event</b>
            </h2>
        </center>

        <br>

        <!-- Text input-->

        <div class="form-group">
            <label class="col-md-5 control-label" style="color: #fff">Event
                Name<abbr title="Required">*</abbr>
            </label>
            <div class="col-md-5 inputGroupContainer">
                <div class="input-group">
                    <input name="event_name" id="form_fname" placeholder="Event Name"
                                               value='${eventname}' class="form-control" type="text" required>

                </div>
            </div>
        </div>

        <!-- Text input-->

        <div class="form-group">
            <label class="col-md-5 control-label" style="color: #fff">Registration Amount<abbr title="Required">*</abbr>
            </label>
            <div class="col-md-5 inputGroupContainer">
                <div class="input-group">
                    <input name="registration_amount" id="form_lname" placeholder="Registration Amount"
                        value='${registrationamount}'  class="form-control" type="text" required>

                </div>
            </div>
        </div>



        <!-- Text input-->

        <div class="form-group">
            <label class="col-md-5 control-label" style="color: #fff">Event Image<abbr
                title="Required">*</abbr></label>
            <div class="col-md-5 inputGroupContainer">
                <div class="input-group">
                    <input name="event_image" placeholder="Event Image" class="form-control"
                    value=''  type="text" required>

                </div>
            </div>
        </div>

        <!-- Text input-->

        <div class="form-group">
            <label class="col-md-5 control-label" style="color: #fff">EventHead Name<abbr
                title="Required">*</abbr></label>
            <div class="col-md-5 inputGroupContainer">
                <div class="input-group">
                    <input name="eventhead_name" id="password1" placeholder="EventHead Name"
                    value='${eventheadname}'    class="form-control" type="text" required>

                </div>
            </div>
        </div>

        <!-- Text input-->

        <div class="form-group">
            <label class="col-md-5 control-label" style="color: #fff">EventHead Contact
                <abbr title="Required">*</abbr>
            </label>
            <div class="col-md-5 inputGroupContainer">
                <div class="input-group">
                    <input name="eventhead_contact" id="password2" placeholder="(+91)"
                    value='${eventheadcontact}'     class="form-control" type="text" required>

                </div>
            </div>
        </div>

        <!-- Text input-->
        <div class="form-group">
            <label class="col-md-5 control-label" style="color: #fff">Event Description<abbr
                title="Required">*</abbr></label>
            <div class="col-md-5 inputGroupContainer">
                <div class="input-group">
                    <input name="event_description" id="email" placeholder=""
                    value='${eventdescription}'  class="form-control eventdescription" type="text" required>

                </div>
            </div>
        </div>



        <!-- Select Basic -->
                <input name="event_id"class="form-control"
                    value='${eventid}'    type="hidden" >

        <!-- Button -->
        <div class="form-group">
            <label class="col-md-5 control-label"></label>
            <div class="col-md-5">
                <br>

                <button type="submit" class="btn btn-warning">SUBMIT</button>
            </div>
        </div>


    </form>
</div>
    <!-- /.container -->
    </div>
        <!-- pattern height resize-->

</div>    



<%@ include file="includes/footer.jsp" %>
<%@include file="includes/scripts.jsp" %>

</body>
</html>

那么当用户点击修改按钮时,有什么办法可以从数据库中检索 modify-page.jsp 中的特定事件详细信息进行修改,然后用户可以点击提交来更新数据库。

下面是数据库表“Events”的结构

Database structure

【问题讨论】:

  • 我会从头开始。 MVC 模式。
  • 距离提交这个最后一年的项目仅剩 15 天。我的时间越来越少了。
  • 发送带有 url 的唯一 id,">修改。然后在 modify-page request.getParameter("id") 中获取id。
  • 学习 MVC 模式需要 cca 一半,最大值。有一天,如果你是一个学习缓慢的人。 Google for jsp servlet mvc pattern tutorial,有很多结果,甚至是一些 YT 视频,如果你喜欢看而不是阅读的话。您将受益于学习如何正确地做事,而不是编写大量意大利面条式代码。
  • 哦,那我想我应该试试看你能推荐一些链接吗@JozefChocholacek

标签: java mysql jsp servlets


【解决方案1】:

用 url 发送唯一的 id,

<a href="modify-page.jsp?id=<%=resultSet.getString("eventid") %>">Modify</a>

然后在修改页面中获取id request.getParameter("id")

PreparedStatement再次连接数据库

使用SELECT * FROM Events WHERE eventid= ? sql 语句。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 2021-10-17
    • 2018-03-26
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    相关资源
    最近更新 更多