使用DAO将值传递给java类的jsp页面

时间:2014-05-05 04:49:03

标签: java jsp dao

我想将在java类上检索到的值传递给一个页面。我正在使用DAO类。 我从数据库中检索了值并将它们存储在String变量中。现在我想将它们设置为我的view.jsp页面中的文本框。我是这个领域的新手,有人可以帮帮我吗?

View.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>

    <form action="process.jsp">
    Enter Name    <br/> <br> <input type="text" name="uname"  onclick="this.value=''"/><br/><br/>

    <input type="submit" value="view details"/><br/><br/>
    Email id:   <br/> <input type="text"  name="email"  id="email" > <br/><br/>
    password:   <br/> <input type="text"  name="passw"  id="passw"><br/><br/>

    </form>        
    </body>
    </html>

和我的活动 ViewDAO.java

 public static void  view(user u) {
    Connection con=ConnectionProvider.getCon(); 
    String uname=u.getUname();
    try {
        PreparedStatement ps=con.prepareStatement("select email,pass from  S1.USER432 where name='"+uname+"'");
        ResultSet rs = ps.executeQuery();       

        while (rs.next()) {

            String email = rs.getString("EMAIL");
            String pass = rs.getString("PASS");

            }

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       

    }
}

...谢谢

4 个答案:

答案 0 :(得分:1)

如果您使用前置控制器[spring mvc],那么您可以通过执行来传递数据, model.addAttribute(“variable_name”,data); 在jsp中,您可以通过执行$ {variable_name};

来访问它

答案 1 :(得分:0)

如果您使用的是简单的jsp和servlet,那么请创建一个View Controller.java。

您可以使用两种方法来处理GET,将其他方法用于POST

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String email = request.getParameter("email");
            String password = request.getParameter("password");

    request.setAttribute("email", email);
            request.setAttribute("password", password);
                        request.getRequestDispatcher("view.jsp").forward(request, response);
         }
        catch(Exception e){


        }

view.jsp的

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>

    <form action="process.jsp">
    Enter Name    <br/> <br> <input type="text" name="uname"  onclick="this.value=''"/><br/><br/>

    <input type="submit" value="view details"/><br/><br/>
    Email id:   <br/> <input type="text"  name="email"  id="email" value="<%=email%>" > <br/><br/>
    password:   <br/> <input type="text"  name="passw"  id="passw" value="<%=password%>"><br/><br/>


    </form>

    </body>
    </html>

答案 2 :(得分:0)

Servlet决定必须加载哪个页面。所以无论你从DAO得到什么,都必须转到Servlet并通过它转到jsp。您可以使用bean类将值从DAO发送到Servlet。 像这样,

public class Details{
  private String email;
  private String password;

  public void setEmail(String email){
    this.email = email;

  }
  public void setPassword(String password){
    this.password= password;

  }
  public String getEmail(){
     return this.email;
  }
  public String getPassword(){
     return this.password;
  }

}

在String中获取查询结果后,您可以在DAO中进行以下更改。添加这些

Details d = new Details();
d.setEmail(email);
d.setPassword(pass);
return d;

您可以将此对象d传递给servlet,并使用bean的getter方法检索值。此外,必须从Servlet调用DAO。 现在在Servlet方面。

在Servlet上,您可以根据需要将代码放入get或post方法中。可能就像

public class ExampleServlet extends HttpServlet{

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

String email = request.getParameter("email"); //got from the jsp where "email" is the name attribute of the input field
Details d = new Details();
d = ViewDao.view(user_object); //the bean sent by DAO. "user_object" is parameter that your DAO method is taking in your code

if(d.getEmail()!=null){ //just an example

 // your code that redirects desired page
}

}
}

根据DAO返回的d,您可以重定向到您想要的任何页面。

答案 3 :(得分:0)

您需要从servlet调用DAO方法,如下所示:

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

      // call DAO method to get the email and password
    HashMap<String,String> map=ViewDAO.getDetails();

    //from the map you will get the  email and password.then you need to set them in the attributes and get them in your jsp

     request.setAttribute("email", map.get("email"));
      request.setAttribute("password", map.get("password"));

}

你的DAO方法应如下所示:

public static HashMap<String,String>  getDetails(user u) {
    Connection con=ConnectionProvider.getCon(); 
    String uname=u.getUname();
    Map<String,String> map=new HashMap<>();
    try {
        PreparedStatement ps=con.prepareStatement("select email,pass from  S1.USER432 where name='"+uname+"'");
        ResultSet rs = ps.executeQuery();       

        while (rs.next()) {

            String email = rs.getString("EMAIL");
            String pass = rs.getString("PASS");

            }
       map.put("email",email);
       map.put("password",pass);


    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
    return map;
    }
}

希望这会对你有所帮助。