Issue
Hello i am new at JSP developing and i need some help about JSP MVC programming I want to pass a value from Servlet and in Dao class i want to receive it in List function and perform operation and return th array to JSP page to use ..
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 Page :
<%
ObsDao dao = new ObsDao();
List<ObsBean> ComNotify = dao.getComNotify();
for (ObsBean UserNotifi : ComNotify) {
.........
.........
}
%>
This is my complete code , but it shows error , why ?
ERRORS :
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)
Solution
Based on your code I assume that "notto" in your table is a company name, so if your sql statement is correct i would rewrite your code as follows:
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 is an object of the ObsBean type, you should retrieve the company name as a string before you can apply it to the sql statement, also note that java is case sensitive.
in JSP do something like
<%
ObsDao dao = new ObsDao();
ObsDao otherdao = new ObsDao();
otherdao.setCompanyName("My company")
List<ObsBean> ComNotify = dao.getComNotify(otherdao);
for (ObsBean UserNotifi : ComNotify) {
.........
.........
}
%>
Answered By - AbelSurace
Answer Checked By - Katrina (JavaFixing Volunteer)