passing value to a jsp page from a java class using DAO

假如想象 提交于 2019-12-22 01:06:50

问题


I wanted to pass the value retrieved on a java class to a page.I am using DAO classes. I have retrieved the values from the database and stored them on String variables.Now I want to set them to the text boxes in my view.jsp page.I am new to this area,can anyone help me out??

View.jsp is as

<%@ 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>

and My Activity ViewDAO.java is as

 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();
    }       

    }
}

Thanks...


回答1:


You need to call the DAO method from your servlet like below:

    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"));

}

your DAO method should be like the below:

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;
    }
}

Hope this will help you.




回答2:


If you are using a front controller[spring mvc] then you can pass the data by doing, model.addAttribute("variable_name ", data); and in the jsp you can access it by doing this ${variable_name};




回答3:


if you are using simple jsp and servelt, then make one ViewController.java.

You can two methods one for handling GET and other for 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>



回答4:


The Servlet decides which page must be loaded. So whatever you get from the DAO must go to the Servlet and through that, to the jsp. You can use a bean class to send values from DAO to Servlet. Like this,

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;
  }

}

And you can make the following changes in DAO after getting the query results in the String. Add these

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

You can pass this object d to the servlet and retrieve the values using the getter methods of the bean. Also, the DAO must be called from the Servlet. And now on the Servlet side.

On the Servlet you can put your code in the get or post method depending on your need. It may be like

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
}

}
}

Based on d returned by the DAO you may redirect to any page you want.



来源:https://stackoverflow.com/questions/23464968/passing-value-to-a-jsp-page-from-a-java-class-using-dao

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!