使用DAO将值传递给java类的jsp页面
我想将java类上检索的值传递给页面。我正在使用DAO类。 我已经从数据库中检索了值并将它们存储在String变量中。现在我想将它们设置为我的view.jsp页面中的文本框。我是这个领域的新手,有谁可以帮我解决?
View.jsp是
Insert title here
和我的活动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(); } } }
谢谢…
您需要从servlet调用DAO方法,如下所示:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // call DAO method to get the email and password HashMap 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 getDetails(user u) { Connection con=ConnectionProvider.getCon(); String uname=u.getUname(); Map 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; } }
希望这会帮助你。
如果您正在使用前端控制器[spring mvc],那么您可以通过执行,model.addAttribute(“variable_name”,data)传递数据; 在jsp中你可以通过这个$ {variable_name}来访问它;
如果你使用简单的jsp和servelt,那么制作一个ViewController.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"%> Insert title here
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
,您可以重定向到您想要的任何页面。