gpt4 book ai didi

java - 如何将用户重定向到包含他想在 jsp servlet 中修改的详细信息的预期页面?

转载 作者:行者123 更新时间:2023-11-30 22:05:08 25 4
gpt4 key购买 nike

我的项目的主要功能是授予用户修改他想要的事件的权限。所以第一页是 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

修改页面.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

最佳答案

通过 url 发送唯一的 id,

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

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

PreparedStatement再次连接数据库

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

关于java - 如何将用户重定向到包含他想在 jsp servlet 中修改的详细信息的预期页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42067457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com