从mysql数据库获取值以在JSP中显示(JAVA) [英] Getting values from mysql database to show in JSP (JAVA)

查看:424
本文介绍了从mysql数据库获取值以在JSP中显示(JAVA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用一些帮助如何从mysql数据库获取值,并将其解析为servlet,这将打开一个jsp文件Showlist.jsp并将所有值作为参数。

I could use some help how to get values from a mysql database, and parse it to a servlet, which opens a jsp file "Showlist.jsp" and have all the values as parameter.

我的数据库中的表格包含:

My tabel in my database contains:

ID, Name, Initials, cpr, password, role.

我在我的系统中创建了3个用户,我想在jsp文件中显示所有用户。

I have created 3 users in my system, and i want to show all users in a jsp file.

现在我有3层,其中包含以下类:

Right now i have 3 layers which contain the following classes:

查看图层
- html / jsp文件

View layer - html / jsp files

功能层
- 类功能

Function layer - Class function

数据层
- 类DAOoperator(包含mysql语句。
- 类DTOoperator(这是我使用getter和setter方法的对象)
- class DataAccess(包含与mysql数据库的连接)

Data layer - Class DAOoperator (Contains mysql statements. - Class DTOoperator (Which is my object with getter and setter methods) - class DataAccess (contains connection to mysql database)

所有这些都由一个名为Controller的servlet控制。

All this is controlled by a servlet called Controller.

是有一个简单的方法吗?

Is there an easy way to do this??

推荐答案

在servlet中访问DAO并调用返回的getUser()方法用户列表。然后将结果设置为请求。

In the servlet you access the DAO and call the getUser() method which returns a list of user. Then you set the result to the request.

request.setAttribute("users", userList);

在js中p你可以通过:

In the jsp you can get it by:

    <% List<User> userList = (List<User>)request.getAttribute("users"); %>

更好的方法是在jsp中使用jstl而不是普通的java代码。

a better approach is to use jstl in the jsp instead of plain java code.

这篇关于从mysql数据库获取值以在JSP中显示(JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆