高手帮我看看那里错了,谢谢了!

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>&nbsp;</td>
          <td><label>
            <input type="submit" name="Submit" value="提交" />
            <input type="reset" name="Submit2" value="取消" />
          </label></td>
        </tr>
       
      </table>
        </form>

</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值