简体   繁体   English

使用servlet和jsps进行数据库搜索

[英]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. 我正在学习Java Servlet,并尝试编写一个程序,要求用户输入名称,并在数据库中列出所有具有匹配名称的行。 My problem is, I am storing the user given value in the setter method of a bean class from my controller class. 我的问题是,我将用户给定的值存储在控制器类的bean类的setter方法中。 However, I am not able to retrieve it from dao class. 但是,我无法从dao类中检索它。 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. 我猜您正在尝试应用“模型2”模式,以便在不同的层中隔离3个不同的组件。

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. 一种是放置代码以处理用户请求(称为controller )并构建其他不同组件的地方,即Bean / DTO ,在其中放置通常涉及响应的数据。 And finally a jsp archive ( view ), where you put how to write the response using the previously loaded Bean. 最后是一个jsp存档( view ),在其中放置了如何使用先前加载的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 . 允许将视图和控制器分开的关键抽象bean ,并且知道将bean 放置在何处,以使其对于ControllerView都可见。 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 . 在您的情况下,我看到您正在使用一个Controller,正在构建一个Bean,我想您忘记了将该bean传递给view ,然后在一些scriplet标签中使用了该bean。

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 : 然后在jsp上(在您的情况下为“ SearchList.jsp”中),您可能会放置以下内容:

<%
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). 这是模型2的基础,它是复合模式(由其他模式构成的模式),毕竟,复合模式是用于制作Java Web应用程序(Spring MVC,其余所有已知框架都将Struts置于此components之上,因为它只是用于构建Web应用程序的JSEE规范的一部分。

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. 我建议阅读《 Head First Servlets and JSP,第二版》这本书,非常适合开始使用作为服务器的普通Servlet,jsp和普通类。

I guess that is the very basic information you need to start doing some servlets that rocks! 我想这是您开始做一些摇摆不定的servlet所需的非常基本的信息! Keep up practice! 继续练习!

Greetings! 问候!

And hope it helps! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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