I am new here and have been working on this all evening. I know this should be simple and I am just missing something silly. I have a jsp page and a servlet (code below). I am trying to search out a customer from my database, which works. However, if the customer is not found I want to post an error message on the same jsp page so that the user can possibly re-enter the phone number in case they made a mistake. I am unsure how to do this. I can forward to the new “customer” page just fine and I have tried successfully to redirect back to the same page, but I don’t know how to put the message there. Help please!
jsp page:
<%@ 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 code:
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 This the the first time I have built a servlet so this is all new to me. I love examples so any help you can give will be greatly appreciated.
You may set a flag in
requestobject inside yourservlet, if the customer is not found e.g.In your JSP, put a JSP scriptlet to check the request attribute and print the message wherever you want e.g. if you want the message after your
<HR/>then:I am sharing the very basic way of achieving your desired result.