JSP / Servlet如果错误停留在同一页面而不是转发

时间:2012-11-09 02:56:28

标签: java mysql jsp

我是新来的,整个晚上一直在努力。我知道这应该很简单,我只是遗漏了一些愚蠢的东西。我有一个jsp页面和一个servlet(下面的代码)。我试图从我的数据库中搜索出一个可以运行的客户。但是,如果找不到客户,我想在同一个jsp页面上发布错误消息,以便用户可以在出错时重新输入电话号码。我不确定该怎么做。我可以转发到新的“客户”页面,我已经尝试成功重定向到同一页面,但我不知道如何将消息放在那里。求救!

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="custform" method="POST" action="ChooseCustomer.do" >
            <span class="sectionheader">Look Up Customer by Phone Number:</span>

            <input type="text" name="phone1" size="3" maxlength="3" onKeyUp="checklen(this)" />             
            <input type="text" name="phone2" size="3" maxlength="3" onKeyUp="checklen(this)" />
            <input type="text" name="phone3" size="4" maxlength="4" onKeyUp="checklen(this)" />
            <input type="submit" name="formaction" value="Search" />
            <input type="submit" name="formaction" value="Enter New Customer" />
            </form> 
    <%@ 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;

/**
 * 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 {
        Customer c = null;
        if (request.getParameter("formaction").equals("Search")) {

                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();
                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"));
            } catch (Exception e) { e.printStackTrace(); }
            request.setAttribute("customer", c);        
        }
        RequestDispatcher rd = request.getRequestDispatcher("/customer.jsp");
        rd.forward(request, response);
    }
}

PS这是我第一次构建一个servlet,所以这对我来说都是新手。我喜欢这个例子,所以你能给予的任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

如果找不到客户,您可以在request内的servlet对象中设置一个标记。

          request.setAttribute("customerFound", "No");

在JSP中,放置一个JSP scriptlet来检查请求属性并在任何地方打印消息,例如:如果您希望在<HR/>之后发送消息:

        <hr />
        <% if("No".equals(request.getAttribute("customerFound")) { %>
            <div style="color: red">No customer found</div>
        <% } %>
        <br />

我正在分享实现理想结果的基本方法。