2010-5-2 23:15:37org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet GetBlogServlet threw exception
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at com.jia.blog.GetBlogServlet.doGet(GetBlogServlet.java:38)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:619)
代码如下:
package com.jia.blog;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.dbutils.QueryRunner;
public class CommentServlet extends HttpServlet {
private static final long serialVersionUID = 7538344999745634584L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//doGet(request,response);
request.setCharacterEncoding("UTF-8");
String method = request.getParameter("method");
if(method.equals("add")){
add(request,response);
}
}
public void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
String name = request.getParameter("name");
String content = request.getParameter("content");
String blog_Id = request.getParameter("blog_Id");
String path = request.getContextPath();
if(name==null ||name.equals("")){
name="匿名";
}
String sql = "insert into comment(username,content,blog_Id,createdtime)values(?,?,?,now())";
String ps [] = {name,content,blog_Id};
QueryRunner qr = DBHelper.getQueryRunner();
try{
qr.update(sql,ps);
System.out.println(blog_Id);
}catch (Exception e) {
e.printStackTrace();
}
response.sendRedirect(path+"/GetBlogServlet?id="+blog_Id);
//request.getRequestDispatcher("/GetBlogServlet?id="+blog_Id).forward(request,
//response);
}
}
还有这个Servlet的代码如下:
package com.jia.blog;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
public class GetBlogServlet extends HttpServlet {
private static final long serialVersionUID = 1040014153607544023L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
DataSource ds = null;
String id = request.getParameter("id");
try {
// 通过在context.xml文件,设定的数据源对象的名字,获取数据源对象
Context context = new InitialContext();
ds = (DataSource) context.lookup("java:/comp/env/jdbc/mysql");
} catch (Exception e) {
System.out.println("获取数据源时出错");
}
try {
String sql = "select id,title,content,createdtime from blog where id = "
+ id;
QueryRunner qr = new QueryRunner(ds);
List list = (List) qr.query(sql, new BeanListHandler(Blog.class));
Blog blog = (Blog) list.get(0);
request.setAttribute("blog", blog);
request.getRequestDispatcher("/displayblog.jsp").forward(request,
response);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
还有这个JSP页面如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.jia.blog.Blog" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>阅读博客</title>
</head>
<body>
<%Blog blog=(Blog)request.getAttribute("blog"); %>
<td><form id="form1" name="form1" method="post" action="/blog/CommentServlet">
<input type="hidden" name="method" value="add"/>
<input type="hidden" name="blog_id" value="<%=blog.getId() %>"/>
<table width="684" border="1">
<tr>
<td width="209" align="center">评论人:</td>
<td width="459"><label>
<input name="name" type="text" id="name" size="35" />
</label></td>
</tr>
<tr>
<td align="center">评论内容:</td>
<td><label>
<textarea name="content" cols="60" rows="10"></textarea>
</label></td>
</tr>
<tr>
<td> </td>
<td><label>
<input type="submit" name="Submit" value="提交" />
<input type="reset" name="Submit2" value="取消" />
</label></td>
</tr>
</table>
</form>
</body>
</html>