简体   繁体   中英

Database search using servlets and jsps

I am learning java servlets and trying to write a program that asks the user to enter a name ,and lists all the rows with the matching name in the db. My problem is, I am storing the user given value in the setter method of a bean class from my controller class. However, I am not able to retrieve it from dao class. Not getting any exceptions or errors, the code is not producing any results. Any help would be appreciated.

Controller Class:

protected void doPost(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
SearchDataHolder sch = new SearchDataHolder();
String fname = (String) request.getParameter("fname");
String lname = (String) request.getParameter("lname");
String email = (String) request.getParameter("email");
sch.setFname(request.getParameter("fname"));
sch.setLname((String) request.getParameter("lname"));
sch.setEmail((String) request.getParameter("email"));
if (((fname).isEmpty()) && ((lname).isEmpty()) && (((email).isEmpty() ))) {
    response.sendRedirect("BlankError.jsp");
    return;
} else {
    RequestDispatcher rd = request
            .getRequestDispatcher("SearchList.jsp");
    rd.forward(request, response);
    return;
}

Dao Class:

Connection con = ConnectionUtils.createConnection();
PreparedStatement ps;
List<SearchDataHolder> users = new ArrayList<SearchDataHolder>();
SearchDataHolder sdh = new SearchDataHolder();
String firstName = sdh.getFname();
String lastName = sdh.getLname();
String email = sdh.getEmail();
try {

    String userList = "select * from Personal_Info where Fname='"
            + firstName + "' or Lname='" + lastName + "' or Email='"
            + email + "'";
    ps = con.prepareStatement(userList);
    ResultSet rs = ps.executeQuery();
    while (rs.next()) {
        sdh.setFname((String) rs.getString("Fname"));
        sdh.setLname((String) rs.getString("Lname"));
        sdh.setMname((String) rs.getString("Mname"));
        sdh.setSex(rs.getString("Sex"));
        sdh.setDob((String) rs.getString("Date"));
        sdh.setEmail((String) rs.getString("Email"));
        sdh.setPtype((String) rs.getString("PhoneType"));
        sdh.setPhone((String) rs.getString("Phone"));
        sdh.setStreet((String) rs.getString("Street"));
        sdh.setCity((String) rs.getString("City"));
        sdh.setCity((String) rs.getString("State"));
        sdh.setZip((String) rs.getString("ZipCOde"));
        sdh.setCountry((String) rs.getString("Country"));
        users.add(sdh);
    }
} catch (SQLException e) {
    e.printStackTrace();
}
return users;

i guess that you are trying to apply the "Model 2" pattern in order to isolate in different layers, 3 different components.

One is where all you put the code to handle the user request (know as controller ) and build other, different component, that is a Bean / DTO where you put the data that is involved usually response. And finally a jsp archive ( view ), where you put how to write the response using the previously loaded Bean.

The key abstraction that allow to separate the view and the controller is the bean , and knowing where to place the bean in order to make it visible for the both Controller and for the View . In your case, i see that you are using a Controller, that is building a Bean, what i guess you forgot to pass that bean to the view , and then use that bean inside some scriplet tags .

Inside your controller there should be a line like this :

request.setAttribute("nameOfTheBeanToUseInsideTheJSPScriptlet",bean)

And later on the jsp (in your case in the "SearchList.jsp"), you probably will put something like this :

<%
Bean bean = request.getAttribute("nameOfTheBeanToUseInsideTheJSPScriptlet");
out.print(bean.getAValue());
%>

This is the very basic of model 2, that is composite pattern (a pattern made of other patterns) that is, after all, the underlaying base of all the different more complex framework that are used for making web applications of java (Spring MVC, Struts, the rest of all know frameworks, are placed on top of this components , because it is just part of JSEE specification for building web apps).

I suggest to read the book "Head First Servlets and JSP, 2nd Edition", is it perfect for starting working with plain servlets, jsp and normal classes that servers as beans.

I guess that is the very basic information you need to start doing some servlets that rocks! Keep up practice!

Greetings!

And hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM