如何将值从 servlet 传递到 Dao class 并执行操作和 return List/Arraylist 到 JSP 页面
How to pass value from servlet to Dao class and perform operation and return List/ Arraylist to JSP page
你好我是 JSP 开发新手,我需要一些关于 JSP MVC 编程的帮助我想从 Servlet 和 Dao 中传递一个值 class 我想接收它在 List 函数中执行操作并将第 return 个数组添加到 JSP 页面以使用 ..
Servlet
String company = "ABCD";
ObsBean ComName = new ObsBean();
ComName.setCompanyName(company);
dao.getComNotify(ComName);
Bean Class (ObsBean)
private String CompanyName;
public String getCompanyName() {
return CompanyName;
}
public void setCompanyName(String CompanyName) {
this.CompanyName = CompanyName;
}
**DAO Class (ObsDao) **
public List getComNotify(ObsBean ComName) {
List<ObsBean> comNotify = new ArrayList<ObsBean>();
String cname = ComName.getCompanyName();//getting from bean class by getter
try {
String sql = "SELECT * from ObsNotify where notto='"+cname+"'";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
ObsBean userNotify = new ObsBean();
userNotify.setNotifyName(rs.getString("notname"));
userNotify.setNotifyBy(rs.getString("notby"));
userNotify.setNotifyTo(rs.getString("notto"));
userNotify.setNotifyDate(rs.getString("notdate"));
comNotify.add(userNotify);
}
}
catch (Exception e) {
out.print("Error for User Notification - : "+e);
}
return comNotify;
}
IN JSP 页:
<%
ObsDao dao = new ObsDao();
List<ObsBean> ComNotify = dao.getComNotify();
for (ObsBean UserNotifi : ComNotify) {
.........
.........
}
%>
这是我的完整代码,但显示错误,为什么?
错误:
HTTP Status 500 – Internal Server Error
Type Exception Report
Message javax.servlet.ServletException: java.lang.NoSuchMethodError: com.ApexCorner.ModelDao.ObsDao.getComNotify()Ljava/util/List;
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.NoSuchMethodError: com.ApexCorner.ModelDao.ObsDao.getComNotify()Ljava/util/List;
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:565)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:466)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
在您的 Dao class 中,CompanyName 应该有 getter 方法。所以试试下面的代码:
STRING COM = Comname;
,<-- 将此行更改为 String COM=Comname.getCompanyName();
根据您的代码,我假设您的 table 中的 "notto" 是一个公司名称,因此如果您的 sql 声明是正确的,我将重写您的代码如下:
public List getComNotify(ObsBean Comname) {
List<ObsBean> comNotify = new ArrayList<ObsBean>();
String COM = Comname.getcompanyname();
try {
String sql = "SELECT * from ObsNotify where notto='"+COM+"'";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
ObsBean userNotify = new ObsBean();
int totalRows = rs.getRow();
userNotify.setNotifyName(rs.getString("notname"));
userNotify.setNotifyBy(rs.getString("notby"));
comNotify.add(userNotify);
}
}
catch (Exception e) {
out.print("Error for User Notification - : "+e);
}
return comNotify;
}
Comname是一个ObsBean类型的对象,你需要将公司名取回字符串才能应用到sql语句中,还要注意java是区分大小写的。
在JSP中做类似
的事情
<%
ObsDao dao = new ObsDao();
ObsDao otherdao = new ObsDao();
otherdao.setCompanyName("My company")
List<ObsBean> ComNotify = dao.getComNotify(otherdao);
for (ObsBean UserNotifi : ComNotify) {
.........
.........
}
%>
你好我是 JSP 开发新手,我需要一些关于 JSP MVC 编程的帮助我想从 Servlet 和 Dao 中传递一个值 class 我想接收它在 List 函数中执行操作并将第 return 个数组添加到 JSP 页面以使用 ..
Servlet
String company = "ABCD";
ObsBean ComName = new ObsBean();
ComName.setCompanyName(company);
dao.getComNotify(ComName);
Bean Class (ObsBean)
private String CompanyName;
public String getCompanyName() {
return CompanyName;
}
public void setCompanyName(String CompanyName) {
this.CompanyName = CompanyName;
}
**DAO Class (ObsDao) **
public List getComNotify(ObsBean ComName) {
List<ObsBean> comNotify = new ArrayList<ObsBean>();
String cname = ComName.getCompanyName();//getting from bean class by getter
try {
String sql = "SELECT * from ObsNotify where notto='"+cname+"'";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
ObsBean userNotify = new ObsBean();
userNotify.setNotifyName(rs.getString("notname"));
userNotify.setNotifyBy(rs.getString("notby"));
userNotify.setNotifyTo(rs.getString("notto"));
userNotify.setNotifyDate(rs.getString("notdate"));
comNotify.add(userNotify);
}
}
catch (Exception e) {
out.print("Error for User Notification - : "+e);
}
return comNotify;
}
IN JSP 页:
<%
ObsDao dao = new ObsDao();
List<ObsBean> ComNotify = dao.getComNotify();
for (ObsBean UserNotifi : ComNotify) {
.........
.........
}
%>
这是我的完整代码,但显示错误,为什么?
错误:
HTTP Status 500 – Internal Server Error
Type Exception Report
Message javax.servlet.ServletException: java.lang.NoSuchMethodError: com.ApexCorner.ModelDao.ObsDao.getComNotify()Ljava/util/List;
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.NoSuchMethodError: com.ApexCorner.ModelDao.ObsDao.getComNotify()Ljava/util/List;
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:565)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:466)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
在您的 Dao class 中,CompanyName 应该有 getter 方法。所以试试下面的代码:
STRING COM = Comname;
,<-- 将此行更改为 String COM=Comname.getCompanyName();
根据您的代码,我假设您的 table 中的 "notto" 是一个公司名称,因此如果您的 sql 声明是正确的,我将重写您的代码如下:
public List getComNotify(ObsBean Comname) {
List<ObsBean> comNotify = new ArrayList<ObsBean>();
String COM = Comname.getcompanyname();
try {
String sql = "SELECT * from ObsNotify where notto='"+COM+"'";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
ObsBean userNotify = new ObsBean();
int totalRows = rs.getRow();
userNotify.setNotifyName(rs.getString("notname"));
userNotify.setNotifyBy(rs.getString("notby"));
comNotify.add(userNotify);
}
}
catch (Exception e) {
out.print("Error for User Notification - : "+e);
}
return comNotify;
}
Comname是一个ObsBean类型的对象,你需要将公司名取回字符串才能应用到sql语句中,还要注意java是区分大小写的。
在JSP中做类似
的事情<%
ObsDao dao = new ObsDao();
ObsDao otherdao = new ObsDao();
otherdao.setCompanyName("My company")
List<ObsBean> ComNotify = dao.getComNotify(otherdao);
for (ObsBean UserNotifi : ComNotify) {
.........
.........
}
%>