由於您使用<% ... %>
,你應該寫有效 Java代碼,讓你在你的語句結尾缺少一個分號;
:
<%Interets inte=new Interets(${IdUser})%>
^here you're missing a semicolon...
而且,你不能混用EL與小腳本,所以代碼應該是:
<%
Integer idUser = (Integer)request.getAttribute("IdUser");
//if the attribute is stored in HttpSession (probably)
//or ServletContext (oddly), use the right method to get it
Interets inte =new Interets(idUser);
%>
只是要注意,這可能會解決您當前編輯的問題,但也有在當前的DESI幾個問題gn例如你不初始化Statement
任何地方你Interets
類,但依然在構造函數中使用它......
你應該學會基本的Java和JDBC的基本首次嘗試打開到數據庫的連接之前並打印結果在網絡環境中。我不會深入到這些主題,而是寫一個可能的解決方案,以你目前的代碼,我會評論了錯誤的代碼,以便您注意到什麼是錯的,或者你不應該做的:
public class Interets {
//static Statement St ;
//public ResultSet rs;
public List<String> getDescriptionOfInterets(Integer idUser) {
String res=" ";
//these three: Connection, Statement, ResultSet MUST ALWAYS be local
//variables of a method, NEVER assigned to class field AND NEVER to
//static variables to avoid synchronization problems in a
//multithreaded environment such as a web application
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<String> descriptionList = new ArrayList<String>();
try{
//this is one way to open a database connection
//you should change it to a database connection pool
//which is a different topic you should search about
//since you didn't say which database you're working with
//I'll assume it's mysql
Class.forName("com.mysql.jdbc.Driver");
//similar with the database credentials
String user = "root";
String password = "root";
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/dbname", user, password);
/*
ResultSet result = St.executeQuery("SELECT description FROM interets, avoir, consomateur WHERE avoir.id_interet=interets.id_interets AND avoir.id_user=consomateur.code_bar AND consomateur.code_bar="+IdUser+"");
ResultSetMetaData resultMeta = (ResultSetMetaData) result.getMetaData();
*/
ps = con.prepareStatement("SELECT description "
+ "FROM interets, avoir, consomateur "
+ "WHERE avoir.id = interets.id AND "
+ "avoir.id?user = consomateur.code_bar AND "
+ "contomateur.code_bar = ?");
ps.setInt(1, idUser);
rs = ps.executeQuery();
//while(result.next()){
while (rs.next) {
//String Newligne=System.getProperty("line.separator");
//for(int i = 1; i <= resultMeta.getColumnCount(); i++)
//res=res+Newligne+result.getObject(i).toString();
//System.out.println(res);
descriptionList.add(rs.getString(1));
}
} catch (Exception e) {
//Handle your error! This is very important to check the causes of the problem
//you should at least print the stacktrace
//printing a single line saying "oh no! I got an error" doesn't help
System.out.println("Erreur dans la requete d'affichage.");
e.printStacktrace();
} finally {
//remember to ALWAYS close the opened resources
close(rs);
close(ps);
close(con);
}
}
public void close(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
//silent...
}
}
}
public void close(Statement st) {
if (st != null) {
try {
st.close();
} catch (SQLException e) {
//silent...
}
}
}
public void close(Connection con) {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
//silent...
}
}
}
}
然後在你的JSP:
Description of Interets
<table>
<%
Integer idUser = (Integer)request.getAttribute("IdUser");
//if the attribute is stored in HttpSession (probably)
//or ServletContext (oddly), use the right method to get it
Interets interets = new Interets();
List<String> descriptionList = interets.getDescriptionOfInterets(idUser);
for (String description : descriptionList) {
%>
<tr>
<td><%= description %></td>
</tr>
<%
}
%>
</table>
甚至更好,使用Servlet,Expression Language和JSTL(假設你的JSP被命名爲 「interets.jsp」,並在根包):
@WebServlet("/interets")
public class InteretsServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
Integer idUser = (Integer)session.getAttribute("IdUser");
Interets interets = new Interets();
List<String> descriptionList = interets.getDescriptionOfInterets(idUser);
request.setAttribute("descriptionList", descriptionList);
request.getRequestDispatcher("/interets.jsp").forward(request, response);
}
}
而且JSP可以像
Description of Interets
<table>
<c:forEach items="${descriptionList}" value="description">
<tr>
<td>${description}</td>
</tr>
</c:forEach>
</table>
我已經在我的servlet這個方法:\t公共無效doInteret(HttpServletRequest的請求,HttpServletResponse的響應)拋出的SQLException,了ServletException,IOException異常{ \t \t \t \t \t ID用戶=(整數)會話。的getAttribute( 「ID用戶所」); \t \t Interets inte = new Interets(); \t \t} – user3365621