好的,我将在这里发布很多代码,其中一些您可能都想从中挖出你的眼睛。这是我的第一个 servlet 项目,我想我已经找到了所有可能的方法来破坏它,然后又发明了一些方法。
下面是三个jsp页面。用户通过电话号码选择客户或单击按钮输入新用户的第一页(索引)。这调用了一个 servlet (ChooseCustomer)。选择客户或选择输入新客户会将用户发送到第 2 页 (customer.jsp),如果他们选择了客户,或者他们需要编辑/输入新客户,该页面将填充客户信息。这再次一切正常。这就是我的麻烦开始的地方。此页面有两个按钮,用户单击它们可以启动交货单或取货单。这些按钮调用似乎工作正常的 servlet (Customersrvlt)。然而,用户随后被引导至订单页面。页面加载(有时),但我在将信息从会话获取到页面时遇到问题。我试过使用 session.setAttribute("name", "object"), 我尝试过使用 bean,以及我在书本和网上找到的任何东西。正如您从最后一个 jsp 页面上的代码中看到的那样,我有一些不同的东西。它正在工作,我现在吃了晚餐,但它没有。我关闭了一切并重新启动以防万一。没变。我无法同时访问订单和客户会话对象,或者我可以获取一个但不能获取另一个,并且我根本无法使用 ${order.oid} 标签获取订单对象。它说我的 oid 不存在并出现碧玉错误(如有必要,我可以发布)。我已经编写并重写了代码并尝试使用 . <% ... %> 给了我一个关于它在 div 中的错误,这就是它被注释掉的原因。PS:我有两个类 OrderBean 和 Customer,它们具有 setter 和 getter 以及构造函数。我对类中的所有内容进行了双重和三重检查,它们是否具有正确的小写 setOrderId() 格式。我的 web.xml 文件很好,并且映射了 servlet。在使用控制台设置会话属性进行打印并且对象似乎在那里之后,我还在 servlet 中进行了检查。我正在使用 Eclipse Juno、Tomcat 7(带有 XAMPP)和 MySQL。帮助!
索引.jsp
<%-- index.jsp --%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" isELIgnored="false" %>
<%@ page import="java.util.*" %>
<%@ include file="staticpages/pageHeader.html" %>
<br />
<hr />
<br />
<form name="selectcust" method="POST" action="ChooseCustomer.do" >
<div class="sectionheader" style="margin_right: auto; margin-left: auto; text-align: center;">Look Up Customer by Phone Number</div>
<br />
<div style="margin-right: auto; margin-left: auto; text-align: center;">
<input type="text" name="phone1" size="3" maxlength="3" />
<input type="text" name="phone2" size="3" maxlength="3" />
<input type="text" name="phone3" size="4" maxlength="4" />
<br /><br />
<input type="submit" name="formaction" value="Search" size="30" class="btn" />
<br /><br />
<input type="submit" name="formaction" value="Enter New Customer" size="30" class="btn" />
<br /><br />
<% if("No".equals(request.getAttribute("customerFound"))) { %>
<div style="color: #FF0000; text-align: center;" >Customer was not found, please re-enter or start a new customer.</div>
<% } %>
</div>
</form>
<%@ include file="staticpages/pageFooter.html" %>
客户.jsp
<%-- customer.jsp --%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" isELIgnored="false" %>
<%@ page import="java.util.*" %>
<%@ include file="staticpages/pageHeader.html" %>
<script src="scripts/customervalidation.js" type="text/javascript" ></script>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<br />
<hr />
<br />
<form name="custform" method="POST" onSubmit="return validateCustomerData();" action="submitCustomer.do" >
<span class="sectionheader">Enter or Edit Customer:</span>
<br /><input type="text" size="50" name="errmsg" class="err" />
<br />
<table>
<tr>
<td>
<span class="leftspan">First Name:</span>
<span class="rightspan">
<input type="text" name="fname" id="fname" disabled="disabled" value="${customer.cfname}" onKeyDown="editedCust()" size="45" />
<input type="text" size="1" name="errfn" class="err" disabled="disabled" /> </span><br />
<span class="leftspan">Last Name:</span>
<span class="rightspan">
<input type="text" name="lname" disabled="disabled" value="${customer.clname}" onKeyDown="editedCust()" />
<input type="text" size="1" name="errln" class="err" disabled="disabled" /></span><br />
<span class="leftspan">Address:</span>
<span class="rightspan">
<input type="text" name="address" disabled="disabled" value="${customer.caddress}" onKeyDown="editedCust()" />
<input type="text" size="1" name="errad" class="err" disabled="disabled" /></span><br />
<span class="leftspan">City:</span>
<span class="rightspan">
<input type="text" name="city" disabled="disabled" value="${customer.ccity}" onKeyDown="editedCust()" />
<input type="text" size="1" name="errct" class="err" disabled="disabled" /></span><br />
<span class="leftspan">State:</span>
<span class="rightspan">
<input type="text" name="state" size="3" maxlength="2" disabled="disabled" onKeyUp="checklen(this)" value="${customer.cstate}" onKeyDown="editedCust()" />
<input type="text" size="1" name="errst" class="err" disabled="disabled" />
Zip Code:
<input type="text" name="zip" size="5" maxlength="5" disabled="disabled" onKeyUp="checklen(this)" value="${customer.czip}" onKeyDown="editedCust()" />
<input type="text" size="1" name="errzp" class="err" disabled="disabled" /></span><br />
<span class="leftspan">Phone:</span>
<span class="rightspan">
<input type="text" name="phone4" size="3" maxlength="3" disabled="disabled" onKeyUp="checklen(this)" value="${fn:substring(customer.cphone, 0, 3)}" onKeyDown="editedCust()" />
<input type="text" name="phone5" size="3" maxlength="3" disabled="disabled" onKeyUp="checklen(this)" value="${fn:substring(customer.cphone, 3, 6)}" onKeyDown="editedCust()" />
<input type="text" name="phone6" size="4" maxlength="4" disabled="disabled" onKeyUp="checklen(this)" value="${fn:substring(customer.cphone, 6, 10)}" onKeyDown="editedCust()" />
<input type="text" size="1" name="errph" class="err" disabled="disabled" /></span>
<br />
<span class="leftspan" style="verticle-align: top">Notes:</span><br />
<textarea name="notes" rows="5" cols="35" disabled="disabled" onKeyDown="editedCust()">${customer.cnotes}</textarea><br />
<input type="hidden" name="custid" value="${customer.cid}" />
<input type="hidden" name="edited" />
</td>
<td>
<input type="button" name="editaddcustomer" value="Edit/Add Customer" onClick="enableFields()" />
<br />
<br />
<br />
<br />
<input type="submit" name="formaction" value="Save-Start Delivery Order" />
<br />
<br />
<br />
<br />
<input type="submit" name="formaction" value="Save-Start Pick Up Order" />
</td></tr>
</table>
</form>
<%@ include file="staticpages/pageFooter.html" %>
订单.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page language="java" import="java.util.*"%>
<%@ include file="staticpages/pageHeader.html" %>
<%@ page import="pizzapkg.OrderBean" %>
Customer Info:
<%--<% Customer cust = (Customer) session.getAttribute("customer"); --%>
Customer #: ${customer.cid}<br />
<%-- <% cust.getCid(); %>
<% cust.getCfname(); %> <% cust.getClname(); %><br />
<% cust.getCaddress(); %><br />
<% cust.getCcity(); %><br />
<% cust.getCstate(); %>, <% cust.getCzip(); %>
<% cust.getCphone(); %>$<br />
<% cust.getCnotes(); %><br />--%>
<% OrderBean ob = (OrderBean) session.getAttribute("order"); %>
Order #: <% ob.getOrderId(); %>
Date/Time: <% ob.getOrderDate(); ob.getOrderTime(); %>
<% ob.getOrderType(); %>
<%@ include file="staticpages/pageFooter.html" %>
选择客户 Servlet
package pizzapkg;
import java.io.IOException;
import java.sql.ResultSet;
import javax.servlet.RequestDispatcher;
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 javax.servlet.http.HttpSession;
/** Servlet implementation class ChooseCustomer */
@WebServlet("/ChooseCustomer")
public class ChooseCustomer extends HttpServlet {
private static final long serialVersionUID = 1L;
/** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);
session.isNew();
// Create customer object
Customer c = null;
// Check which button was selected on index page.
if (request.getParameter("formaction").equals("Search")) {
// Create database object to search for customer phone number
Database db = (Database) getServletContext().getAttribute("db");
/* Search Database for existing customer */
String searchPhone = request.getParameter("phone1")
+ request.getParameter("phone2")
+ request.getParameter("phone3");
String sql = "SELECT * FROM customers WHERE cust_phone=\"" + searchPhone + "\";";
ResultSet rs;
try {
rs = db.runSqlQuery(sql);
rs.next();
// Check to see if any records were returned.
if (rs.isFirst()) {
// Get data from database and populate customer object
c = new Customer(rs.getString("cust_id"), rs.getString("cust_fname"),
rs.getString("cust_lname"), rs.getString("cust_address"),
rs.getString("cust_city"), rs.getString("cust_state"),
rs.getString("cust_zip"), rs.getString("cust_phone"),
rs.getString("cust_notes"));
request.setAttribute("customerFound", "Yes");
}
else {
// Customer phone number not in database send error message.
request.setAttribute("customerFound", "No");
}
} catch (Exception e) { e.printStackTrace(); }
// Set request customer attribute
session.setAttribute("customer", c);
// If customer found forward to customer.jsp otherwise stay on index page.
if (request.getAttribute("customerFound").equals("No")) {
RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");
rd.forward(request, response);
}
else {
RequestDispatcher rd = request.getRequestDispatcher("/customer.jsp");
rd.forward(request, response);
}
}
else {
RequestDispatcher rd = request.getRequestDispatcher("/customer.jsp");
rd.forward(request, response);
}
}
}
客户rvlt Servlet
package pizzapkg;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import javax.servlet.RequestDispatcher;
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 javax.servlet.http.HttpSession;
/** Servlet implementation class Customersvlt */
@WebServlet("/Customersvlt")
public class Customersvlt extends HttpServlet {
private static final long serialVersionUID = 1L;
/** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
String btn = request.getParameter("formaction");
String delv = "Save-Start Delivery Order";
// Create customer object
Customer c = (Customer) session.getAttribute("customer");
// Create database object
Database db = (Database) getServletContext().getAttribute("db");
String phone = request.getParameter("phone4")
+ request.getParameter("phone5")
+ request.getParameter("phone6");
// If customer was not selected from database, insert customer
if(request.getParameter("custid").isEmpty()) {
try {
// Get customer information from index form.
c = new Customer(request.getParameter("custid"),
request.getParameter("fname"), request.getParameter("lname"),
request.getParameter("address"), request.getParameter("city"),
request.getParameter("state"),request.getParameter("zip"), phone,
request.getParameter("notes"));
c.setCid(db.insertCustomer(c));
String p = c.getCphone();
FormatPhone fp = new FormatPhone(p);
c.setCphone(fp.getPhone());
session.setAttribute("customer", c);
} catch (SQLException e) { e.printStackTrace(); }
}
else if (request.getParameter("edited").equals("edited")
&& !request.getParameter("custid").isEmpty()) {
// If customer was selected and edited update customer information.
c.setCid(request.getParameter("custid"));
try {
db.updateCustomer(c);
String p = c.getCphone();
FormatPhone fp = new FormatPhone(p);
c.setCphone(fp.getPhone());
session.setAttribute("customer", c);
} catch (SQLException e) { e.printStackTrace(); }
}
else {
// Customer information was not edited just start order.
String p = c.getCphone();
FormatPhone fp = new FormatPhone(p);
c.setCphone(fp.getPhone());
//session.setAttribute("customer", c);
}
// Start order based on user selection
//PizzaOrder po = null;
ResultSet rs = null;
OrderBean obean = null;
if (btn.equals(delv)) {
// Start new delivery order
try {
rs = db.startOrder(c, "Delivery");
rs.next();
} catch (SQLException e) { e.printStackTrace(); }
}
else {
// Start new pickup order
try {
rs = db.startOrder(c, "Pick Up");
rs.next();
} catch (SQLException e) { e.printStackTrace(); }
}
//String id, String cd, String dt, String tm, String pd, double tt) {
try {
String d = new SimpleDateFormat("MM/dd/yyyy").format(rs.getDate("order_datetime"));
String time = new SimpleDateFormat("h:mm a").format(rs.getTime("order_datetime"));
obean = new OrderBean(rs.getString("order_id"), rs.getString("order_cust_id"),
d, time, rs.getString("order_pupdeliv"), rs.getDouble("order_total"));
} catch (SQLException e) { e.printStackTrace(); }
session.setAttribute("order", obean);
RequestDispatcher rd = request.getRequestDispatcher("/order.jsp");
rd.forward(request, response);
}
}