我是新来的,我想弄清楚为什么我的 jsp 不显示我的输出。请记住,我是新来的,所以请善待。 AddCustomers 表单将输入发送到数据库,但 AddCustomers.jsp 不会在表中显示名字、姓氏、地址和密码。但是,它会显示带有上述标题的行,但不会显示其他内容。
我已经查看了所有与此类似的问题,但找不到任何可以解决此问题的问题。我正在将 Eclipse IDE 和 Postgres 与 Wildfly 23 一起使用。也许这是一个简单的错误,我只是花了很多时间看它以至于看不到它,也许看不到,但无论哪种方式我都看不到它.
任何帮助表示赞赏。
这是我的表单、servlet 和 jsp 代码。
添加客户表格
<!-- header -->
<jsp:include page="header.jsp" />
<title>FlyAway Register</title>
</head>
<body>
<!-- Form to gather user information -->
<section>
<div class="wrapper">
<h1>FlyAway Registration</h1>
<div id="AddCustomer_form_Div">
<h4 style="color: #00BFFF; padding-bottom: 30px;">Please fill out the form below to get started!</h4>
<form action="AddCustomers" method="get" class="form-horizontal">
<div class="form-group">
<div class="col-sm-4 col-md-6 col-lg-10">
<label for="fName" id="fName">First Name: </label>
<input
type="text" class="form-control" name="fName"
value="${customer.fName }" />
</div>
</div>
<div class="form-group" id="lName">
<div class="col-sm-4 col-md-6 col-lg-10">
<label for="lName"id="lName" >Last Name: </label>
<input type="text"
class="form-control" id="lName" name="lName" value="${customer.lName }" />
</div>
</div>
<div class="form-group">
<div class="col-sm-4 col-md-6 col-lg-10">
<label for="address" id="address">Address: </label> <input type="text"
class="form-control" id="address" name="address" style="margin-left: 17px;"
value="${customer.address }" />
</div>
</div>
<div class="form-group">
<div class="col-sm-4 col-md-6 col-lg-10">
<label for="password" id="password">Password: </label> <input type="password"
class="form-control" id="password" name="password" style="margin-left: 7px;"
value="${customer.password }" />
</div>
</div>
<br> <br>
<button id="AddCustomers" class="btn btn-success btn-lg"
style="background-color: #00BFFF; color: #ffffff ;" type="submit" value="submit">Register</button>
</form>
</div>
</div>
</section>
<!-- footer -->
<jsp:include page="footer.jsp" />
这是servlet代码
package flyaway.airlines.servlets;
import java.io.*;
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 flyaway.ailrines.model.Customers;
import flyaway.airlines.dao.FlyawayDAO;
import flyaway.airlines.dao.FlyawayDAOTestConnection;
/**
* Servlet implementation class AddCustomersServlet
*/
@WebServlet("/AddCustomers")
public class AddCustomersServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String fName = request.getParameter("fName");
String lName = request.getParameter("lName");
String address = request.getParameter("address");
String password = request.getParameter("password");
boolean a;
FlyawayDAO dao = FlyawayDAOTestConnection.getFlyawayDAO();
Customers customer = new Customers(fName, lName, address, password);
a = dao.addCustomer(customer);
List<Customers> customers = dao.getCustomers();
//
request.getSession().setAttribute("customer", customers);
request.getRequestDispatcher("AddCustomers.jsp").forward(request, response);
}
}
这是jsp代码
<!-- header -->
<jsp:include page="header.jsp" />
<!-- JSTL includes -->
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<header>
<div class="wrapper">
<table id="customers">
<thead>
<tr>
<td>First Name</td>
<td>Last Name</td>
<td>Address</td>
<td>Password</td>
</tr>
</thead>
<tbody>
<c:forEach var="customer" items="${customers}" >
<tr>
<c:out value="${customer.fName }" />
<c:out value="${customer.lName }" />
<c:out value="${customer.address }" />
<c:out value="${customer.password }" />
</tr>
</c:forEach>
</tbody>
</table>
<a href="AddCustomers_Form.jsp"><button id="AddCustomerButton"
class="btn btn-info btn-lg" type="submit">Back</button></a>
</div>
</header>
<!-- footer -->
<jsp:include page="footer.jsp" />
附带说明一下,如果我以这种方式执行 servlet,我可以让表格打印用户信息,但是我会丢失所有的 css 样式。
package flyaway.airlines.servlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;
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 flyaway.ailrines.model.Customers;
import flyaway.airlines.dao.FlyawayDAO;
import flyaway.airlines.dao.FlyawayDAOTestConnection;
/**
* Servlet implementation class Display
*/
@WebServlet("/Display")
public class Display extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
FlyawayDAO dao = FlyawayDAOTestConnection.getFlyawayDAO();
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println("<html>");
out.print("<head>");
out.println("<body>");
out.println("<table border=1 width=50% background-color=blue>");
out.println("<tr><th>FirstName</th><th>LastName</th><th>Address</th><th>Password</th></tr>");
String fn = request.getParameter("fName");
String ln = request.getParameter("lName");
String ad = request.getParameter("address");
String pa = request.getParameter("password");
List<Customers> customers = dao.getCustomers();
out.println("<tr><td>" + fn + "</td><td>" + ln + "</td><td>" + ad + "</td><td>" + pa + "</td></tr>");
out.println("</table>");
out.print("</body>");
out.println("</html>");
}
}