import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.sql.*; import java.text.DateFormat; public class uudised extends HttpServlet{ static final String HEADER="Uudised"; static final String FOOTER=""; boolean logitud=false; public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ 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 type=0; if (request.getParameter("type")!=null){ type=Integer.parseInt(request.getParameter("type")); if (type<0){ type=0; } } 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; } } output.println(HEADER); try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection connection=DriverManager.getConnection("jdbc:odbc:uudised","",""); Statement statement=connection.createStatement(); PreparedStatement ps; ResultSet rs; if (type==0){ rs=statement.executeQuery("select count(*) as kokku from messages "); }else{ ps=connection.prepareStatement("select count(*) as kokku from messages where type=?"); ps.setInt(1, type); rs=ps.executeQuery(); } rs.next(); int kokku=rs.getInt("kokku"); output.println("

Uudised

"); if (type>0){ output.println("Valitud grupis on kokku "+kokku+(kokku==1?" uudis!": " uudist!")+" Kõikide gruppide nägemiseks vajutage SIIA!"); }else{ output.println("Kokku "+kokku+(kokku==1?" uudis ": " uudist ")); } output.println("
Logi "+(logitud?"välja":"sisse")+""); if(logitud){ output.println(" | Lisa uudis"); } output.println(" | Kümme viimast uudist koos kommentaaridega XML's

"); 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) | "); } } } if (type>0){ ps=connection.prepareStatement("select *, count(msgid) as kommentaare from messages left join comments on messages.id=comments.msgid left join types on messages.type=types.typeid where type=? group by id order by id desc limit ?, ?"); ps.setInt(1, type); ps.setInt(2, start); ps.setInt(3, msg_per_page); rs=ps.executeQuery(); }else{ ps=connection.prepareStatement("select *, count(msgid) as kommentaare from messages left join comments on messages.id=comments.msgid left join types on messages.type=types.typeid group by id order by id desc limit ?, ?"); ps.setInt(1, start); ps.setInt(2, msg_per_page); rs=ps.executeQuery(); } Date aeg; String strAeg; while(rs.next()){ aeg=rs.getDate("time"); if (aeg!=null){ DateFormat df = DateFormat.getDateInstance(DateFormat.FULL); strAeg=df.format(aeg); } else{ strAeg=" (aeg puudub)"; } int id=rs.getInt("id"); int typeid=rs.getInt("typeid"); output.println(""+ ""+ "
"+rs.getString("header")+"
Kirjutas: "+rs.getString("msgauthor")+" @ "+strAeg+"Tüüp: "+rs.getString("name")+"
"+rs.getString("contents")+"
"+(logitud?("Muuda | Kustuta"):" ")+"Kommentaare: "+rs.getInt("kommentaare")+"

"); } output.println(""); connection.close(); }catch(Exception e ){ output.println("

VIGA

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