LINUX.ORG.RU

trouble with Tomcat


0

0

Добрый день.
вот тут занимаемся изучением jsp. столкнулись со следующее проблемой.
раньше был простой сервлет из mysql брал данные и отдавал браузеру.
затем мы его улучшили добавив к нему пул соединении. работает
а потом прочитали умных книг про mvc2 и решили переделать сервлет на
модель(class) представление(jsp) и контролер(servlet).
однако не работает зараза не знаю в какую сторону копать.

anonymous

ето первый класс
public class JDBCServlet extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
Connection conn = null;
PrintWriter writer = response.getWriter();
response.setContentType("text/html");
writer.println("<html><body>");
writer.println("<table><tr>");
writer.println("<td>id</id>");
writer.println("<td>title</td>");
writer.println("<td>author</td>");
writer.println("</tr>");
try {
Class.forName(new String("com.mysql.jdbc.Driver"));
conn = DriverManager.getConnection("jdbc:mysql://localhost/achtungpanzer", "achtungpanzer", "achtungpanzer");
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM stats");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
writer.println("<tr>");
writer.println("<td>"+rs.getString("id")+"</td >");
writer.println("<td>"+rs.getString("name")+"</ td>");
writer.println("<td>"+rs.getString("value")+"< /td>");
writer.println("</tr>");
}
rs.close();
pstmt.close();
} catch (ClassNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
writer.println("</table>");
}

}

anonymous
()

это второй класс
public class JNDIDSServlet extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter writer = response.getWriter();
response.setContentType("text/html;encoding=Cp1251");
response.setCharacterEncoding("Cp1251");
writer.println("<html><body>");
writer.println("<table><tr>");
writer.println("<td>id</id>");
writer.println("<td>name</td>");
writer.println("<td>value</td>");
writer.println("</tr>");
try {
Context ctx = (Context) new InitialContext().lookup("java:/comp/env");
DataSource dataSource = (DataSource) ctx.lookup("jdbc/AP");
Connection conn = dataSource.getConnection();
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM stats");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
writer.println("<tr>");
writer.println("<td>" + rs.getString("id") + "</td>");
writer.println("<td>" + rs.getString("name") + "</td>");
writer.println("<td>" + rs.getString("value") + "</td>");
writer.println("</tr>");
}
rs.close();
pstmt.close(); } catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
writer.println("</table>");
}

anonymous
()

класс модель
public class StatsModel {
protected ResultSet result;
protected Connection connection = null;

protected Vector id = new Vector();
protected Vector name = new Vector();
protected Vector value = new Vector();

public void initialize() throws IOException,SQLException {
try {
Context ctx = (Context) new InitialContext().lookup("java:/comp/env");
DataSource dataSource = (DataSource) ctx.lookup("jdbc/AP");
connection = dataSource.getConnection();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}

public void execute() throws IOException,SQLException {
try {
Statement statement = connection.createStatement();
result = statement.executeQuery("select id, name, value from stats where lang=ru");

while (result.next()) {
id.addElement(result.getString("id"));
name.addElement(result.getString("name"));
value.addElement(result.getString("value"));
}
result.close();
statement.close();

} catch (Throwable theException) {
theException.printStackTrace();
}
}
public void destroy() {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}

public String getID(int index) throws IndexOutOfBoundsException,ArrayIndexOutOfBoundsException {
return (String) id.elementAt(index);
}
public String getName(int index) throws IndexOutOfBoundsException,ArrayIndexOutOfBoundsException {
return (String) name.elementAt(index);
}
public String getValue(int index) throws IndexOutOfBoundsException,ArrayIndexOutOfBoundsException {
return (String) value.elementAt(index);
}
public int getSize() {
return id.size();
}
}

anonymous
()

класс контроллера
public class StatsSerlvet extends HttpServlet {

public void doGet(HttpServletRequest request,
HttpServletResponse response) {
try {
StatsModel statsModel = new StatsModel();
statsModel.initialize();
statsModel.execute();

request.setAttribute("StatsModel",statsModel);

ServletContext servletContext = getServletContext();
RequestDispatcher dispatcher = servletContext.getRequestDispatcher("/StatsModelResult.jsp");
dispatcher.forward(request,response);

} catch (Throwable theException) {
theException.printStackTrace();
}
}
}

anonymous
()

представление <html> <head><title>Simple jsp page</title></head> <body> <jsp:useBean id="statsModel" class="kz.nurm.www.AchtungPanzer.stats.StatsModel" scope="request"> </jsp:useBean> <table> <tr> <td>ID</td> <td>Name</td> <td>Value</td> </tr> <% for (int _i=0; _i< statsModel.getSize();_i++) { %> <tr> <td><%=statsModel.getID(_i)%></td> <td><%=statsModel.getName(_i)%></td> <td><%=statsModel.getValue(_i)%></td> </tr> <%}%> </table> </body> </html>

anonymous
()

context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/AP" docBase="AP"
debug="5" reloadable="true" crossContext="true">
<Resource name="jdbc/AP" auth="Container" type="javax.sql.DataSource"
maxActive="50" maxIdle="10" maxWait="10000"
username="achtungpanzer" password="achtungpanzer" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/achtungpanzer"/>

</Context>

anonymous
()

web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"; > <servlet> <servlet-name>StatsServlet</servlet-name> <servlet-class>kz.nurm.www.AchtungPanzer.stats.StatsSerlvet</servlet-cl ass> </servlet>

<servlet-mapping> <servlet-name>StatsServlet</servlet-name> <url-pattern>/StatsServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>JDBCServlet</servlet-name> <servlet-class>kz.nurm.www.AchtungPanzer.JDBCServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>JDBCServlet</servlet-name> <url-pattern>/JDBCServlet</url-pattern> </servlet-mapping>

<servlet> <servlet-name>JNDIDSServlet</servlet-name> <servlet-class>kz.nurm.www.AchtungPanzer.JNDIDSServlet</servlet-class&g t; </servlet> <servlet-mapping> <servlet-name>JNDIDSServlet</servlet-name> <url-pattern>/JNDIDSServlet</url-pattern> </servlet-mapping> <resource-ref> <description> Resource reference to a factory for java.sql.Connection instances that may be used for talking to a particular database that is configured in the server.xml file. </description> <res-ref-name> jdbc/AP </res-ref-name> <res-type> javax.sql.DataSource </res-type> <res-auth> Container </res-auth> </resource-ref>

</web-app>

anonymous
()

>работает

>а потом прочитали умных книг

работает - не трож! и Селектера не слушай ;)

Pi ★★★★★
()
Ответ на: комментарий от Pi

тут проблема, мне так, кажется, в том что контроллер не может получить данные от модели?

anonymous
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.