import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.sql.*; import java.text.DateFormat; public class uudisteKom extends HttpServlet{ static final String HEADER="Uudised"; static final String FOOTER=""; boolean logitud=false; public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ Date aeg; String strAeg; DateFormat df = DateFormat.getDateInstance(DateFormat.FULL); response.setContentType("text/html"); PrintWriter output=response.getWriter(); HttpSession session = request.getSession(true); Boolean tmpLog=(Boolean)session.getAttribute("logitud"); if (tmpLog!=null){ logitud=tmpLog.booleanValue(); } int msg_per_page=5; if(request.getParameter("limit")!=null){ msg_per_page=Integer.parseInt(request.getParameter("limit")); if (msg_per_page>100 || msg_per_page<0){ msg_per_page=10; } } int start=0; if(request.getParameter("start")!=null){ start=Integer.parseInt(request.getParameter("start")); if (start<0){ start=0; } } int id=0; if(request.getParameter("id")!=null){ id=Integer.parseInt(request.getParameter("id")); if (id<0){ response.sendRedirect("uudised"); } }else{ response.sendRedirect("uudised"); } try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection connection=DriverManager.getConnection("jdbc:odbc:uudised","",""); PreparedStatement ps=connection.prepareStatement("select count(*) as kokku from comments where msgid=?"); ps.setInt(1, id); ResultSet rs=ps.executeQuery(); rs.next(); int kokku=rs.getInt("kokku"); ps=connection.prepareStatement("select * from messages left join types on messages.type=types.typeid where messages.id=?"); ps.setInt(1, id); rs=ps.executeQuery(); if (!rs.next()){ response.sendRedirect("uudised"); } aeg=rs.getDate("time"); if (aeg!=null){ strAeg=df.format(aeg); } else{ strAeg=" (aeg puudub)"; } output.println(HEADER); output.println("

Kommentaarid

"); output.println("Kokku "+kokku+(kokku==1?" kommentaar!":" kommentaari!")); output.println("
Näita uudiseid
"); output.println(""+ ""+ "
"+rs.getString("header")+"
Kirjutas: "+rs.getString("msgauthor")+" @ "+strAeg+"Tüüp: "+rs.getString("name")+"
"+rs.getString("contents")+"

"); if (kokku>5){ output.println("
"+ ""+ "Näitan kaupa.
"); } if (kokku>msg_per_page){ output.println("| "); for(int i=0; i=i && start"+(i+1)+"-"+(((i+msg_per_page) | "); } } } ps=connection.prepareStatement("select * from comments where msgid=? order by comid desc limit ?, ? "); ps.setInt(1, id); ps.setInt(2, start); ps.setInt(3, msg_per_page); rs=ps.executeQuery(); while(rs.next()){ aeg=rs.getDate("comtime"); if (aeg!=null){ strAeg=df.format(aeg); } else{ strAeg=" (aeg puudub)"; } output.println(""+ ""+ "
"+rs.getString("comauthor")+" @ "+strAeg+""+ (logitud?("Kustuta"):" ")+"
"+rs.getString("comcontents")+"

"); } output.println("
"+ ""+ ""+ ""+ "
Nimi:
Kommentaar:
 
"); connection.close(); }catch(Exception e ){ output.println("

VIGA

"+e.getMessage()); } output.println(FOOTER); } }